-#ifdef FAKE_THREADS
- Thread savethread = thr;
- LOGOP myop;
- dSP;
- I32 oldscope = scopestack_ix;
- I32 retval;
- AV *returnav;
- int i;
-
- DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
- thr, SvPEEK(TOPs)));
- thr = (Thread) arg;
- savemark = TOPMARK;
- thr->prev = thr->prev_run = savethread;
- thr->next = savethread->next;
- thr->next_run = savethread->next_run;
- savethread->next = savethread->next_run = thr;
- thr->wait_queue = 0;
- thr->private = 0;
-
- /* Now duplicate most of perl_call_sv but with a few twists */
- op = (OP*)&myop;
- Zero(op, 1, LOGOP);
- myop.op_flags = OPf_STACKED;
- myop.op_next = Nullop;
- myop.op_flags |= OPf_KNOW;
- myop.op_flags |= OPf_WANT_LIST;
- op = pp_entersub(ARGS);
- DEBUG_L(if (!op)
- PerlIO_printf(PerlIO_stderr(), "thread starts at Nullop\n"));
- /*
- * When this thread is next scheduled, we start in the right
- * place. When the thread runs off the end of the sub, perl.c
- * handles things, using savemark to figure out how much of the
- * stack is the return value for any join.
- */
- thr = savethread; /* back to the old thread */
- return 0;
-#else
- Thread thr = (Thread) arg;
- LOGOP myop;
- dSP;
- I32 oldmark = TOPMARK;
- I32 oldscope = scopestack_ix;
- I32 retval;
- AV *returnav;
- int i, ret;
- dJMPENV;
-
- /* Don't call *anything* requiring dTHR until after pthread_setspecific */
- /*
- * Wait until our creator releases us. If we didn't do this, then
- * it would be potentially possible for out thread to carry on and
- * do stuff before our creator fills in our "self" field. For example,
- * if we went and created another thread which tried to JOIN with us,
- * then we'd be in a mess.
- */
- MUTEX_LOCK(&thr->mutex);
- MUTEX_UNLOCK(&thr->mutex);
-
- /*
- * It's safe to wait until now to set the thread-specific pointer
- * from our pthread_t structure to our struct thread, since we're
- * the only thread who can get at it anyway.
- */
- SET_THR(thr);
-
- /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
- DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
- thr, SvPEEK(TOPs)));
-
- JMPENV_PUSH(ret);
- switch (ret) {
- case 3:
- PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
- /* fall through */
- case 1:
- STATUS_ALL_FAILURE;
- /* fall through */
- case 2:
- /* my_exit() was called */
- while (scopestack_ix > oldscope)
- LEAVE;
- JMPENV_POP;
- av_store(returnav, 0, newSViv(statusvalue));
- goto finishoff;
- }
-
- CATCH_SET(TRUE);
-
- /* Now duplicate most of perl_call_sv but with a few twists */
- op = (OP*)&myop;
- Zero(op, 1, LOGOP);
- myop.op_flags = OPf_STACKED;
- myop.op_next = Nullop;
- myop.op_flags |= OPf_KNOW;
- myop.op_flags |= OPf_WANT_LIST;
- op = pp_entersub(ARGS);
- if (op)
- runops();
- 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));
-
- finishoff:
-#if 0
- /* removed for debug */
- SvREFCNT_dec(curstack);
-#endif
- SvREFCNT_dec(thr->cvcache);
- SvREFCNT_dec(thr->magicals);
- SvREFCNT_dec(thr->specific);
- SvREFCNT_dec(thr->errsv);
- SvREFCNT_dec(thr->errhv);
- Safefree(markstack);
- Safefree(scopestack);
- Safefree(savestack);
- Safefree(retstack);
- Safefree(cxstack);
- Safefree(tmps_stack);
- Safefree(ofs);
-
- MUTEX_LOCK(&thr->mutex);
- DEBUG_L(PerlIO_printf(PerlIO_stderr(),
- "%p: threadstart finishing: state is %u\n",
- thr, ThrSTATE(thr)));
- switch (ThrSTATE(thr)) {
- case THRf_R_JOINABLE:
- ThrSETSTATE(thr, THRf_ZOMBIE);
- MUTEX_UNLOCK(&thr->mutex);
- DEBUG_L(PerlIO_printf(PerlIO_stderr(),
- "%p: R_JOINABLE thread finished\n", thr));
- break;
- case THRf_R_JOINED:
- ThrSETSTATE(thr, THRf_DEAD);
- MUTEX_UNLOCK(&thr->mutex);
- remove_thread(thr);
- DEBUG_L(PerlIO_printf(PerlIO_stderr(),
- "%p: R_JOINED thread finished\n", thr));
- break;
- case THRf_R_DETACHED:
- ThrSETSTATE(thr, THRf_DEAD);
- MUTEX_UNLOCK(&thr->mutex);
- SvREFCNT_dec(returnav);
- DEBUG_L(PerlIO_printf(PerlIO_stderr(),
- "%p: DETACHED thread finished\n", thr));
- remove_thread(thr); /* This might trigger main thread to finish */
- break;
- default:
- MUTEX_UNLOCK(&thr->mutex);
- croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
- /* NOTREACHED */
- }
- return THREAD_RET_CAST(returnav); /* Available for anyone to join with */
- /* us unless we're detached, in which */
- /* case noone sees the value anyway. */
-#endif