typedef struct thread *Thread;
#define THREAD_RET_TYPE void *
#define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
-#endif;
+#endif
static void
remove_thread(struct thread *t)
dSP;
I32 oldscope = scopestack_ix;
I32 retval;
- AV *returnav;
+ AV *av;
int i;
DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
I32 oldmark = TOPMARK;
I32 oldscope = scopestack_ix;
I32 retval;
- AV *returnav;
+ SV *sv;
+ AV *av = newAV();
int i, ret;
dJMPENV;
DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
thr, SvPEEK(TOPs)));
+#ifdef OLD_WAY
JMPENV_PUSH(ret);
switch (ret) {
case 3:
while (scopestack_ix > oldscope)
LEAVE;
JMPENV_POP;
- av_store(returnav, 0, newSViv(statusvalue));
+ MUTEX_LOCK(&thr->mutex);
+ thr->flags |= THRf_DID_DIE;
+ MUTEX_UNLOCK(&thr->mutex);
+ av = newSVpvf("Thread called exit with value %d", statusvalue);
goto finishoff;
}
op = pp_entersub(ARGS);
if (op)
runops();
+#else
+ sv = POPs;
+ PUTBACK;
+ perl_call_sv(sv, G_ARRAY|G_EVAL);
+#endif
SPAGAIN;
retval = sp - (stack_base + oldmark);
sp = stack_base + oldmark + 1;
- DEBUG_L(for (i = 1; i <= retval; i++)
- PerlIO_printf(PerlIO_stderr(),
- "%p returnav[%d] = %s\n",
- thr, i, SvPEEK(sp[i - 1]));)
- returnav = newAV();
- av_store(returnav, 0, newSVpv("", 0));
- for (i = 1; i <= retval; i++, sp++)
- sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
-
+ if (SvCUR(thr->errsv)) {
+ MUTEX_LOCK(&thr->mutex);
+ thr->flags |= THRf_DID_DIE;
+ MUTEX_UNLOCK(&thr->mutex);
+ av_store(av, 0, &sv_no);
+ av_store(av, 1, newSVsv(thr->errsv));
+ DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
+ SvPV(thr->errsv, na));
+ } else {
+ DEBUG_L(STMT_START {
+ for (i = 1; i <= retval; i++) {
+ PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
+ thr, i, SvPEEK(sp[i - 1]));)
+ }
+ } STMT_END);
+ av_store(av, 0, &sv_yes);
+ for (i = 1; i <= retval; i++, sp++)
+ sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*sp));
+ }
+
finishoff:
#if 0
/* removed for debug */
case THRf_R_DETACHED:
ThrSETSTATE(thr, THRf_DEAD);
MUTEX_UNLOCK(&thr->mutex);
- SvREFCNT_dec(returnav);
+ SvREFCNT_dec(av);
DEBUG_L(PerlIO_printf(PerlIO_stderr(),
"%p: DETACHED thread finished\n", thr));
remove_thread(thr); /* This might trigger main thread to finish */
croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
/* NOTREACHED */
}
- return THREAD_RET_CAST(returnav); /* Available for anyone to join with */
+ return THREAD_RET_CAST(av); /* Available for anyone to join with */
/* us unless we're detached, in which */
/* case noone sees the value anyway. */
#endif
}
static SV *
-newthread (SV *startsv, AV *initargs, char *Class)
+newthread (SV *startsv, AV *initargs, char *classname)
{
#ifdef USE_THREADS
dSP;
sv = newSViv(thr->tid);
sv_magic(sv, thr->oursv, '~', 0, 0);
SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
- return sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE));
+ return sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
#else
croak("No threads in this perl");
return &sv_undef;
PROTOTYPES: DISABLE
void
-new(Class, startsv, ...)
- char * Class
+new(classname, startsv, ...)
+ char * classname
SV * startsv
AV * av = av_make(items - 2, &ST(2));
PPCODE:
- XPUSHs(sv_2mortal(newthread(startsv, av, Class)));
+ XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
void
join(t)
}
JOIN(t, &av);
- /* Could easily speed up the following if necessary */
- for (i = 0; i <= AvFILL(av); i++)
- XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
+ if (SvTRUE(*av_fetch(av, 0, FALSE))) {
+ /* Could easily speed up the following if necessary */
+ for (i = 1; i <= AvFILL(av); i++)
+ XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
+ } else {
+ char *mess = SvPV(*av_fetch(av, 1, FALSE), na);
+ DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+ "%p: join propagating die message: %s\n",
+ thr, mess));
+ croak(mess);
+ }
#endif
void
#endif
void
-self(Class)
- char * Class
+self(classname)
+ char * classname
PREINIT:
SV *sv;
PPCODE:
sv = newSViv(thr->tid);
sv_magic(sv, thr->oursv, '~', 0, 0);
SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
- PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE))));
+ PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
+ gv_stashpv(classname, TRUE))));
#endif
U32
#endif
void
-list(Class)
- char * Class
+list(classname)
+ char * classname
PREINIT:
Thread t;
AV * av;
SV *sv = newSViv(0); /* fill in tid later */
sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
av_push(av, sv_bless(newRV_noinc(sv),
- gv_stashpv(Class, TRUE)));
+ gv_stashpv(classname, TRUE)));
}
}
OUTPUT:
RETVAL
+MODULE = Thread PACKAGE = Thread::Specific
+
+void
+data(classname = "Thread::Specific")
+ char * classname
+ PPCODE:
+ if (AvFILL(thr->specific) == -1) {
+ GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
+ av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
+ }
+ XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));