Bump version and other misc. changes. 3rd patch from:
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 /* Workaround for XSUB.h bug under WIN32 */
6 #ifdef WIN32
7 #  undef setjmp
8 #  if !defined(__BORLANDC__)
9 #    define setjmp(x) _setjmp(x)
10 #  endif
11 #endif
12 #ifdef HAS_PPPORT_H
13 #  define NEED_PL_signals
14 #  define NEED_newRV_noinc
15 #  define NEED_sv_2pv_nolen
16 #  include "ppport.h"
17 #  include "threads.h"
18 #endif
19
20 #ifdef USE_ITHREADS
21
22 #ifdef WIN32
23 #  include <windows.h>
24    /* Supposed to be in Winbase.h */
25 #  ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
26 #    define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
27 #  endif
28 #  include <win32thread.h>
29 #else
30 #  ifdef OS2
31 typedef perl_os_thread pthread_t;
32 #  else
33 #    include <pthread.h>
34 #  endif
35 #  include <thread.h>
36 #  define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
37 #  ifdef OLD_PTHREADS_API
38 #    define PERL_THREAD_DETACH(t) pthread_detach(&(t))
39 #  else
40 #    define PERL_THREAD_DETACH(t) pthread_detach((t))
41 #  endif
42 #endif
43 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
44 #  include <sys/param.h>
45 #endif
46
47 /* Values for 'state' member */
48 #define PERL_ITHR_JOINABLE              0
49 #define PERL_ITHR_DETACHED              1
50 #define PERL_ITHR_JOINED                2
51 #define PERL_ITHR_FINISHED              4
52 #define PERL_ITHR_THREAD_EXIT_ONLY      8
53
54 typedef struct _ithread {
55     struct _ithread *next;      /* Next thread in the list */
56     struct _ithread *prev;      /* Prev thread in the list */
57     PerlInterpreter *interp;    /* The threads interpreter */
58     UV tid;                     /* Threads module's thread id */
59     perl_mutex mutex;           /* Mutex for updating things in this struct */
60     int count;                  /* How many SVs have a reference to us */
61     int state;                  /* Detached, joined, finished, etc. */
62     int gimme;                  /* Context of create */
63     SV *init_function;          /* Code to run */
64     SV *params;                 /* Args to pass function */
65 #ifdef WIN32
66     DWORD  thr;                 /* OS's idea if thread id */
67     HANDLE handle;              /* OS's waitable handle */
68 #else
69     pthread_t thr;              /* OS's handle for the thread */
70 #endif
71     IV stack_size;
72 } ithread;
73
74
75 /* Used by Perl interpreter for thread context switching */
76 #define MY_CXT_KEY "threads::_guts" XS_VERSION
77
78 typedef struct {
79     ithread *thread;
80 } my_cxt_t;
81
82 START_MY_CXT
83
84 /* Structure for 'main' thread
85  * Also forms the 'base' for the doubly-linked list of threads */
86 static ithread main_thread;
87
88 /* Protects the creation and destruction of threads*/
89 static perl_mutex create_destruct_mutex;
90
91 static UV tid_counter = 1;
92 static IV joinable_threads = 0;
93 static IV running_threads = 0;
94 static IV detached_threads = 0;
95 #ifdef THREAD_CREATE_NEEDS_STACK
96 static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
97 #else
98 static IV default_stack_size = 0;
99 #endif
100 static IV page_size = 0;
101
102
103 /* Used by Perl interpreter for thread context switching */
104 static void
105 S_ithread_set(pTHX_ ithread *thread)
106 {
107     dMY_CXT;
108     MY_CXT.thread = thread;
109 }
110
111 static ithread *
112 S_ithread_get(pTHX)
113 {
114     dMY_CXT;
115     return (MY_CXT.thread);
116 }
117
118
119 /* Free any data (such as the Perl interpreter) attached to an ithread
120  * structure.  This is a bit like undef on SVs, where the SV isn't freed,
121  * but the PVX is.  Must be called with thread->mutex already held.
122  */
123 static void
124 S_ithread_clear(pTHX_ ithread *thread)
125 {
126     PerlInterpreter *interp;
127
128     assert((thread->state & PERL_ITHR_FINISHED) &&
129            (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
130
131     interp = thread->interp;
132     if (interp) {
133         dTHXa(interp);
134
135         PERL_SET_CONTEXT(interp);
136         S_ithread_set(aTHX_ thread);
137
138         SvREFCNT_dec(thread->params);
139         thread->params = Nullsv;
140
141         perl_destruct(interp);
142         perl_free(interp);
143         thread->interp = NULL;
144     }
145
146     PERL_SET_CONTEXT(aTHX);
147 }
148
149
150 /* Free an ithread structure and any attached data if its count == 0 */
151 static void
152 S_ithread_destruct(pTHX_ ithread *thread)
153 {
154 #ifdef WIN32
155     HANDLE handle;
156 #endif
157     /* Return if thread is still being used */
158     if (thread->count != 0) {
159         return;
160     }
161
162     /* Main thread (0) is immortal and should never get here */
163     assert(thread->tid != 0);
164
165     /* Remove from circular list of threads */
166     MUTEX_LOCK(&create_destruct_mutex);
167     thread->next->prev = thread->prev;
168     thread->prev->next = thread->next;
169     thread->next = NULL;
170     thread->prev = NULL;
171     MUTEX_UNLOCK(&create_destruct_mutex);
172
173     /* Thread is now disowned */
174     MUTEX_LOCK(&thread->mutex);
175     S_ithread_clear(aTHX_ thread);
176
177 #ifdef WIN32
178     handle = thread->handle;
179     thread->handle = NULL;
180 #endif
181     MUTEX_UNLOCK(&thread->mutex);
182     MUTEX_DESTROY(&thread->mutex);
183
184 #ifdef WIN32
185     if (handle) {
186         CloseHandle(handle);
187     }
188 #endif
189
190     /* Call PerlMemShared_free() in the context of the "first" interpreter
191      * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
192      */
193     aTHX = PL_curinterp;
194     PerlMemShared_free(thread);
195 }
196
197
198 /* Warn if exiting with any unjoined threads */
199 static int
200 S_exit_warning(pTHX)
201 {
202     int veto_cleanup;
203
204     MUTEX_LOCK(&create_destruct_mutex);
205     veto_cleanup = (running_threads || joinable_threads);
206     MUTEX_UNLOCK(&create_destruct_mutex);
207
208     if (veto_cleanup) {
209         if (ckWARN_d(WARN_THREADS)) {
210             Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
211                             IVdf " running and unjoined\n\t%"
212                             IVdf " finished and unjoined\n\t%"
213                             IVdf " running and detached\n",
214                             running_threads,
215                             joinable_threads,
216                             detached_threads);
217         }
218     }
219
220     return (veto_cleanup);
221 }
222
223 /* Called on exit from main thread */
224 int
225 Perl_ithread_hook(pTHX)
226 {
227     return ((aTHX == PL_curinterp) ? S_exit_warning(aTHX) : 0);
228 }
229
230
231 /* MAGIC (in mg.h sense) hooks */
232
233 int
234 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
235 {
236     ithread *thread = (ithread *)mg->mg_ptr;
237     SvIV_set(sv, PTR2IV(thread));
238     SvIOK_on(sv);
239     return (0);
240 }
241
242 int
243 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
244 {
245     ithread *thread = (ithread *)mg->mg_ptr;
246     int cleanup;
247
248     MUTEX_LOCK(&thread->mutex);
249     cleanup = ((--thread->count == 0) &&
250                (thread->state & PERL_ITHR_FINISHED) &&
251                (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
252     MUTEX_UNLOCK(&thread->mutex);
253
254     if (cleanup) {
255         S_ithread_destruct(aTHX_ thread);
256     }
257     return (0);
258 }
259
260 int
261 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
262 {
263     ithread *thread = (ithread *)mg->mg_ptr;
264     MUTEX_LOCK(&thread->mutex);
265     thread->count++;
266     MUTEX_UNLOCK(&thread->mutex);
267     return (0);
268 }
269
270 MGVTBL ithread_vtbl = {
271     ithread_mg_get,     /* get */
272     0,                  /* set */
273     0,                  /* len */
274     0,                  /* clear */
275     ithread_mg_free,    /* free */
276     0,                  /* copy */
277     ithread_mg_dup      /* dup */
278 };
279
280
281 /* Provided default, minimum and rational stack sizes */
282 static IV
283 good_stack_size(pTHX_ IV stack_size)
284 {
285     /* Use default stack size if no stack size specified */
286     if (! stack_size) {
287         return (default_stack_size);
288     }
289
290 #ifdef PTHREAD_STACK_MIN
291     /* Can't use less than minimum */
292     if (stack_size < PTHREAD_STACK_MIN) {
293         if (ckWARN(WARN_THREADS)) {
294             Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
295         }
296         return (PTHREAD_STACK_MIN);
297     }
298 #endif
299
300     /* Round up to page size boundary */
301     if (page_size <= 0) {
302 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
303         SETERRNO(0, SS_NORMAL);
304 #  ifdef _SC_PAGESIZE
305         page_size = sysconf(_SC_PAGESIZE);
306 #  else
307         page_size = sysconf(_SC_MMAP_PAGE_SIZE);
308 #  endif
309         if ((long)page_size < 0) {
310             if (errno) {
311                 SV * const error = get_sv("@", FALSE);
312                 (void)SvUPGRADE(error, SVt_PV);
313                 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
314             } else {
315                 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
316             }
317         }
318 #else
319 #  ifdef HAS_GETPAGESIZE
320         page_size = getpagesize();
321 #  else
322 #    if defined(I_SYS_PARAM) && defined(PAGESIZE)
323         page_size = PAGESIZE;
324 #    else
325         page_size = 8192;   /* A conservative default */
326 #    endif
327 #  endif
328         if (page_size <= 0) {
329             Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
330         }
331 #endif
332     }
333     stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
334
335     return (stack_size);
336 }
337
338
339 /* Starts executing the thread.
340  * Passed as the C level function to run in the new thread.
341  */
342 #ifdef WIN32
343 static THREAD_RET_TYPE
344 S_ithread_run(LPVOID arg)
345 #else
346 static void *
347 S_ithread_run(void * arg)
348 #endif
349 {
350     ithread *thread = (ithread *)arg;
351     int jmp_rc = 0;
352     I32 oldscope;
353     int exit_app = 0;
354     int exit_code = 0;
355     int cleanup;
356
357     dJMPENV;
358
359     dTHXa(thread->interp);
360
361     /* Blocked until ->create() call finishes */
362     MUTEX_LOCK(&thread->mutex);
363     MUTEX_UNLOCK(&thread->mutex);
364
365     PERL_SET_CONTEXT(thread->interp);
366     S_ithread_set(aTHX_ thread);
367
368     PL_perl_destruct_level = 2;
369
370     {
371         AV *params = (AV *)SvRV(thread->params);
372         int len = (int)av_len(params)+1;
373         int ii;
374
375         dSP;
376         ENTER;
377         SAVETMPS;
378
379         /* Put args on the stack */
380         PUSHMARK(SP);
381         for (ii=0; ii < len; ii++) {
382             XPUSHs(av_shift(params));
383         }
384         PUTBACK;
385
386         oldscope = PL_scopestack_ix;
387         JMPENV_PUSH(jmp_rc);
388         if (jmp_rc == 0) {
389             /* Run the specified function */
390             len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
391         } else if (jmp_rc == 2) {
392             /* Thread exited */
393             exit_app = 1;
394             exit_code = STATUS_CURRENT;
395             while (PL_scopestack_ix > oldscope) {
396                 LEAVE;
397             }
398         }
399         JMPENV_POP;
400
401         /* Remove args from stack and put back in params array */
402         SPAGAIN;
403         for (ii=len-1; ii >= 0; ii--) {
404             SV *sv = POPs;
405             if (jmp_rc == 0) {
406                 av_store(params, ii, SvREFCNT_inc(sv));
407             }
408         }
409
410         FREETMPS;
411         LEAVE;
412
413         /* Check for failure */
414         if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
415             oldscope = PL_scopestack_ix;
416             JMPENV_PUSH(jmp_rc);
417             if (jmp_rc == 0) {
418                 /* Warn that thread died */
419                 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
420             } else if (jmp_rc == 2) {
421                 /* Warn handler exited */
422                 exit_app = 1;
423                 exit_code = STATUS_CURRENT;
424                 while (PL_scopestack_ix > oldscope) {
425                     LEAVE;
426                 }
427             }
428             JMPENV_POP;
429         }
430
431         /* Release function ref */
432         SvREFCNT_dec(thread->init_function);
433         thread->init_function = Nullsv;
434     }
435
436     PerlIO_flush((PerlIO *)NULL);
437
438     MUTEX_LOCK(&create_destruct_mutex);
439     MUTEX_LOCK(&thread->mutex);
440     /* Mark as finished */
441     thread->state |= PERL_ITHR_FINISHED;
442     /* Clear exit flag if required */
443     if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
444         exit_app = 0;
445     }
446     /* Cleanup if detached */
447     cleanup = (thread->state & PERL_ITHR_DETACHED);
448     MUTEX_UNLOCK(&thread->mutex);
449
450     /* Adjust thread status counts */
451     if (cleanup) {
452         detached_threads--;
453     } else {
454         running_threads--;
455         joinable_threads++;
456     }
457     MUTEX_UNLOCK(&create_destruct_mutex);
458
459     /* Exit application if required */
460     if (exit_app) {
461         oldscope = PL_scopestack_ix;
462         JMPENV_PUSH(jmp_rc);
463         if (jmp_rc == 0) {
464             /* Warn if there are unjoined threads */
465             S_exit_warning(aTHX);
466         } else if (jmp_rc == 2) {
467             /* Warn handler exited */
468             exit_code = STATUS_CURRENT;
469             while (PL_scopestack_ix > oldscope) {
470                 LEAVE;
471             }
472         }
473         JMPENV_POP;
474
475         my_exit(exit_code);
476     }
477
478     /* Clean up detached thread */
479     if (cleanup) {
480         S_ithread_destruct(aTHX_ thread);
481     }
482
483 #ifdef WIN32
484     return ((DWORD)0);
485 #else
486     return (0);
487 #endif
488 }
489
490
491 /* Type conversion helper functions */
492
493 static SV *
494 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
495 {
496     SV *sv;
497     MAGIC *mg;
498
499     /* If incrementing thread ref count, then call within mutex lock */
500     if (inc) {
501         MUTEX_LOCK(&thread->mutex);
502         thread->count++;
503         MUTEX_UNLOCK(&thread->mutex);
504     }
505
506     if (! obj) {
507         obj = newSV(0);
508     }
509
510     sv = newSVrv(obj, classname);
511     sv_setiv(sv, PTR2IV(thread));
512     mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
513     mg->mg_flags |= MGf_DUP;
514     SvREADONLY_on(sv);
515
516     return (obj);
517 }
518
519 static ithread *
520 SV_to_ithread(pTHX_ SV *sv)
521 {
522     /* Argument is a thread */
523     if (SvROK(sv)) {
524       return (INT2PTR(ithread *, SvIV(SvRV(sv))));
525     }
526     /* Argument is classname, therefore return current thread */
527     return (S_ithread_get(aTHX));
528 }
529
530
531 /* threads->create()
532  * Called in context of parent thread.
533  * Called with create_destruct_mutex locked.  (Unlocked on error.)
534  */
535 static ithread *
536 S_ithread_create(
537         pTHX_ SV *init_function,
538         IV        stack_size,
539         int       gimme,
540         int       exit_opt,
541         SV       *params)
542 {
543     ithread     *thread;
544     CLONE_PARAMS clone_param;
545     ithread     *current_thread = S_ithread_get(aTHX);
546
547     SV         **tmps_tmp = PL_tmps_stack;
548     IV           tmps_ix  = PL_tmps_ix;
549 #ifndef WIN32
550     int          rc_stack_size = 0;
551     int          rc_thread_create = 0;
552 #endif
553
554     /* Allocate thread structure */
555     thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
556     if (!thread) {
557         MUTEX_UNLOCK(&create_destruct_mutex);
558         PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
559         my_exit(1);
560     }
561     Zero(thread, 1, ithread);
562
563     /* Add to threads list */
564     thread->next = &main_thread;
565     thread->prev = main_thread.prev;
566     main_thread.prev = thread;
567     thread->prev->next = thread;
568
569     /* Set count to 1 immediately in case thread exits before
570      * we return to caller!
571      */
572     thread->count = 1;
573
574     /* Block new thread until ->create() call finishes */
575     MUTEX_INIT(&thread->mutex);
576     MUTEX_LOCK(&thread->mutex);
577
578     thread->tid = tid_counter++;
579     thread->stack_size = good_stack_size(aTHX_ stack_size);
580     thread->gimme = gimme;
581     thread->state = exit_opt;
582
583     /* "Clone" our interpreter into the thread's interpreter.
584      * This gives thread access to "static data" and code.
585      */
586     PerlIO_flush((PerlIO *)NULL);
587     S_ithread_set(aTHX_ thread);
588
589     SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
590     PL_srand_called = FALSE;   /* Set it to false so we can detect if it gets
591                                   set during the clone */
592
593 #ifdef WIN32
594     thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
595 #else
596     thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
597 #endif
598
599     /* perl_clone() leaves us in new interpreter's context.  As it is tricky
600      * to spot an implicit aTHX, create a new scope with aTHX matching the
601      * context for the duration of our work for new interpreter.
602      */
603     {
604         dTHXa(thread->interp);
605
606         MY_CXT_CLONE;
607
608         /* Here we remove END blocks since they should only run in the thread
609          * they are created
610          */
611         SvREFCNT_dec(PL_endav);
612         PL_endav = newAV();
613
614         if (SvPOK(init_function)) {
615             thread->init_function = newSV(0);
616             sv_copypv(thread->init_function, init_function);
617         } else {
618             clone_param.flags = 0;
619             thread->init_function = sv_dup(init_function, &clone_param);
620             if (SvREFCNT(thread->init_function) == 0) {
621                 SvREFCNT_inc_void(thread->init_function);
622             }
623         }
624
625         thread->params = sv_dup(params, &clone_param);
626         SvREFCNT_inc_void(thread->params);
627
628         /* The code below checks that anything living on the tmps stack and
629          * has been cloned (so it lives in the ptr_table) has a refcount
630          * higher than 0.
631          *
632          * If the refcount is 0 it means that a something on the stack/context
633          * was holding a reference to it and since we init_stacks() in
634          * perl_clone that won't get cleaned and we will get a leaked scalar.
635          * The reason it was cloned was that it lived on the @_ stack.
636          *
637          * Example of this can be found in bugreport 15837 where calls in the
638          * parameter list end up as a temp.
639          *
640          * One could argue that this fix should be in perl_clone.
641          */
642         while (tmps_ix > 0) {
643             SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
644             tmps_ix--;
645             if (sv && SvREFCNT(sv) == 0) {
646                 SvREFCNT_inc_void(sv);
647                 SvREFCNT_dec(sv);
648             }
649         }
650
651         SvTEMP_off(thread->init_function);
652         ptr_table_free(PL_ptr_table);
653         PL_ptr_table = NULL;
654         PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
655     }
656     S_ithread_set(aTHX_ current_thread);
657     PERL_SET_CONTEXT(aTHX);
658
659     /* Create/start the thread */
660 #ifdef WIN32
661     thread->handle = CreateThread(NULL,
662                                   (DWORD)thread->stack_size,
663                                   S_ithread_run,
664                                   (LPVOID)thread,
665                                   STACK_SIZE_PARAM_IS_A_RESERVATION,
666                                   &thread->thr);
667 #else
668     {
669         static pthread_attr_t attr;
670         static int attr_inited = 0;
671         static int attr_joinable = PTHREAD_CREATE_JOINABLE;
672         if (! attr_inited) {
673             pthread_attr_init(&attr);
674             attr_inited = 1;
675         }
676
677 #  ifdef PTHREAD_ATTR_SETDETACHSTATE
678         /* Threads start out joinable */
679         PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
680 #  endif
681
682 #  ifdef _POSIX_THREAD_ATTR_STACKSIZE
683         /* Set thread's stack size */
684         if (thread->stack_size > 0) {
685             rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
686         }
687 #  endif
688
689         /* Create the thread */
690         if (! rc_stack_size) {
691 #  ifdef OLD_PTHREADS_API
692             rc_thread_create = pthread_create(&thread->thr,
693                                               attr,
694                                               S_ithread_run,
695                                               (void *)thread);
696 #  else
697 #    if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
698             pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
699 #    endif
700             rc_thread_create = pthread_create(&thread->thr,
701                                               &attr,
702                                               S_ithread_run,
703                                               (void *)thread);
704 #  endif
705         }
706
707 #  ifdef _POSIX_THREAD_ATTR_STACKSIZE
708         /* Try to get thread's actual stack size */
709         {
710             size_t stacksize;
711 #ifdef HPUX1020
712             stacksize = pthread_attr_getstacksize(attr);
713 #else
714             if (! pthread_attr_getstacksize(&attr, &stacksize))
715 #endif
716                 if (stacksize > 0) {
717                     thread->stack_size = (IV)stacksize;
718                 }
719         }
720 #  endif
721     }
722 #endif
723
724     /* Check for errors */
725 #ifdef WIN32
726     if (thread->handle == NULL) {
727 #else
728     if (rc_stack_size || rc_thread_create) {
729 #endif
730         /* Must unlock mutex for destruct call */
731         MUTEX_UNLOCK(&create_destruct_mutex);
732         sv_2mortal(params);
733         S_ithread_destruct(aTHX_ thread);
734 #ifndef WIN32
735         if (ckWARN_d(WARN_THREADS)) {
736             if (rc_stack_size) {
737                 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
738             } else {
739                 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
740             }
741         }
742 #endif
743         return (NULL);
744     }
745
746     running_threads++;
747     sv_2mortal(params);
748     return (thread);
749 }
750
751 #endif /* USE_ITHREADS */
752
753
754 MODULE = threads    PACKAGE = threads    PREFIX = ithread_
755 PROTOTYPES: DISABLE
756
757 #ifdef USE_ITHREADS
758
759 void
760 ithread_create(...)
761     PREINIT:
762         char *classname;
763         ithread *thread;
764         SV *function_to_call;
765         AV *params;
766         HV *specs;
767         IV stack_size;
768         int context;
769         int exit_opt;
770         SV *thread_exit_only;
771         char *str;
772         int idx;
773         int ii;
774     CODE:
775         if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
776             if (--items < 2) {
777                 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
778             }
779             specs = (HV*)SvRV(ST(1));
780             idx = 1;
781         } else {
782             if (items < 2) {
783                 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
784             }
785             specs = NULL;
786             idx = 0;
787         }
788
789         if (sv_isobject(ST(0))) {
790             /* $thr->create() */
791             classname = HvNAME(SvSTASH(SvRV(ST(0))));
792             thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
793             stack_size = thread->stack_size;
794             exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
795         } else {
796             /* threads->create() */
797             classname = (char *)SvPV_nolen(ST(0));
798             stack_size = default_stack_size;
799             thread_exit_only = get_sv("threads::thread_exit_only", TRUE);
800             exit_opt = (SvTRUE(thread_exit_only))
801                                     ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
802         }
803
804         function_to_call = ST(idx+1);
805
806         context = -1;
807         if (specs) {
808             /* stack_size */
809             if (hv_exists(specs, "stack", 5)) {
810                 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
811             } else if (hv_exists(specs, "stacksize", 9)) {
812                 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
813             } else if (hv_exists(specs, "stack_size", 10)) {
814                 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
815             }
816
817             /* context */
818             if (hv_exists(specs, "context", 7)) {
819                 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
820                 switch (*str) {
821                     case 'a':
822                     case 'A':
823                         context = G_ARRAY;
824                         break;
825                     case 's':
826                     case 'S':
827                         context = G_SCALAR;
828                         break;
829                     case 'v':
830                     case 'V':
831                         context = G_VOID;
832                         break;
833                     default:
834                         Perl_croak(aTHX_ "Invalid context: %s", str);
835                 }
836             } else if (hv_exists(specs, "array", 5)) {
837                 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
838                     context = G_ARRAY;
839                 }
840             } else if (hv_exists(specs, "scalar", 6)) {
841                 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
842                     context = G_SCALAR;
843                 }
844             } else if (hv_exists(specs, "void", 4)) {
845                 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
846                     context = G_VOID;
847                 }
848             }
849
850             /* exit => thread_only */
851             if (hv_exists(specs, "exit", 4)) {
852                 str = (char *)SvPV_nolen(*hv_fetch(specs, "exit", 4, 0));
853                 exit_opt = (*str == 't' || *str == 'T')
854                                     ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
855             }
856         }
857         if (context == -1) {
858             context = GIMME_V;  /* Implicit context */
859         } else {
860             context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
861         }
862
863         /* Function args */
864         params = newAV();
865         if (items > 2) {
866             for (ii=2; ii < items ; ii++) {
867                 av_push(params, SvREFCNT_inc(ST(idx+ii)));
868             }
869         }
870
871         /* Create thread */
872         MUTEX_LOCK(&create_destruct_mutex);
873         thread = S_ithread_create(aTHX_ function_to_call,
874                                         stack_size,
875                                         context,
876                                         exit_opt,
877                                         newRV_noinc((SV*)params));
878         if (! thread) {
879             XSRETURN_UNDEF;     /* Mutex already unlocked */
880         }
881         ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
882
883         /* Let thread run */
884         MUTEX_UNLOCK(&thread->mutex);
885         MUTEX_UNLOCK(&create_destruct_mutex);
886
887         /* XSRETURN(1); - implied */
888
889
890 void
891 ithread_list(...)
892     PREINIT:
893         char *classname;
894         ithread *thread;
895         int list_context;
896         IV count = 0;
897         int want_running;
898     PPCODE:
899         /* Class method only */
900         if (SvROK(ST(0))) {
901             Perl_croak(aTHX_ "Usage: threads->list(...)");
902         }
903         classname = (char *)SvPV_nolen(ST(0));
904
905         /* Calling context */
906         list_context = (GIMME_V == G_ARRAY);
907
908         /* Running or joinable parameter */
909         if (items > 1) {
910             want_running = SvTRUE(ST(1));
911         }
912
913         /* Walk through threads list */
914         MUTEX_LOCK(&create_destruct_mutex);
915         for (thread = main_thread.next;
916              thread != &main_thread;
917              thread = thread->next)
918         {
919             /* Ignore detached or joined threads */
920             if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
921                 continue;
922             }
923
924             /* Filter per parameter */
925             if (items > 1) {
926                 if (want_running) {
927                     if (thread->state & PERL_ITHR_FINISHED) {
928                         continue;   /* Not running */
929                     }
930                 } else {
931                     if (! (thread->state & PERL_ITHR_FINISHED)) {
932                         continue;   /* Still running - not joinable yet */
933                     }
934                 }
935             }
936
937             /* Push object on stack if list context */
938             if (list_context) {
939                 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
940             }
941             count++;
942         }
943         MUTEX_UNLOCK(&create_destruct_mutex);
944         /* If scalar context, send back count */
945         if (! list_context) {
946             XSRETURN_IV(count);
947         }
948
949
950 void
951 ithread_self(...)
952     PREINIT:
953         char *classname;
954         ithread *thread;
955     CODE:
956         /* Class method only */
957         if (SvROK(ST(0))) {
958             Perl_croak(aTHX_ "Usage: threads->self()");
959         }
960         classname = (char *)SvPV_nolen(ST(0));
961
962         thread = S_ithread_get(aTHX);
963
964         ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
965         /* XSRETURN(1); - implied */
966
967
968 void
969 ithread_tid(...)
970     PREINIT:
971         ithread *thread;
972     CODE:
973         thread = SV_to_ithread(aTHX_ ST(0));
974         XST_mUV(0, thread->tid);
975         /* XSRETURN(1); - implied */
976
977
978 void
979 ithread_join(...)
980     PREINIT:
981         ithread *thread;
982         int join_err;
983         AV *params;
984         int len;
985         int ii;
986 #ifdef WIN32
987         DWORD waitcode;
988 #else
989         void *retval;
990 #endif
991     PPCODE:
992         /* Object method only */
993         if (! sv_isobject(ST(0))) {
994             Perl_croak(aTHX_ "Usage: $thr->join()");
995         }
996
997         /* Check if the thread is joinable */
998         thread = SV_to_ithread(aTHX_ ST(0));
999         join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
1000         if (join_err) {
1001             if (join_err & PERL_ITHR_DETACHED) {
1002                 Perl_croak(aTHX_ "Cannot join a detached thread");
1003             } else {
1004                 Perl_croak(aTHX_ "Thread already joined");
1005             }
1006         }
1007
1008         /* Join the thread */
1009 #ifdef WIN32
1010         waitcode = WaitForSingleObject(thread->handle, INFINITE);
1011 #else
1012         pthread_join(thread->thr, &retval);
1013 #endif
1014
1015         MUTEX_LOCK(&thread->mutex);
1016         /* Mark as joined */
1017         thread->state |= PERL_ITHR_JOINED;
1018
1019         /* Get the return value from the call_sv */
1020         {
1021             AV *params_copy;
1022             PerlInterpreter *other_perl;
1023             CLONE_PARAMS clone_params;
1024             ithread *current_thread;
1025
1026             params_copy = (AV *)SvRV(thread->params);
1027             other_perl = thread->interp;
1028             clone_params.stashes = newAV();
1029             clone_params.flags = CLONEf_JOIN_IN;
1030             PL_ptr_table = ptr_table_new();
1031             current_thread = S_ithread_get(aTHX);
1032             S_ithread_set(aTHX_ thread);
1033             /* Ensure 'meaningful' addresses retain their meaning */
1034             ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1035             ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1036             ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1037             params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1038             S_ithread_set(aTHX_ current_thread);
1039             SvREFCNT_dec(clone_params.stashes);
1040             SvREFCNT_inc_void(params);
1041             ptr_table_free(PL_ptr_table);
1042             PL_ptr_table = NULL;
1043         }
1044
1045         /* We are finished with the thread */
1046         S_ithread_clear(aTHX_ thread);
1047         MUTEX_UNLOCK(&thread->mutex);
1048
1049         MUTEX_LOCK(&create_destruct_mutex);
1050         if (! (thread->state & PERL_ITHR_DETACHED)) {
1051             joinable_threads--;
1052         }
1053         MUTEX_UNLOCK(&create_destruct_mutex);
1054
1055         /* If no return values, then just return */
1056         if (! params) {
1057             XSRETURN_UNDEF;
1058         }
1059
1060         /* Put return values on stack */
1061         len = (int)AvFILL(params);
1062         for (ii=0; ii <= len; ii++) {
1063             SV* param = av_shift(params);
1064             XPUSHs(sv_2mortal(param));
1065         }
1066
1067         /* Free return value array */
1068         SvREFCNT_dec(params);
1069
1070
1071 void
1072 ithread_yield(...)
1073     CODE:
1074         YIELD;
1075
1076
1077 void
1078 ithread_detach(...)
1079     PREINIT:
1080         ithread *thread;
1081         int detach_err;
1082         int cleanup;
1083     CODE:
1084         /* Check if the thread is detachable */
1085         thread = SV_to_ithread(aTHX_ ST(0));
1086         if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
1087             if (detach_err & PERL_ITHR_DETACHED) {
1088                 Perl_croak(aTHX_ "Thread already detached");
1089             } else {
1090                 Perl_croak(aTHX_ "Cannot detach a joined thread");
1091             }
1092         }
1093
1094         /* Detach the thread */
1095         MUTEX_LOCK(&create_destruct_mutex);
1096         MUTEX_LOCK(&thread->mutex);
1097         thread->state |= PERL_ITHR_DETACHED;
1098 #ifdef WIN32
1099         /* Windows has no 'detach thread' function */
1100 #else
1101         PERL_THREAD_DETACH(thread->thr);
1102 #endif
1103         /* Cleanup if finished */
1104         cleanup = (thread->state & PERL_ITHR_FINISHED);
1105         MUTEX_UNLOCK(&thread->mutex);
1106
1107         if (cleanup) {
1108             joinable_threads--;
1109         } else {
1110             running_threads--;
1111             detached_threads++;
1112         }
1113         MUTEX_UNLOCK(&create_destruct_mutex);
1114
1115         if (cleanup) {
1116             S_ithread_destruct(aTHX_ thread);
1117         }
1118
1119
1120 void
1121 ithread_kill(...)
1122     PREINIT:
1123         ithread *thread;
1124         char *sig_name;
1125         IV signal;
1126     CODE:
1127         /* Must have safe signals */
1128         if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
1129             Perl_croak(aTHX_ "Cannot signal threads without safe signals");
1130         }
1131
1132         /* Object method only */
1133         if (! sv_isobject(ST(0))) {
1134             Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1135         }
1136
1137         /* Get signal */
1138         sig_name = SvPV_nolen(ST(1));
1139         if (isALPHA(*sig_name)) {
1140             if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
1141                 sig_name += 3;
1142             }
1143             if ((signal = whichsig(sig_name)) < 0) {
1144                 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1145             }
1146         } else {
1147             signal = SvIV(ST(1));
1148         }
1149
1150         /* Set the signal for the thread */
1151         thread = SV_to_ithread(aTHX_ ST(0));
1152         MUTEX_LOCK(&thread->mutex);
1153         if (thread->interp) {
1154             dTHXa(thread->interp);
1155             PL_psig_pend[signal]++;
1156             PL_sig_pending = 1;
1157         }
1158         MUTEX_UNLOCK(&thread->mutex);
1159
1160         /* Return the thread to allow for method chaining */
1161         ST(0) = ST(0);
1162         /* XSRETURN(1); - implied */
1163
1164
1165 void
1166 ithread_DESTROY(...)
1167     CODE:
1168         sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1169
1170
1171 void
1172 ithread_equal(...)
1173     PREINIT:
1174         int are_equal = 0;
1175     CODE:
1176         /* Compares TIDs to determine thread equality */
1177         if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1178             ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1179             ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1180             are_equal = (thr1->tid == thr2->tid);
1181         }
1182         if (are_equal) {
1183             XST_mYES(0);
1184         } else {
1185             /* Return 0 on false for backward compatibility */
1186             XST_mIV(0, 0);
1187         }
1188         /* XSRETURN(1); - implied */
1189
1190
1191 void
1192 ithread_object(...)
1193     PREINIT:
1194         char *classname;
1195         UV tid;
1196         ithread *thread;
1197         int have_obj = 0;
1198     CODE:
1199         /* Class method only */
1200         if (SvROK(ST(0))) {
1201             Perl_croak(aTHX_ "Usage: threads->object($tid)");
1202         }
1203         classname = (char *)SvPV_nolen(ST(0));
1204
1205         if ((items < 2) || ! SvOK(ST(1))) {
1206             XSRETURN_UNDEF;
1207         }
1208
1209         /* threads->object($tid) */
1210         tid = SvUV(ST(1));
1211
1212         /* Walk through threads list */
1213         MUTEX_LOCK(&create_destruct_mutex);
1214         for (thread = main_thread.next;
1215              thread != &main_thread;
1216              thread = thread->next)
1217         {
1218             /* Look for TID */
1219             if (thread->tid == tid) {
1220                 /* Ignore if detached or joined */
1221                 if (! (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
1222                     /* Put object on stack */
1223                     ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1224                     have_obj = 1;
1225                 }
1226                 break;
1227             }
1228         }
1229         MUTEX_UNLOCK(&create_destruct_mutex);
1230
1231         if (! have_obj) {
1232             XSRETURN_UNDEF;
1233         }
1234         /* XSRETURN(1); - implied */
1235
1236
1237 void
1238 ithread__handle(...);
1239     PREINIT:
1240         ithread *thread;
1241     CODE:
1242         thread = SV_to_ithread(aTHX_ ST(0));
1243 #ifdef WIN32
1244         XST_mUV(0, PTR2UV(&thread->handle));
1245 #else
1246         XST_mUV(0, PTR2UV(&thread->thr));
1247 #endif
1248         /* XSRETURN(1); - implied */
1249
1250
1251 void
1252 ithread_get_stack_size(...)
1253     PREINIT:
1254         IV stack_size;
1255     CODE:
1256         if (sv_isobject(ST(0))) {
1257             /* $thr->get_stack_size() */
1258             ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1259             stack_size = thread->stack_size;
1260         } else {
1261             /* threads->get_stack_size() */
1262             stack_size = default_stack_size;
1263         }
1264         XST_mIV(0, stack_size);
1265         /* XSRETURN(1); - implied */
1266
1267
1268 void
1269 ithread_set_stack_size(...)
1270     PREINIT:
1271         IV old_size;
1272     CODE:
1273         if (items != 2) {
1274             Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1275         }
1276         if (sv_isobject(ST(0))) {
1277             Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1278         }
1279
1280         old_size = default_stack_size;
1281         default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1282         XST_mIV(0, old_size);
1283         /* XSRETURN(1); - implied */
1284
1285
1286 void
1287 ithread_is_running(...)
1288     PREINIT:
1289         ithread *thread;
1290     CODE:
1291         /* Object method only */
1292         if (! sv_isobject(ST(0))) {
1293             Perl_croak(aTHX_ "Usage: $thr->is_running()");
1294         }
1295
1296         thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1297         ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1298         /* XSRETURN(1); - implied */
1299
1300
1301 void
1302 ithread_is_detached(...)
1303     PREINIT:
1304         ithread *thread;
1305     CODE:
1306         thread = SV_to_ithread(aTHX_ ST(0));
1307         ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1308         /* XSRETURN(1); - implied */
1309
1310
1311 void
1312 ithread_is_joinable(...)
1313     PREINIT:
1314         ithread *thread;
1315     CODE:
1316         /* Object method only */
1317         if (! sv_isobject(ST(0))) {
1318             Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
1319         }
1320
1321         thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1322         MUTEX_LOCK(&thread->mutex);
1323         ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1324                  ! (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1325             ? &PL_sv_yes : &PL_sv_no;
1326         MUTEX_UNLOCK(&thread->mutex);
1327         /* XSRETURN(1); - implied */
1328
1329
1330 void
1331 ithread_wantarray(...)
1332     PREINIT:
1333         ithread *thread;
1334     CODE:
1335         thread = SV_to_ithread(aTHX_ ST(0));
1336         ST(0) = (thread->gimme & G_ARRAY) ? &PL_sv_yes :
1337                 (thread->gimme & G_VOID)  ? &PL_sv_undef
1338                            /* G_SCALAR */ : &PL_sv_no;
1339         /* XSRETURN(1); - implied */
1340
1341
1342 void
1343 ithread_set_thread_exit_only(...)
1344     PREINIT:
1345         ithread *thread;
1346     CODE:
1347         if (items != 2) {
1348             Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
1349         }
1350         thread = SV_to_ithread(aTHX_ ST(0));
1351         MUTEX_LOCK(&thread->mutex);
1352         if (SvTRUE(ST(1))) {
1353             thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1354         } else {
1355             thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1356         }
1357         MUTEX_UNLOCK(&thread->mutex);
1358
1359 #endif /* USE_ITHREADS */
1360
1361
1362 BOOT:
1363 {
1364 #ifdef USE_ITHREADS
1365     MY_CXT_INIT;
1366
1367     PL_perl_destruct_level = 2;
1368     MUTEX_INIT(&create_destruct_mutex);
1369     MUTEX_LOCK(&create_destruct_mutex);
1370
1371     PL_threadhook = &Perl_ithread_hook;
1372
1373     /* The 'main' thread is thread 0.
1374      * It is detached (unjoinable) and immortal.
1375      */
1376     Zero(&main_thread, 1, ithread);         /* Thread 0 */
1377
1378     MUTEX_INIT(&main_thread.mutex);
1379
1380     /* Head of the threads list */
1381     main_thread.next = &main_thread;
1382     main_thread.prev = &main_thread;
1383
1384     main_thread.count = 1;                  /* Immortal */
1385
1386     main_thread.interp = aTHX;
1387     main_thread.state = PERL_ITHR_DETACHED; /* Detached */
1388     main_thread.stack_size = default_stack_size;
1389 #  ifdef WIN32
1390     main_thread.thr = GetCurrentThreadId();
1391 #  else
1392     main_thread.thr = pthread_self();
1393 #  endif
1394
1395     S_ithread_set(aTHX_ &main_thread);
1396     MUTEX_UNLOCK(&create_destruct_mutex);
1397 #endif /* USE_ITHREADS */
1398 }