Can SV / HV / AV be used / created / modified outside of the main stream?

I am trying to create an SV with newSVpvn () on a new thread created with pthread_create (). At the moment my program crashes. The C function where the crash occurs is as follows

void *_inet_aton(void *v_arg) {
    SV* rv = &PL_sv_undef;
    struct thread_arg *arg = (struct thread_arg *)v_arg;

    struct hostent *rslv = gethostbyname(arg->host);
    if (!rslv) {
        goto RET;
    }

    if (rslv->h_addrtype == AF_INET && rslv->h_length == 4) {
        // !!!CRASH HERE!!!
        rv = newSVpvn((char *)rslv->h_addr, rslv->h_length);
    }

    RET:
        free(arg->host);
        free(arg);
}

      

And XSUB

void
inet_aton(Net_DNS_Native *self, char *host)
    CODE:
        pthread_t tid;
        struct thread_arg *t_arg = malloc(sizeof(struct thread_arg));
        t_arg->self = self;
        t_arg->host = strdup(host);
        pthread_create(&tid, &self->thread_attrs, _inet_aton, (void *)t_arg);

      

Test example

use blib;
use Net::DNS::Native;


my $dns = Net::DNS::Native->new();
$dns->inet_aton("google.com");

# wait for a thread
sleep 10;

      

The complete code can be found here: https://github.com/olegwtf/p5-Net-DNS-Native/blob/fbc57dbe9e6832afed8d46cd369db6930bbd53bc/Native.xs

So, can you do what I want?

+3


source to share


1 answer


The fact that it's crashing is a pretty good sign that you can't :)



Create SV on a stream that actually has a Perl interpreter, i.e. when getting a resolved name.

+2


source







All Articles