#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
+/* Workaround for XSUB.h bug under WIN32 */
+#ifdef WIN32
+# undef setjmp
+# if !defined(__BORLANDC__)
+# define setjmp(x) _setjmp(x)
+# endif
+#endif
#ifdef HAS_PPPORT_H
+# define NEED_PL_signals
# define NEED_newRV_noinc
# define NEED_sv_2pv_nolen
# include "ppport.h"
#ifdef WIN32
# include <windows.h>
+ /* Supposed to be in Winbase.h */
+# ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
+# define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
+# endif
# include <win32thread.h>
#else
# ifdef OS2
# define PERL_THREAD_DETACH(t) pthread_detach((t))
# endif
#endif
+#if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
+# include <sys/param.h>
+#endif
/* Values for 'state' member */
#define PERL_ITHR_JOINABLE 0
#else
pthread_t thr; /* OS's handle for the thread */
#endif
+ IV stack_size;
} ithread;
static perl_mutex create_destruct_mutex;
static UV tid_counter = 0;
-static IV active_threads = 0;
+static IV joinable_threads = 0;
+static IV running_threads = 0;
+static IV detached_threads = 0;
+#ifdef THREAD_CREATE_NEEDS_STACK
+static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
+#else
+static IV default_stack_size = 0;
+#endif
+static IV page_size = 0;
/* Used by Perl interpreter for thread context switching */
{
PerlInterpreter *interp;
- assert(thread->state & PERL_ITHR_FINISHED &&
- thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
+ assert((thread->state & PERL_ITHR_FINISHED) &&
+ (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
interp = thread->interp;
if (interp) {
return;
}
- MUTEX_LOCK(&create_destruct_mutex);
/* Main thread (0) is immortal and should never get here */
assert(thread->tid != 0);
/* Remove from circular list of threads */
+ MUTEX_LOCK(&create_destruct_mutex);
thread->next->prev = thread->prev;
thread->prev->next = thread->next;
thread->next = NULL;
{
int veto_cleanup = 0;
MUTEX_LOCK(&create_destruct_mutex);
- if ((aTHX == PL_curinterp) && (active_threads != 1)) {
+ if ((aTHX == PL_curinterp) &&
+ (running_threads || joinable_threads))
+ {
if (ckWARN_d(WARN_THREADS)) {
- Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running", active_threads);
+ Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
+ IVdf " running and unjoined\n\t%"
+ IVdf " finished and unjoined\n\t%"
+ IVdf " running and detached\n",
+ running_threads,
+ joinable_threads,
+ detached_threads);
}
veto_cleanup = 1;
}
};
+/* Provided default, minimum and rational stack sizes */
+static IV
+good_stack_size(pTHX_ IV stack_size)
+{
+ /* Use default stack size if no stack size specified */
+ if (! stack_size)
+ return (default_stack_size);
+
+#ifdef PTHREAD_STACK_MIN
+ /* Can't use less than minimum */
+ if (stack_size < PTHREAD_STACK_MIN) {
+ if (ckWARN(WARN_THREADS)) {
+ Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
+ }
+ return (PTHREAD_STACK_MIN);
+ }
+#endif
+
+ /* Round up to page size boundary */
+ if (page_size <= 0) {
+#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
+ SETERRNO(0, SS_NORMAL);
+# ifdef _SC_PAGESIZE
+ page_size = sysconf(_SC_PAGESIZE);
+# else
+ page_size = sysconf(_SC_MMAP_PAGE_SIZE);
+# endif
+ if ((long)page_size < 0) {
+ if (errno) {
+ SV * const error = get_sv("@", FALSE);
+ (void)SvUPGRADE(error, SVt_PV);
+ Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
+ } else {
+ Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
+ }
+ }
+#else
+# ifdef HAS_GETPAGESIZE
+ page_size = getpagesize();
+# else
+# if defined(I_SYS_PARAM) && defined(PAGESIZE)
+ page_size = PAGESIZE;
+# else
+ page_size = 8192; /* A conservative default */
+# endif
+# endif
+ if (page_size <= 0)
+ Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
+#endif
+ }
+ stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
+
+ return (stack_size);
+}
+
+
/* Starts executing the thread.
* Passed as the C level function to run in the new thread.
*/
AV *params = (AV *)SvRV(thread->params);
int len = (int)av_len(params)+1;
int ii;
+ int jmp_rc = 0;
+ I32 oldscope;
+
+ dJMPENV;
dSP;
ENTER;
}
PUTBACK;
- /* Run the specified function */
- len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
+ oldscope = PL_scopestack_ix;
+ JMPENV_PUSH(jmp_rc);
+ if (jmp_rc == 0) {
+ /* Run the specified function */
+ len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
+ } else if (jmp_rc == 2) {
+ while (PL_scopestack_ix > oldscope) {
+ LEAVE;
+ }
+ }
+ JMPENV_POP;
/* Remove args from stack and put back in params array */
SPAGAIN;
for (ii=len-1; ii >= 0; ii--) {
SV *sv = POPs;
- av_store(params, ii, SvREFCNT_inc(sv));
+ if (jmp_rc == 0) {
+ av_store(params, ii, SvREFCNT_inc(sv));
+ }
}
+ FREETMPS;
+ LEAVE;
+
/* Check for failure */
if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
- Perl_warn(aTHX_ "Thread failed to start: %" SVf, ERRSV);
+ oldscope = PL_scopestack_ix;
+ JMPENV_PUSH(jmp_rc);
+ if (jmp_rc == 0) {
+ Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
+ } else if (jmp_rc == 2) {
+ while (PL_scopestack_ix > oldscope) {
+ LEAVE;
+ }
+ }
+ JMPENV_POP;
}
- FREETMPS;
- LEAVE;
-
/* Release function ref */
SvREFCNT_dec(thread->init_function);
thread->init_function = Nullsv;
cleanup = (thread->state & PERL_ITHR_DETACHED);
MUTEX_UNLOCK(&thread->mutex);
- if (cleanup)
+ if (cleanup) {
+ MUTEX_LOCK(&create_destruct_mutex);
+ detached_threads--;
+ MUTEX_UNLOCK(&create_destruct_mutex);
S_ithread_destruct(aTHX_ thread);
-
- MUTEX_LOCK(&create_destruct_mutex);
- active_threads--;
- MUTEX_UNLOCK(&create_destruct_mutex);
+ } else {
+ MUTEX_LOCK(&create_destruct_mutex);
+ running_threads--;
+ joinable_threads++;
+ MUTEX_UNLOCK(&create_destruct_mutex);
+ }
#ifdef WIN32
return ((DWORD)0);
pTHX_ SV *obj,
char *classname,
SV *init_function,
+ IV stack_size,
+ int gimme,
SV *params)
{
ithread *thread;
MUTEX_INIT(&thread->mutex);
thread->tid = tid_counter++;
- thread->gimme = GIMME_V;
+ thread->stack_size = good_stack_size(aTHX_ stack_size);
+ thread->gimme = gimme;
/* "Clone" our interpreter into the thread's interpreter.
* This gives thread access to "static data" and code.
*/
SvREFCNT_dec(PL_endav);
PL_endav = newAV();
- clone_param.flags = 0;
- thread->init_function = sv_dup(init_function, &clone_param);
- if (SvREFCNT(thread->init_function) == 0) {
- SvREFCNT_inc(thread->init_function);
+
+ if (SvPOK(init_function)) {
+ thread->init_function = newSV(0);
+ sv_copypv(thread->init_function, init_function);
+ } else {
+ clone_param.flags = 0;
+ thread->init_function = sv_dup(init_function, &clone_param);
+ if (SvREFCNT(thread->init_function) == 0) {
+ SvREFCNT_inc(thread->init_function);
+ }
}
thread->params = sv_dup(params, &clone_param);
/* Create/start the thread */
#ifdef WIN32
thread->handle = CreateThread(NULL,
- (DWORD)0,
+ (DWORD)thread->stack_size,
S_ithread_run,
(LPVOID)thread,
- 0,
+ STACK_SIZE_PARAM_IS_A_RESERVATION,
&thread->thr);
#else
{
PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
# endif
-# ifdef THREAD_CREATE_NEEDS_STACK
+# ifdef _POSIX_THREAD_ATTR_STACKSIZE
/* Set thread's stack size */
- rc_stack_size = pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK);
+ if (thread->stack_size > 0) {
+ rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
+ }
# endif
/* Create the thread */
(void *)thread);
# endif
}
+
+# ifdef _POSIX_THREAD_ATTR_STACKSIZE
+ /* Try to get thread's actual stack size */
+ {
+ size_t stacksize;
+#ifdef HPUX1020
+ stacksize = pthread_attr_getstacksize(attr);
+#else
+ if (! pthread_attr_getstacksize(&attr, &stacksize))
+#endif
+ if (stacksize > 0) {
+ thread->stack_size = (IV)stacksize;
+ }
+ }
+# endif
}
#endif
sv_2mortal(params);
S_ithread_destruct(aTHX_ thread);
#ifndef WIN32
- if (ckWARN_d(WARN_THREADS)) {
-# ifdef THREAD_CREATE_NEEDS_STACK
- if (rc_stack_size)
- Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", (IV)THREAD_CREATE_NEEDS_STACK, rc_stack_size);
- else
-# endif
- Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
- }
+ if (ckWARN_d(WARN_THREADS)) {
+ if (rc_stack_size)
+ Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
+ else
+ Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
+ }
#endif
return (&PL_sv_undef);
}
- active_threads++;
+ running_threads++;
MUTEX_UNLOCK(&create_destruct_mutex);
sv_2mortal(params);
ithread_create(...)
PREINIT:
char *classname;
+ ithread *thread;
SV *function_to_call;
AV *params;
+ HV *specs;
+ IV stack_size;
+ int context;
+ char *str;
+ int idx;
int ii;
CODE:
- if (items < 2)
- Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
+ if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
+ if (--items < 2)
+ Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
+ specs = (HV*)SvRV(ST(1));
+ idx = 1;
+ } else {
+ if (items < 2)
+ Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
+ specs = NULL;
+ idx = 0;
+ }
- classname = (char *)SvPV_nolen(ST(0));
- function_to_call = ST(1);
+ if (sv_isobject(ST(0))) {
+ /* $thr->create() */
+ classname = HvNAME(SvSTASH(SvRV(ST(0))));
+ thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
+ stack_size = thread->stack_size;
+ } else {
+ /* threads->create() */
+ classname = (char *)SvPV_nolen(ST(0));
+ stack_size = default_stack_size;
+ }
+
+ function_to_call = ST(idx+1);
+
+ context = -1;
+ if (specs) {
+ /* stack_size */
+ if (hv_exists(specs, "stack", 5)) {
+ stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
+ } else if (hv_exists(specs, "stacksize", 9)) {
+ stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
+ } else if (hv_exists(specs, "stack_size", 10)) {
+ stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
+ }
+
+ /* context */
+ if (hv_exists(specs, "context", 7)) {
+ str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
+ switch (*str) {
+ case 'a':
+ case 'A':
+ context = G_ARRAY;
+ break;
+ case 's':
+ case 'S':
+ context = G_SCALAR;
+ break;
+ case 'v':
+ case 'V':
+ context = G_VOID;
+ break;
+ default:
+ Perl_croak(aTHX_ "Invalid context: %s", str);
+ }
+ } else if (hv_exists(specs, "array", 5)) {
+ if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
+ context = G_ARRAY;
+ }
+ } else if (hv_exists(specs, "scalar", 6)) {
+ if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
+ context = G_SCALAR;
+ }
+ } else if (hv_exists(specs, "void", 4)) {
+ if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
+ context = G_VOID;
+ }
+ }
+ }
+ if (context == -1) {
+ context = GIMME_V; /* Implicit context */
+ } else {
+ context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
+ }
/* Function args */
params = newAV();
if (items > 2) {
- for (ii=2; ii < items; ii++) {
- av_push(params, SvREFCNT_inc(ST(ii)));
+ for (ii=2; ii < items ; ii++) {
+ av_push(params, SvREFCNT_inc(ST(idx+ii)));
}
}
/* Create thread */
ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
- classname,
- function_to_call,
- newRV_noinc((SV*)params)));
+ classname,
+ function_to_call,
+ stack_size,
+ context,
+ newRV_noinc((SV*)params)));
/* XSRETURN(1); - implied */
ithread *thread;
int list_context;
IV count = 0;
+ int want_running;
PPCODE:
/* Class method only */
if (SvROK(ST(0)))
- Perl_croak(aTHX_ "Usage: threads->list()");
+ Perl_croak(aTHX_ "Usage: threads->list(...)");
classname = (char *)SvPV_nolen(ST(0));
/* Calling context */
list_context = (GIMME_V == G_ARRAY);
+ /* Running or joinable parameter */
+ if (items > 1) {
+ want_running = SvTRUE(ST(1));
+ }
+
/* Walk through threads list */
MUTEX_LOCK(&create_destruct_mutex);
for (thread = threads->next;
if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
continue;
}
+
+ /* Filter per parameter */
+ if (items > 1) {
+ if (want_running) {
+ if (thread->state & PERL_ITHR_FINISHED) {
+ continue; /* Not running */
+ }
+ } else {
+ if (! (thread->state & PERL_ITHR_FINISHED)) {
+ continue; /* Still running - not joinable yet */
+ }
+ }
+ }
+
/* Push object on stack if list context */
if (list_context) {
XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
S_ithread_clear(aTHX_ thread);
MUTEX_UNLOCK(&thread->mutex);
+ MUTEX_LOCK(&create_destruct_mutex);
+ joinable_threads--;
+ MUTEX_UNLOCK(&create_destruct_mutex);
+
/* If no return values, then just return */
if (! params) {
XSRETURN_UNDEF;
cleanup = (thread->state & PERL_ITHR_FINISHED);
MUTEX_UNLOCK(&thread->mutex);
- if (cleanup)
+ MUTEX_LOCK(&create_destruct_mutex);
+ if (cleanup) {
+ joinable_threads--;
+ } else {
+ running_threads--;
+ detached_threads++;
+ }
+ MUTEX_UNLOCK(&create_destruct_mutex);
+
+ if (cleanup) {
S_ithread_destruct(aTHX_ thread);
+ }
+
+
+void
+ithread_kill(...)
+ PREINIT:
+ ithread *thread;
+ char *sig_name;
+ IV signal;
+ CODE:
+ /* Must have safe signals */
+ if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
+ Perl_croak(aTHX_ "Cannot signal threads without safe signals");
+
+ /* Object method only */
+ if (! sv_isobject(ST(0)))
+ Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
+
+ /* Get signal */
+ sig_name = SvPV_nolen(ST(1));
+ if (isALPHA(*sig_name)) {
+ if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
+ sig_name += 3;
+ if ((signal = whichsig(sig_name)) < 0)
+ Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
+ } else
+ signal = SvIV(ST(1));
+
+ /* Set the signal for the thread */
+ thread = SV_to_ithread(aTHX_ ST(0));
+ MUTEX_LOCK(&thread->mutex);
+ if (thread->interp) {
+ dTHXa(thread->interp);
+ PL_psig_pend[signal]++;
+ PL_sig_pending = 1;
+ }
+ MUTEX_UNLOCK(&thread->mutex);
+
+ /* Return the thread to allow for method chaining */
+ ST(0) = ST(0);
+ /* XSRETURN(1); - implied */
void
#endif
/* XSRETURN(1); - implied */
+
+void
+ithread_get_stack_size(...)
+ PREINIT:
+ IV stack_size;
+ CODE:
+ if (sv_isobject(ST(0))) {
+ /* $thr->get_stack_size() */
+ ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
+ stack_size = thread->stack_size;
+ } else {
+ /* threads->get_stack_size() */
+ stack_size = default_stack_size;
+ }
+ XST_mIV(0, stack_size);
+ /* XSRETURN(1); - implied */
+
+
+void
+ithread_set_stack_size(...)
+ PREINIT:
+ IV old_size;
+ CODE:
+ if (items != 2)
+ Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
+ if (sv_isobject(ST(0)))
+ Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
+
+ old_size = default_stack_size;
+ default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
+ XST_mIV(0, old_size);
+ /* XSRETURN(1); - implied */
+
+
+void
+ithread_is_running(...)
+ PREINIT:
+ ithread *thread;
+ CODE:
+ /* Object method only */
+ if (! sv_isobject(ST(0)))
+ Perl_croak(aTHX_ "Usage: $thr->is_running()");
+
+ thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
+ MUTEX_LOCK(&thread->mutex);
+ ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
+ MUTEX_UNLOCK(&thread->mutex);
+ /* XSRETURN(1); - implied */
+
+
+void
+ithread_is_detached(...)
+ PREINIT:
+ ithread *thread;
+ CODE:
+ thread = SV_to_ithread(aTHX_ ST(0));
+ MUTEX_LOCK(&thread->mutex);
+ ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
+ MUTEX_UNLOCK(&thread->mutex);
+ /* XSRETURN(1); - implied */
+
+
+void
+ithread_is_joinable(...)
+ PREINIT:
+ ithread *thread;
+ CODE:
+ /* Object method only */
+ if (! sv_isobject(ST(0)))
+ Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
+
+ thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
+ MUTEX_LOCK(&thread->mutex);
+ ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
+ ! (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
+ ? &PL_sv_yes : &PL_sv_no;
+ MUTEX_UNLOCK(&thread->mutex);
+ /* XSRETURN(1); - implied */
+
+
+void
+ithread_wantarray(...)
+ PREINIT:
+ ithread *thread;
+ CODE:
+ thread = SV_to_ithread(aTHX_ ST(0));
+ MUTEX_LOCK(&thread->mutex);
+ ST(0) = (thread->gimme & G_ARRAY) ? &PL_sv_yes :
+ (thread->gimme & G_VOID) ? &PL_sv_undef
+ /* G_SCALAR */ : &PL_sv_no;
+ MUTEX_UNLOCK(&thread->mutex);
+ /* XSRETURN(1); - implied */
+
#endif /* USE_ITHREADS */
}
Zero(thread, 1, ithread);
- PL_perl_destruct_level = 2;
MUTEX_INIT(&thread->mutex);
thread->tid = tid_counter++; /* Thread 0 */
thread->interp = aTHX;
thread->state = PERL_ITHR_DETACHED; /* Detached */
+ thread->stack_size = default_stack_size;
# ifdef WIN32
thread->thr = GetCurrentThreadId();
# else
thread->thr = pthread_self();
# endif
- active_threads++;
-
S_ithread_set(aTHX_ thread);
MUTEX_UNLOCK(&create_destruct_mutex);
#endif /* USE_ITHREADS */