Commit | Line | Data |
---|---|---|
68795e93 NIS |
1 | #define PERL_NO_GET_CONTEXT |
2 | #include "EXTERN.h" | |
3 | #include "perl.h" | |
4 | #include "XSUB.h" | |
4dcb9e53 JH |
5 | /* Workaround for XSUB.h bug under WIN32 */ |
6 | #ifdef WIN32 | |
7 | # undef setjmp | |
c608f8c0 JH |
8 | # if !defined(__BORLANDC__) |
9 | # define setjmp(x) _setjmp(x) | |
10 | # endif | |
4dcb9e53 | 11 | #endif |
0f1612a7 | 12 | #ifdef HAS_PPPORT_H |
404aaa48 | 13 | # define NEED_PL_signals |
0f1612a7 | 14 | # define NEED_newRV_noinc |
dcefd27c | 15 | # define NEED_sv_2pv_flags |
0f1612a7 JH |
16 | # include "ppport.h" |
17 | # include "threads.h" | |
18 | #endif | |
68795e93 | 19 | |
73e09c8f JH |
20 | #ifdef USE_ITHREADS |
21 | ||
68795e93 | 22 | #ifdef WIN32 |
fc04eb16 | 23 | # include <windows.h> |
514612b7 JH |
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 | |
fc04eb16 | 28 | # include <win32thread.h> |
68795e93 | 29 | #else |
fc04eb16 | 30 | # ifdef OS2 |
5c728af0 | 31 | typedef perl_os_thread pthread_t; |
fc04eb16 JH |
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 | |
467f3f08 | 42 | #endif |
d305c2c9 JH |
43 | #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM) |
44 | # include <sys/param.h> | |
45 | #endif | |
68795e93 | 46 | |
62375a60 | 47 | /* Values for 'state' member */ |
6ebc233e RGS |
48 | #define PERL_ITHR_DETACHED 1 /* Thread has been detached */ |
49 | #define PERL_ITHR_JOINED 2 /* Thread has been joined */ | |
50 | #define PERL_ITHR_FINISHED 4 /* Thread has finished execution */ | |
6158f8b3 | 51 | #define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */ |
6ebc233e RGS |
52 | #define PERL_ITHR_NONVIABLE 16 /* Thread creation failed */ |
53 | #define PERL_ITHR_DIED 32 /* Thread finished by dying */ | |
6158f8b3 DM |
54 | |
55 | #define PERL_ITHR_UNCALLABLE (PERL_ITHR_DETACHED|PERL_ITHR_JOINED) | |
56 | ||
fc04eb16 JH |
57 | |
58 | typedef struct _ithread { | |
59 | struct _ithread *next; /* Next thread in the list */ | |
60 | struct _ithread *prev; /* Prev thread in the list */ | |
61 | PerlInterpreter *interp; /* The threads interpreter */ | |
62 | UV tid; /* Threads module's thread id */ | |
63 | perl_mutex mutex; /* Mutex for updating things in this struct */ | |
6ebc233e | 64 | int count; /* Reference count. See S_ithread_create. */ |
fc04eb16 JH |
65 | int state; /* Detached, joined, finished, etc. */ |
66 | int gimme; /* Context of create */ | |
67 | SV *init_function; /* Code to run */ | |
68 | SV *params; /* Args to pass function */ | |
68795e93 | 69 | #ifdef WIN32 |
fc04eb16 JH |
70 | DWORD thr; /* OS's idea if thread id */ |
71 | HANDLE handle; /* OS's waitable handle */ | |
68795e93 | 72 | #else |
fc04eb16 | 73 | pthread_t thr; /* OS's handle for the thread */ |
68795e93 | 74 | #endif |
514612b7 | 75 | IV stack_size; |
955c272e JH |
76 | SV *err; /* Error from abnormally terminated thread */ |
77 | char *err_class; /* Error object's classname if applicable */ | |
8264cf32 | 78 | #ifndef WIN32 |
b762d866 | 79 | sigset_t initial_sigmask; /* Thread wakes up with signals blocked */ |
8264cf32 | 80 | #endif |
68795e93 NIS |
81 | } ithread; |
82 | ||
fc04eb16 | 83 | |
5c6ff896 | 84 | #define MY_CXT_KEY "threads::_cxt" XS_VERSION |
628ab322 DM |
85 | |
86 | typedef struct { | |
861d5cbe JH |
87 | /* Used by Perl interpreter for thread context switching */ |
88 | ithread *context; | |
628ab322 DM |
89 | } my_cxt_t; |
90 | ||
91 | START_MY_CXT | |
92 | ||
68795e93 | 93 | |
5c6ff896 | 94 | #define MY_POOL_KEY "threads::_pool" XS_VERSION |
68795e93 | 95 | |
5c6ff896 JH |
96 | typedef struct { |
97 | /* Structure for 'main' thread | |
98 | * Also forms the 'base' for the doubly-linked list of threads */ | |
99 | ithread main_thread; | |
100 | ||
101 | /* Protects the creation and destruction of threads*/ | |
102 | perl_mutex create_destruct_mutex; | |
103 | ||
104 | UV tid_counter; | |
105 | IV joinable_threads; | |
106 | IV running_threads; | |
107 | IV detached_threads; | |
e9a908c9 | 108 | IV total_threads; |
5c6ff896 JH |
109 | IV default_stack_size; |
110 | IV page_size; | |
111 | } my_pool_t; | |
112 | ||
113 | #define dMY_POOL \ | |
114 | SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, \ | |
115 | sizeof(MY_POOL_KEY)-1, TRUE); \ | |
116 | my_pool_t *my_poolp = INT2PTR(my_pool_t*, SvUV(my_pool_sv)) | |
117 | ||
118 | #define MY_POOL (*my_poolp) | |
c05ae023 | 119 | |
8264cf32 | 120 | #ifndef WIN32 |
b762d866 JW |
121 | /* Block most signals for calling thread, setting the old signal mask to |
122 | * oldmask, if it is not NULL */ | |
123 | STATIC int | |
124 | S_block_most_signals(sigset_t *oldmask) | |
125 | { | |
126 | sigset_t newmask; | |
127 | ||
128 | sigfillset(&newmask); | |
129 | /* Don't block certain "important" signals (stolen from mg.c) */ | |
130 | #ifdef SIGILL | |
131 | sigdelset(&newmask, SIGILL); | |
132 | #endif | |
133 | #ifdef SIGBUS | |
134 | sigdelset(&newmask, SIGBUS); | |
135 | #endif | |
136 | #ifdef SIGSEGV | |
137 | sigdelset(&newmask, SIGSEGV); | |
138 | #endif | |
139 | ||
8264cf32 | 140 | #if defined(VMS) |
d2aa556d CB |
141 | /* no per-thread blocking available */ |
142 | return sigprocmask(SIG_BLOCK, &newmask, oldmask); | |
b762d866 JW |
143 | #else |
144 | return pthread_sigmask(SIG_BLOCK, &newmask, oldmask); | |
145 | #endif /* WIN32 */ | |
146 | } | |
147 | ||
148 | /* Set the signal mask for this thread to newmask */ | |
149 | STATIC int | |
150 | S_set_sigmask(sigset_t *newmask) | |
151 | { | |
8264cf32 | 152 | #if defined(VMS) |
d2aa556d | 153 | return sigprocmask(SIG_SETMASK, newmask, NULL); |
b762d866 JW |
154 | #else |
155 | return pthread_sigmask(SIG_SETMASK, newmask, NULL); | |
156 | #endif /* WIN32 */ | |
157 | } | |
8264cf32 | 158 | #endif |
c05ae023 | 159 | |
fc04eb16 | 160 | /* Used by Perl interpreter for thread context switching */ |
861d5cbe | 161 | STATIC void |
fc04eb16 | 162 | S_ithread_set(pTHX_ ithread *thread) |
c05ae023 | 163 | { |
628ab322 | 164 | dMY_CXT; |
861d5cbe | 165 | MY_CXT.context = thread; |
c05ae023 AB |
166 | } |
167 | ||
861d5cbe | 168 | STATIC ithread * |
fc04eb16 JH |
169 | S_ithread_get(pTHX) |
170 | { | |
628ab322 | 171 | dMY_CXT; |
861d5cbe | 172 | return (MY_CXT.context); |
c05ae023 AB |
173 | } |
174 | ||
175 | ||
fc04eb16 JH |
176 | /* Free any data (such as the Perl interpreter) attached to an ithread |
177 | * structure. This is a bit like undef on SVs, where the SV isn't freed, | |
6ebc233e RGS |
178 | * but the PVX is. Must be called with thread->mutex already locked. Also, |
179 | * must be called with MY_POOL.create_destruct_mutex unlocked as destruction | |
180 | * of the interpreter can lead to recursive destruction calls that could | |
181 | * lead to a deadlock on that mutex. | |
2e676467 | 182 | */ |
861d5cbe | 183 | STATIC void |
fc04eb16 | 184 | S_ithread_clear(pTHX_ ithread *thread) |
2e676467 DM |
185 | { |
186 | PerlInterpreter *interp; | |
b762d866 | 187 | sigset_t origmask; |
fc04eb16 | 188 | |
adc09a0e | 189 | assert(((thread->state & PERL_ITHR_FINISHED) && |
8718f9a1 | 190 | (thread->state & PERL_ITHR_UNCALLABLE)) |
adc09a0e JH |
191 | || |
192 | (thread->state & PERL_ITHR_NONVIABLE)); | |
2e676467 | 193 | |
8264cf32 | 194 | #ifndef WIN32 |
b762d866 JW |
195 | /* We temporarily set the interpreter context to the interpreter being |
196 | * destroyed. It's in no condition to handle signals while it's being | |
197 | * taken apart. | |
198 | */ | |
199 | S_block_most_signals(&origmask); | |
8264cf32 | 200 | #endif |
b762d866 | 201 | |
2e676467 DM |
202 | interp = thread->interp; |
203 | if (interp) { | |
fc04eb16 JH |
204 | dTHXa(interp); |
205 | ||
206 | PERL_SET_CONTEXT(interp); | |
207 | S_ithread_set(aTHX_ thread); | |
f2cba68d | 208 | |
fc04eb16 JH |
209 | SvREFCNT_dec(thread->params); |
210 | thread->params = Nullsv; | |
2e676467 | 211 | |
955c272e JH |
212 | if (thread->err) { |
213 | SvREFCNT_dec(thread->err); | |
214 | thread->err = Nullsv; | |
215 | } | |
216 | ||
fc04eb16 | 217 | perl_destruct(interp); |
9ca4d7fd | 218 | perl_free(interp); |
fc04eb16 | 219 | thread->interp = NULL; |
2e676467 | 220 | } |
fc04eb16 | 221 | |
2e676467 | 222 | PERL_SET_CONTEXT(aTHX); |
8264cf32 | 223 | #ifndef WIN32 |
b762d866 | 224 | S_set_sigmask(&origmask); |
8264cf32 | 225 | #endif |
2e676467 DM |
226 | } |
227 | ||
68795e93 | 228 | |
6158f8b3 DM |
229 | /* Decrement the refcount of an ithread, and if it reaches zero, free it. |
230 | * Must be called with the mutex held. | |
6ebc233e RGS |
231 | * On return, mutex is released (or destroyed). |
232 | */ | |
861d5cbe | 233 | STATIC void |
6158f8b3 | 234 | S_ithread_free(pTHX_ ithread *thread) |
68795e93 | 235 | { |
385d56e4 | 236 | #ifdef WIN32 |
fc04eb16 | 237 | HANDLE handle; |
385d56e4 | 238 | #endif |
adc09a0e JH |
239 | dMY_POOL; |
240 | ||
6158f8b3 DM |
241 | if (! (thread->state & PERL_ITHR_NONVIABLE)) { |
242 | assert(thread->count > 0); | |
243 | if (--thread->count > 0) { | |
244 | MUTEX_UNLOCK(&thread->mutex); | |
245 | return; | |
246 | } | |
6ebc233e RGS |
247 | assert((thread->state & PERL_ITHR_FINISHED) && |
248 | (thread->state & PERL_ITHR_UNCALLABLE)); | |
fc04eb16 | 249 | } |
adc09a0e | 250 | MUTEX_UNLOCK(&thread->mutex); |
9feacc09 | 251 | |
fc04eb16 JH |
252 | /* Main thread (0) is immortal and should never get here */ |
253 | assert(thread->tid != 0); | |
254 | ||
255 | /* Remove from circular list of threads */ | |
5c6ff896 | 256 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
adc09a0e JH |
257 | assert(thread->prev && thread->next); |
258 | thread->next->prev = thread->prev; | |
259 | thread->prev->next = thread->next; | |
fc04eb16 JH |
260 | thread->next = NULL; |
261 | thread->prev = NULL; | |
5c6ff896 | 262 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
c2f2a82b | 263 | |
fc04eb16 | 264 | /* Thread is now disowned */ |
9ca4d7fd | 265 | MUTEX_LOCK(&thread->mutex); |
fc04eb16 | 266 | S_ithread_clear(aTHX_ thread); |
385d56e4 JH |
267 | |
268 | #ifdef WIN32 | |
fc04eb16 JH |
269 | handle = thread->handle; |
270 | thread->handle = NULL; | |
385d56e4 | 271 | #endif |
fc04eb16 JH |
272 | MUTEX_UNLOCK(&thread->mutex); |
273 | MUTEX_DESTROY(&thread->mutex); | |
385d56e4 | 274 | |
c7667023 | 275 | #ifdef WIN32 |
fea7688c | 276 | if (handle) { |
fc04eb16 | 277 | CloseHandle(handle); |
fea7688c | 278 | } |
c7667023 | 279 | #endif |
385d56e4 | 280 | |
fc04eb16 | 281 | PerlMemShared_free(thread); |
ae3fba3d DM |
282 | |
283 | /* total_threads >= 1 is used to veto cleanup by the main thread, | |
284 | * should it happen to exit while other threads still exist. | |
6ebc233e RGS |
285 | * Decrement this as the very last thing in the thread's existence. |
286 | * Otherwise, MY_POOL and global state such as PL_op_mutex may get | |
287 | * freed while we're still using it. | |
ae3fba3d DM |
288 | */ |
289 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); | |
290 | MY_POOL.total_threads--; | |
291 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); | |
68795e93 NIS |
292 | } |
293 | ||
fc04eb16 | 294 | |
6158f8b3 DM |
295 | static void |
296 | S_ithread_count_inc(pTHX_ ithread *thread) | |
297 | { | |
298 | MUTEX_LOCK(&thread->mutex); | |
299 | thread->count++; | |
300 | MUTEX_UNLOCK(&thread->mutex); | |
301 | } | |
302 | ||
303 | ||
69a9b4b8 | 304 | /* Warn if exiting with any unjoined threads */ |
861d5cbe | 305 | STATIC int |
69a9b4b8 | 306 | S_exit_warning(pTHX) |
62375a60 | 307 | { |
e9a908c9 | 308 | int veto_cleanup, warn; |
adc09a0e | 309 | dMY_POOL; |
69a9b4b8 | 310 | |
5c6ff896 | 311 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
e9a908c9 DM |
312 | veto_cleanup = (MY_POOL.total_threads > 0); |
313 | warn = (MY_POOL.running_threads || MY_POOL.joinable_threads); | |
5c6ff896 | 314 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
60bd5ef6 | 315 | |
e9a908c9 | 316 | if (warn) { |
fc04eb16 | 317 | if (ckWARN_d(WARN_THREADS)) { |
4dcb9e53 JH |
318 | Perl_warn(aTHX_ "Perl exited with active threads:\n\t%" |
319 | IVdf " running and unjoined\n\t%" | |
320 | IVdf " finished and unjoined\n\t%" | |
321 | IVdf " running and detached\n", | |
5c6ff896 JH |
322 | MY_POOL.running_threads, |
323 | MY_POOL.joinable_threads, | |
324 | MY_POOL.detached_threads); | |
fc04eb16 | 325 | } |
62375a60 | 326 | } |
69a9b4b8 | 327 | |
fc04eb16 | 328 | return (veto_cleanup); |
62375a60 NIS |
329 | } |
330 | ||
ae3fba3d | 331 | |
6ebc233e RGS |
332 | /* Called from perl_destruct() in each thread. If it's the main thread, |
333 | * stop it from freeing everything if there are other threads still running. | |
334 | */ | |
69a9b4b8 RGS |
335 | int |
336 | Perl_ithread_hook(pTHX) | |
337 | { | |
5c6ff896 | 338 | dMY_POOL; |
b5c80a23 | 339 | return ((aTHX == MY_POOL.main_thread.interp) ? S_exit_warning(aTHX) : 0); |
69a9b4b8 RGS |
340 | } |
341 | ||
68795e93 NIS |
342 | |
343 | /* MAGIC (in mg.h sense) hooks */ | |
344 | ||
345 | int | |
346 | ithread_mg_get(pTHX_ SV *sv, MAGIC *mg) | |
347 | { | |
fc04eb16 | 348 | ithread *thread = (ithread *)mg->mg_ptr; |
45977657 | 349 | SvIV_set(sv, PTR2IV(thread)); |
68795e93 | 350 | SvIOK_on(sv); |
fc04eb16 | 351 | return (0); |
68795e93 NIS |
352 | } |
353 | ||
354 | int | |
355 | ithread_mg_free(pTHX_ SV *sv, MAGIC *mg) | |
356 | { | |
f2cba68d | 357 | ithread *thread = (ithread *)mg->mg_ptr; |
68795e93 | 358 | MUTEX_LOCK(&thread->mutex); |
6ebc233e | 359 | S_ithread_free(aTHX_ thread); /* Releases MUTEX */ |
fc04eb16 | 360 | return (0); |
68795e93 NIS |
361 | } |
362 | ||
363 | int | |
364 | ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) | |
365 | { | |
6158f8b3 | 366 | S_ithread_count_inc(aTHX_ (ithread *)mg->mg_ptr); |
fc04eb16 | 367 | return (0); |
68795e93 NIS |
368 | } |
369 | ||
370 | MGVTBL ithread_vtbl = { | |
fc04eb16 JH |
371 | ithread_mg_get, /* get */ |
372 | 0, /* set */ | |
373 | 0, /* len */ | |
374 | 0, /* clear */ | |
375 | ithread_mg_free, /* free */ | |
376 | 0, /* copy */ | |
377 | ithread_mg_dup /* dup */ | |
68795e93 NIS |
378 | }; |
379 | ||
47ba8780 | 380 | |
514612b7 | 381 | /* Provided default, minimum and rational stack sizes */ |
861d5cbe JH |
382 | STATIC IV |
383 | S_good_stack_size(pTHX_ IV stack_size) | |
514612b7 | 384 | { |
5c6ff896 JH |
385 | dMY_POOL; |
386 | ||
514612b7 | 387 | /* Use default stack size if no stack size specified */ |
fea7688c | 388 | if (! stack_size) { |
5c6ff896 | 389 | return (MY_POOL.default_stack_size); |
fea7688c | 390 | } |
514612b7 JH |
391 | |
392 | #ifdef PTHREAD_STACK_MIN | |
393 | /* Can't use less than minimum */ | |
394 | if (stack_size < PTHREAD_STACK_MIN) { | |
4dcb9e53 | 395 | if (ckWARN(WARN_THREADS)) { |
514612b7 JH |
396 | Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN); |
397 | } | |
398 | return (PTHREAD_STACK_MIN); | |
399 | } | |
400 | #endif | |
401 | ||
402 | /* Round up to page size boundary */ | |
5c6ff896 | 403 | if (MY_POOL.page_size <= 0) { |
d305c2c9 | 404 | #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE)) |
514612b7 | 405 | SETERRNO(0, SS_NORMAL); |
d305c2c9 | 406 | # ifdef _SC_PAGESIZE |
5c6ff896 | 407 | MY_POOL.page_size = sysconf(_SC_PAGESIZE); |
d305c2c9 | 408 | # else |
5c6ff896 | 409 | MY_POOL.page_size = sysconf(_SC_MMAP_PAGE_SIZE); |
d305c2c9 | 410 | # endif |
5c6ff896 | 411 | if ((long)MY_POOL.page_size < 0) { |
514612b7 | 412 | if (errno) { |
8583b257 | 413 | SV * const error = get_sv("@", 0); |
514612b7 JH |
414 | (void)SvUPGRADE(error, SVt_PV); |
415 | Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error)); | |
416 | } else { | |
417 | Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown"); | |
418 | } | |
419 | } | |
d305c2c9 JH |
420 | #else |
421 | # ifdef HAS_GETPAGESIZE | |
5c6ff896 | 422 | MY_POOL.page_size = getpagesize(); |
514612b7 | 423 | # else |
d305c2c9 | 424 | # if defined(I_SYS_PARAM) && defined(PAGESIZE) |
5c6ff896 | 425 | MY_POOL.page_size = PAGESIZE; |
d305c2c9 | 426 | # else |
5c6ff896 | 427 | MY_POOL.page_size = 8192; /* A conservative default */ |
d305c2c9 | 428 | # endif |
514612b7 | 429 | # endif |
5c6ff896 JH |
430 | if (MY_POOL.page_size <= 0) { |
431 | Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)MY_POOL.page_size); | |
fea7688c | 432 | } |
514612b7 JH |
433 | #endif |
434 | } | |
5c6ff896 | 435 | stack_size = ((stack_size + (MY_POOL.page_size - 1)) / MY_POOL.page_size) * MY_POOL.page_size; |
514612b7 JH |
436 | |
437 | return (stack_size); | |
438 | } | |
439 | ||
440 | ||
fc04eb16 JH |
441 | /* Starts executing the thread. |
442 | * Passed as the C level function to run in the new thread. | |
b1edfb69 | 443 | */ |
47ba8780 | 444 | #ifdef WIN32 |
861d5cbe | 445 | STATIC THREAD_RET_TYPE |
fc04eb16 | 446 | S_ithread_run(LPVOID arg) |
47ba8780 | 447 | #else |
861d5cbe | 448 | STATIC void * |
fc04eb16 | 449 | S_ithread_run(void * arg) |
47ba8780 | 450 | #endif |
fc04eb16 JH |
451 | { |
452 | ithread *thread = (ithread *)arg; | |
69a9b4b8 RGS |
453 | int jmp_rc = 0; |
454 | I32 oldscope; | |
955c272e | 455 | int exit_app = 0; /* Thread terminated using 'exit' */ |
69a9b4b8 | 456 | int exit_code = 0; |
955c272e | 457 | int died = 0; /* Thread terminated abnormally */ |
f2cba68d | 458 | |
69a9b4b8 RGS |
459 | dJMPENV; |
460 | ||
fc04eb16 | 461 | dTHXa(thread->interp); |
47ba8780 | 462 | |
5c6ff896 JH |
463 | dMY_POOL; |
464 | ||
9ca4d7fd | 465 | /* Blocked until ->create() call finishes */ |
fc04eb16 | 466 | MUTEX_LOCK(&thread->mutex); |
fc04eb16 | 467 | MUTEX_UNLOCK(&thread->mutex); |
9ca4d7fd JH |
468 | |
469 | PERL_SET_CONTEXT(thread->interp); | |
470 | S_ithread_set(aTHX_ thread); | |
47ba8780 | 471 | |
8264cf32 | 472 | #ifndef WIN32 |
b762d866 JW |
473 | /* Thread starts with most signals blocked - restore the signal mask from |
474 | * the ithread struct. | |
475 | */ | |
476 | S_set_sigmask(&thread->initial_sigmask); | |
8264cf32 | 477 | #endif |
b762d866 | 478 | |
fc04eb16 | 479 | PL_perl_destruct_level = 2; |
f2cba68d | 480 | |
fc04eb16 JH |
481 | { |
482 | AV *params = (AV *)SvRV(thread->params); | |
483 | int len = (int)av_len(params)+1; | |
484 | int ii; | |
485 | ||
486 | dSP; | |
487 | ENTER; | |
488 | SAVETMPS; | |
489 | ||
490 | /* Put args on the stack */ | |
491 | PUSHMARK(SP); | |
492 | for (ii=0; ii < len; ii++) { | |
493 | XPUSHs(av_shift(params)); | |
494 | } | |
495 | PUTBACK; | |
496 | ||
4dcb9e53 JH |
497 | oldscope = PL_scopestack_ix; |
498 | JMPENV_PUSH(jmp_rc); | |
499 | if (jmp_rc == 0) { | |
500 | /* Run the specified function */ | |
501 | len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL); | |
502 | } else if (jmp_rc == 2) { | |
69a9b4b8 RGS |
503 | /* Thread exited */ |
504 | exit_app = 1; | |
505 | exit_code = STATUS_CURRENT; | |
4dcb9e53 JH |
506 | while (PL_scopestack_ix > oldscope) { |
507 | LEAVE; | |
508 | } | |
509 | } | |
510 | JMPENV_POP; | |
fc04eb16 | 511 | |
8264cf32 | 512 | #ifndef WIN32 |
b762d866 JW |
513 | /* The interpreter is finished, so this thread can stop receiving |
514 | * signals. This way, our signal handler doesn't get called in the | |
515 | * middle of our parent thread calling perl_destruct()... | |
516 | */ | |
517 | S_block_most_signals(NULL); | |
8264cf32 | 518 | #endif |
b762d866 | 519 | |
fc04eb16 JH |
520 | /* Remove args from stack and put back in params array */ |
521 | SPAGAIN; | |
522 | for (ii=len-1; ii >= 0; ii--) { | |
523 | SV *sv = POPs; | |
7df0357e | 524 | if (jmp_rc == 0 && (thread->gimme & G_WANT) != G_VOID) { |
4dcb9e53 JH |
525 | av_store(params, ii, SvREFCNT_inc(sv)); |
526 | } | |
fc04eb16 JH |
527 | } |
528 | ||
4dcb9e53 JH |
529 | FREETMPS; |
530 | LEAVE; | |
531 | ||
955c272e JH |
532 | /* Check for abnormal termination */ |
533 | if (SvTRUE(ERRSV)) { | |
534 | died = PERL_ITHR_DIED; | |
535 | thread->err = newSVsv(ERRSV); | |
536 | /* If ERRSV is an object, remember the classname and then | |
537 | * rebless into 'main' so it will survive 'cloning' | |
538 | */ | |
539 | if (sv_isobject(thread->err)) { | |
540 | thread->err_class = HvNAME(SvSTASH(SvRV(thread->err))); | |
541 | sv_bless(thread->err, gv_stashpv("main", 0)); | |
542 | } | |
543 | ||
544 | if (ckWARN_d(WARN_THREADS)) { | |
545 | oldscope = PL_scopestack_ix; | |
546 | JMPENV_PUSH(jmp_rc); | |
547 | if (jmp_rc == 0) { | |
548 | /* Warn that thread died */ | |
549 | Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV); | |
550 | } else if (jmp_rc == 2) { | |
551 | /* Warn handler exited */ | |
552 | exit_app = 1; | |
553 | exit_code = STATUS_CURRENT; | |
554 | while (PL_scopestack_ix > oldscope) { | |
555 | LEAVE; | |
556 | } | |
4dcb9e53 | 557 | } |
955c272e | 558 | JMPENV_POP; |
4dcb9e53 | 559 | } |
fc04eb16 JH |
560 | } |
561 | ||
fc04eb16 JH |
562 | /* Release function ref */ |
563 | SvREFCNT_dec(thread->init_function); | |
564 | thread->init_function = Nullsv; | |
565 | } | |
62375a60 | 566 | |
fc04eb16 JH |
567 | PerlIO_flush((PerlIO *)NULL); |
568 | ||
5c6ff896 | 569 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
fc04eb16 JH |
570 | MUTEX_LOCK(&thread->mutex); |
571 | /* Mark as finished */ | |
955c272e | 572 | thread->state |= (PERL_ITHR_FINISHED | died); |
69a9b4b8 | 573 | /* Clear exit flag if required */ |
fea7688c | 574 | if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) { |
69a9b4b8 | 575 | exit_app = 0; |
fea7688c | 576 | } |
fc04eb16 | 577 | |
69a9b4b8 | 578 | /* Adjust thread status counts */ |
adc09a0e | 579 | if (thread->state & PERL_ITHR_DETACHED) { |
5c6ff896 | 580 | MY_POOL.detached_threads--; |
4dcb9e53 | 581 | } else { |
5c6ff896 JH |
582 | MY_POOL.running_threads--; |
583 | MY_POOL.joinable_threads++; | |
5168baf3 | 584 | } |
adc09a0e | 585 | MUTEX_UNLOCK(&thread->mutex); |
5c6ff896 | 586 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
69a9b4b8 RGS |
587 | |
588 | /* Exit application if required */ | |
589 | if (exit_app) { | |
590 | oldscope = PL_scopestack_ix; | |
591 | JMPENV_PUSH(jmp_rc); | |
592 | if (jmp_rc == 0) { | |
593 | /* Warn if there are unjoined threads */ | |
594 | S_exit_warning(aTHX); | |
595 | } else if (jmp_rc == 2) { | |
596 | /* Warn handler exited */ | |
597 | exit_code = STATUS_CURRENT; | |
598 | while (PL_scopestack_ix > oldscope) { | |
599 | LEAVE; | |
600 | } | |
601 | } | |
602 | JMPENV_POP; | |
603 | ||
604 | my_exit(exit_code); | |
605 | } | |
606 | ||
6ebc233e RGS |
607 | /* At this point, the interpreter may have been freed, so call |
608 | * free in the the context of of the 'main' interpreter which | |
609 | * can't have been freed due to the veto_cleanup mechanism. | |
610 | */ | |
46c5d8f1 DM |
611 | aTHX = MY_POOL.main_thread.interp; |
612 | ||
6158f8b3 | 613 | MUTEX_LOCK(&thread->mutex); |
6ebc233e | 614 | S_ithread_free(aTHX_ thread); /* Releases MUTEX */ |
91604d21 | 615 | |
47ba8780 | 616 | #ifdef WIN32 |
fc04eb16 | 617 | return ((DWORD)0); |
e8f2bb9a | 618 | #else |
fc04eb16 | 619 | return (0); |
47ba8780 | 620 | #endif |
68795e93 NIS |
621 | } |
622 | ||
fc04eb16 JH |
623 | |
624 | /* Type conversion helper functions */ | |
fea7688c | 625 | |
861d5cbe JH |
626 | STATIC SV * |
627 | S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc) | |
68795e93 NIS |
628 | { |
629 | SV *sv; | |
630 | MAGIC *mg; | |
fc04eb16 | 631 | |
6158f8b3 DM |
632 | if (inc) |
633 | S_ithread_count_inc(aTHX_ thread); | |
fc04eb16 JH |
634 | |
635 | if (! obj) { | |
636 | obj = newSV(0); | |
68795e93 | 637 | } |
fc04eb16 JH |
638 | |
639 | sv = newSVrv(obj, classname); | |
640 | sv_setiv(sv, PTR2IV(thread)); | |
641 | mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0); | |
68795e93 NIS |
642 | mg->mg_flags |= MGf_DUP; |
643 | SvREADONLY_on(sv); | |
fc04eb16 JH |
644 | |
645 | return (obj); | |
68795e93 | 646 | } |
47ba8780 | 647 | |
861d5cbe JH |
648 | STATIC ithread * |
649 | S_SV_to_ithread(pTHX_ SV *sv) | |
68795e93 | 650 | { |
fc04eb16 JH |
651 | /* Argument is a thread */ |
652 | if (SvROK(sv)) { | |
653 | return (INT2PTR(ithread *, SvIV(SvRV(sv)))); | |
654 | } | |
655 | /* Argument is classname, therefore return current thread */ | |
656 | return (S_ithread_get(aTHX)); | |
47ba8780 AB |
657 | } |
658 | ||
47ba8780 | 659 | |
fc04eb16 JH |
660 | /* threads->create() |
661 | * Called in context of parent thread. | |
5c6ff896 | 662 | * Called with MY_POOL.create_destruct_mutex locked. (Unlocked on error.) |
fc04eb16 | 663 | */ |
861d5cbe | 664 | STATIC ithread * |
fc04eb16 | 665 | S_ithread_create( |
9ca4d7fd | 666 | pTHX_ SV *init_function, |
514612b7 | 667 | IV stack_size, |
9d9ff5b1 | 668 | int gimme, |
69a9b4b8 | 669 | int exit_opt, |
fc04eb16 | 670 | SV *params) |
68795e93 | 671 | { |
fc04eb16 | 672 | ithread *thread; |
fc04eb16 | 673 | ithread *current_thread = S_ithread_get(aTHX); |
3b1c3273 | 674 | |
fc04eb16 JH |
675 | SV **tmps_tmp = PL_tmps_stack; |
676 | IV tmps_ix = PL_tmps_ix; | |
d94006e8 | 677 | #ifndef WIN32 |
fc04eb16 JH |
678 | int rc_stack_size = 0; |
679 | int rc_thread_create = 0; | |
d94006e8 | 680 | #endif |
adc09a0e | 681 | dMY_POOL; |
3b1c3273 | 682 | |
adc09a0e | 683 | /* Allocate thread structure in context of the main thread's interpreter */ |
5c6ff896 JH |
684 | { |
685 | PERL_SET_CONTEXT(MY_POOL.main_thread.interp); | |
686 | thread = (ithread *)PerlMemShared_malloc(sizeof(ithread)); | |
687 | } | |
688 | PERL_SET_CONTEXT(aTHX); | |
fc04eb16 | 689 | if (!thread) { |
5c6ff896 | 690 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
fc04eb16 JH |
691 | PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem)); |
692 | my_exit(1); | |
693 | } | |
694 | Zero(thread, 1, ithread); | |
695 | ||
696 | /* Add to threads list */ | |
5c6ff896 JH |
697 | thread->next = &MY_POOL.main_thread; |
698 | thread->prev = MY_POOL.main_thread.prev; | |
699 | MY_POOL.main_thread.prev = thread; | |
fc04eb16 | 700 | thread->prev->next = thread; |
e9a908c9 | 701 | MY_POOL.total_threads++; |
c05ae023 | 702 | |
6ebc233e | 703 | /* 1 ref to be held by the local var 'thread' in S_ithread_run(). |
6158f8b3 | 704 | * 1 ref to be held by the threads object that we assume we will |
6ebc233e RGS |
705 | * be embedded in upon our return. |
706 | * 1 ref to be the responsibility of join/detach, so we don't get | |
707 | * freed until join/detach, even if no thread objects remain. | |
708 | * This allows the following to work: | |
878090d5 | 709 | * { threads->create(sub{...}); } threads->object(1)->join; |
fc04eb16 | 710 | */ |
6158f8b3 | 711 | thread->count = 3; |
fc04eb16 | 712 | |
9ca4d7fd | 713 | /* Block new thread until ->create() call finishes */ |
fc04eb16 | 714 | MUTEX_INIT(&thread->mutex); |
9ca4d7fd JH |
715 | MUTEX_LOCK(&thread->mutex); |
716 | ||
5c6ff896 | 717 | thread->tid = MY_POOL.tid_counter++; |
861d5cbe | 718 | thread->stack_size = S_good_stack_size(aTHX_ stack_size); |
9d9ff5b1 | 719 | thread->gimme = gimme; |
69a9b4b8 | 720 | thread->state = exit_opt; |
fc04eb16 JH |
721 | |
722 | /* "Clone" our interpreter into the thread's interpreter. | |
723 | * This gives thread access to "static data" and code. | |
724 | */ | |
725 | PerlIO_flush((PerlIO *)NULL); | |
726 | S_ithread_set(aTHX_ thread); | |
727 | ||
728 | SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */ | |
729 | PL_srand_called = FALSE; /* Set it to false so we can detect if it gets | |
730 | set during the clone */ | |
3b1c3273 | 731 | |
8264cf32 | 732 | #ifndef WIN32 |
b762d866 JW |
733 | /* perl_clone() will leave us the new interpreter's context. This poses |
734 | * two problems for our signal handler. First, it sets the new context | |
735 | * before the new interpreter struct is fully initialized, so our signal | |
736 | * handler might find bogus data in the interpreter struct it gets. | |
737 | * Second, even if the interpreter is initialized before a signal comes in, | |
738 | * we would like to avoid that interpreter receiving notifications for | |
739 | * signals (especially when they ought to be for the one running in this | |
740 | * thread), until it is running in its own thread. Another problem is that | |
741 | * the new thread will not have set the context until some time after it | |
742 | * has started, so it won't be safe for our signal handler to run until | |
743 | * that time. | |
744 | * | |
745 | * So we block most signals here, so the new thread will inherit the signal | |
746 | * mask, and unblock them right after the thread creation. The original | |
747 | * mask is saved in the thread struct so that the new thread can restore | |
748 | * the original mask. | |
749 | */ | |
750 | S_block_most_signals(&thread->initial_sigmask); | |
8264cf32 | 751 | #endif |
b762d866 | 752 | |
47ba8780 | 753 | #ifdef WIN32 |
fc04eb16 | 754 | thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST); |
47ba8780 | 755 | #else |
fc04eb16 | 756 | thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE); |
47ba8780 | 757 | #endif |
47ba8780 | 758 | |
fc04eb16 JH |
759 | /* perl_clone() leaves us in new interpreter's context. As it is tricky |
760 | * to spot an implicit aTHX, create a new scope with aTHX matching the | |
761 | * context for the duration of our work for new interpreter. | |
762 | */ | |
763 | { | |
894eec8b JH |
764 | CLONE_PARAMS clone_param; |
765 | ||
fc04eb16 JH |
766 | dTHXa(thread->interp); |
767 | ||
768 | MY_CXT_CLONE; | |
769 | ||
770 | /* Here we remove END blocks since they should only run in the thread | |
771 | * they are created | |
772 | */ | |
773 | SvREFCNT_dec(PL_endav); | |
774 | PL_endav = newAV(); | |
404aaa48 | 775 | |
894eec8b | 776 | clone_param.flags = 0; |
f2e0bb91 JH |
777 | if (SvPOK(init_function)) { |
778 | thread->init_function = newSV(0); | |
779 | sv_copypv(thread->init_function, init_function); | |
780 | } else { | |
dd5ef8e0 DM |
781 | thread->init_function = |
782 | SvREFCNT_inc(sv_dup(init_function, &clone_param)); | |
fc04eb16 JH |
783 | } |
784 | ||
785 | thread->params = sv_dup(params, &clone_param); | |
d4315dd6 | 786 | SvREFCNT_inc_void(thread->params); |
fc04eb16 JH |
787 | |
788 | /* The code below checks that anything living on the tmps stack and | |
789 | * has been cloned (so it lives in the ptr_table) has a refcount | |
790 | * higher than 0. | |
791 | * | |
792 | * If the refcount is 0 it means that a something on the stack/context | |
793 | * was holding a reference to it and since we init_stacks() in | |
794 | * perl_clone that won't get cleaned and we will get a leaked scalar. | |
795 | * The reason it was cloned was that it lived on the @_ stack. | |
796 | * | |
797 | * Example of this can be found in bugreport 15837 where calls in the | |
798 | * parameter list end up as a temp. | |
799 | * | |
800 | * One could argue that this fix should be in perl_clone. | |
801 | */ | |
802 | while (tmps_ix > 0) { | |
803 | SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]); | |
804 | tmps_ix--; | |
805 | if (sv && SvREFCNT(sv) == 0) { | |
d4315dd6 | 806 | SvREFCNT_inc_void(sv); |
fc04eb16 JH |
807 | SvREFCNT_dec(sv); |
808 | } | |
809 | } | |
810 | ||
811 | SvTEMP_off(thread->init_function); | |
812 | ptr_table_free(PL_ptr_table); | |
813 | PL_ptr_table = NULL; | |
814 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; | |
815 | } | |
816 | S_ithread_set(aTHX_ current_thread); | |
817 | PERL_SET_CONTEXT(aTHX); | |
818 | ||
819 | /* Create/start the thread */ | |
47ba8780 | 820 | #ifdef WIN32 |
fc04eb16 | 821 | thread->handle = CreateThread(NULL, |
514612b7 | 822 | (DWORD)thread->stack_size, |
fc04eb16 JH |
823 | S_ithread_run, |
824 | (LPVOID)thread, | |
514612b7 | 825 | STACK_SIZE_PARAM_IS_A_RESERVATION, |
fc04eb16 | 826 | &thread->thr); |
82c40bf6 | 827 | #else |
fc04eb16 | 828 | { |
861d5cbe JH |
829 | STATIC pthread_attr_t attr; |
830 | STATIC int attr_inited = 0; | |
831 | STATIC int attr_joinable = PTHREAD_CREATE_JOINABLE; | |
fc04eb16 JH |
832 | if (! attr_inited) { |
833 | pthread_attr_init(&attr); | |
834 | attr_inited = 1; | |
835 | } | |
836 | ||
fa26028c | 837 | # ifdef PTHREAD_ATTR_SETDETACHSTATE |
fc04eb16 JH |
838 | /* Threads start out joinable */ |
839 | PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable); | |
fa26028c | 840 | # endif |
fc04eb16 | 841 | |
514612b7 | 842 | # ifdef _POSIX_THREAD_ATTR_STACKSIZE |
fc04eb16 | 843 | /* Set thread's stack size */ |
514612b7 JH |
844 | if (thread->stack_size > 0) { |
845 | rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size); | |
846 | } | |
3eb37d38 AB |
847 | # endif |
848 | ||
fc04eb16 JH |
849 | /* Create the thread */ |
850 | if (! rc_stack_size) { | |
851 | # ifdef OLD_PTHREADS_API | |
852 | rc_thread_create = pthread_create(&thread->thr, | |
853 | attr, | |
854 | S_ithread_run, | |
855 | (void *)thread); | |
856 | # else | |
857 | # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM) | |
858 | pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); | |
859 | # endif | |
860 | rc_thread_create = pthread_create(&thread->thr, | |
861 | &attr, | |
862 | S_ithread_run, | |
863 | (void *)thread); | |
19a077f6 | 864 | # endif |
fc04eb16 | 865 | } |
514612b7 | 866 | |
8264cf32 | 867 | #ifndef WIN32 |
b762d866 JW |
868 | /* Now it's safe to accept signals, since we're in our own interpreter's |
869 | * context and we have created the thread. | |
870 | */ | |
871 | S_set_sigmask(&thread->initial_sigmask); | |
8264cf32 | 872 | #endif |
b762d866 | 873 | |
514612b7 JH |
874 | # ifdef _POSIX_THREAD_ATTR_STACKSIZE |
875 | /* Try to get thread's actual stack size */ | |
876 | { | |
877 | size_t stacksize; | |
58a3a76c JH |
878 | #ifdef HPUX1020 |
879 | stacksize = pthread_attr_getstacksize(attr); | |
880 | #else | |
881 | if (! pthread_attr_getstacksize(&attr, &stacksize)) | |
882 | #endif | |
883 | if (stacksize > 0) { | |
514612b7 JH |
884 | thread->stack_size = (IV)stacksize; |
885 | } | |
514612b7 JH |
886 | } |
887 | # endif | |
fc04eb16 | 888 | } |
82c40bf6 | 889 | #endif |
bcd9ca9b | 890 | |
fc04eb16 | 891 | /* Check for errors */ |
d94006e8 | 892 | #ifdef WIN32 |
fc04eb16 | 893 | if (thread->handle == NULL) { |
d94006e8 | 894 | #else |
fc04eb16 | 895 | if (rc_stack_size || rc_thread_create) { |
d94006e8 | 896 | #endif |
9ca4d7fd | 897 | /* Must unlock mutex for destruct call */ |
5c6ff896 | 898 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
fc04eb16 | 899 | sv_2mortal(params); |
adc09a0e | 900 | thread->state |= PERL_ITHR_NONVIABLE; |
6ebc233e | 901 | S_ithread_free(aTHX_ thread); /* Releases MUTEX */ |
d94006e8 | 902 | #ifndef WIN32 |
514612b7 | 903 | if (ckWARN_d(WARN_THREADS)) { |
fea7688c | 904 | if (rc_stack_size) { |
514612b7 | 905 | Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size); |
fea7688c | 906 | } else { |
514612b7 | 907 | Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create); |
fea7688c | 908 | } |
514612b7 | 909 | } |
d94006e8 | 910 | #endif |
9ca4d7fd | 911 | return (NULL); |
fc04eb16 JH |
912 | } |
913 | ||
5c6ff896 | 914 | MY_POOL.running_threads++; |
fc04eb16 | 915 | sv_2mortal(params); |
9ca4d7fd | 916 | return (thread); |
68795e93 | 917 | } |
47ba8780 | 918 | |
73e09c8f | 919 | #endif /* USE_ITHREADS */ |
e1c44605 | 920 | |
fcea4b7c | 921 | |
fc04eb16 | 922 | MODULE = threads PACKAGE = threads PREFIX = ithread_ |
68795e93 | 923 | PROTOTYPES: DISABLE |
8222d950 | 924 | |
73e09c8f JH |
925 | #ifdef USE_ITHREADS |
926 | ||
68795e93 | 927 | void |
f4cc38af JH |
928 | ithread_create(...) |
929 | PREINIT: | |
930 | char *classname; | |
514612b7 | 931 | ithread *thread; |
f4cc38af JH |
932 | SV *function_to_call; |
933 | AV *params; | |
514612b7 JH |
934 | HV *specs; |
935 | IV stack_size; | |
9d9ff5b1 | 936 | int context; |
69a9b4b8 RGS |
937 | int exit_opt; |
938 | SV *thread_exit_only; | |
9d9ff5b1 | 939 | char *str; |
514612b7 | 940 | int idx; |
f4cc38af | 941 | int ii; |
5c6ff896 | 942 | dMY_POOL; |
f4cc38af | 943 | CODE: |
514612b7 | 944 | if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) { |
fea7688c | 945 | if (--items < 2) { |
18b9e6f5 | 946 | Perl_croak(aTHX_ "Usage: threads->create(\\%%specs, function, ...)"); |
fea7688c | 947 | } |
514612b7 JH |
948 | specs = (HV*)SvRV(ST(1)); |
949 | idx = 1; | |
950 | } else { | |
fea7688c | 951 | if (items < 2) { |
514612b7 | 952 | Perl_croak(aTHX_ "Usage: threads->create(function, ...)"); |
fea7688c | 953 | } |
514612b7 JH |
954 | specs = NULL; |
955 | idx = 0; | |
956 | } | |
f4cc38af | 957 | |
514612b7 JH |
958 | if (sv_isobject(ST(0))) { |
959 | /* $thr->create() */ | |
960 | classname = HvNAME(SvSTASH(SvRV(ST(0)))); | |
961 | thread = INT2PTR(ithread *, SvIV(SvRV(ST(0)))); | |
8718f9a1 | 962 | MUTEX_LOCK(&thread->mutex); |
514612b7 | 963 | stack_size = thread->stack_size; |
69a9b4b8 | 964 | exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY; |
8718f9a1 | 965 | MUTEX_UNLOCK(&thread->mutex); |
514612b7 JH |
966 | } else { |
967 | /* threads->create() */ | |
968 | classname = (char *)SvPV_nolen(ST(0)); | |
5c6ff896 | 969 | stack_size = MY_POOL.default_stack_size; |
8583b257 | 970 | thread_exit_only = get_sv("threads::thread_exit_only", GV_ADD); |
69a9b4b8 RGS |
971 | exit_opt = (SvTRUE(thread_exit_only)) |
972 | ? PERL_ITHR_THREAD_EXIT_ONLY : 0; | |
514612b7 JH |
973 | } |
974 | ||
975 | function_to_call = ST(idx+1); | |
976 | ||
9d9ff5b1 | 977 | context = -1; |
514612b7 JH |
978 | if (specs) { |
979 | /* stack_size */ | |
980 | if (hv_exists(specs, "stack", 5)) { | |
981 | stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0)); | |
982 | } else if (hv_exists(specs, "stacksize", 9)) { | |
983 | stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0)); | |
984 | } else if (hv_exists(specs, "stack_size", 10)) { | |
985 | stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0)); | |
986 | } | |
9d9ff5b1 JH |
987 | |
988 | /* context */ | |
989 | if (hv_exists(specs, "context", 7)) { | |
990 | str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0)); | |
991 | switch (*str) { | |
992 | case 'a': | |
993 | case 'A': | |
da140a40 JH |
994 | case 'l': |
995 | case 'L': | |
9d9ff5b1 JH |
996 | context = G_ARRAY; |
997 | break; | |
998 | case 's': | |
999 | case 'S': | |
1000 | context = G_SCALAR; | |
1001 | break; | |
1002 | case 'v': | |
1003 | case 'V': | |
1004 | context = G_VOID; | |
1005 | break; | |
1006 | default: | |
1007 | Perl_croak(aTHX_ "Invalid context: %s", str); | |
1008 | } | |
1009 | } else if (hv_exists(specs, "array", 5)) { | |
1010 | if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) { | |
1011 | context = G_ARRAY; | |
1012 | } | |
da140a40 JH |
1013 | } else if (hv_exists(specs, "list", 4)) { |
1014 | if (SvTRUE(*hv_fetch(specs, "list", 4, 0))) { | |
1015 | context = G_ARRAY; | |
1016 | } | |
9d9ff5b1 JH |
1017 | } else if (hv_exists(specs, "scalar", 6)) { |
1018 | if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) { | |
1019 | context = G_SCALAR; | |
1020 | } | |
1021 | } else if (hv_exists(specs, "void", 4)) { | |
1022 | if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) { | |
1023 | context = G_VOID; | |
1024 | } | |
1025 | } | |
69a9b4b8 RGS |
1026 | |
1027 | /* exit => thread_only */ | |
1028 | if (hv_exists(specs, "exit", 4)) { | |
1029 | str = (char *)SvPV_nolen(*hv_fetch(specs, "exit", 4, 0)); | |
1030 | exit_opt = (*str == 't' || *str == 'T') | |
1031 | ? PERL_ITHR_THREAD_EXIT_ONLY : 0; | |
1032 | } | |
9d9ff5b1 JH |
1033 | } |
1034 | if (context == -1) { | |
1035 | context = GIMME_V; /* Implicit context */ | |
1036 | } else { | |
1037 | context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID))); | |
514612b7 | 1038 | } |
f4cc38af JH |
1039 | |
1040 | /* Function args */ | |
1041 | params = newAV(); | |
1042 | if (items > 2) { | |
514612b7 JH |
1043 | for (ii=2; ii < items ; ii++) { |
1044 | av_push(params, SvREFCNT_inc(ST(idx+ii))); | |
f4cc38af JH |
1045 | } |
1046 | } | |
1047 | ||
1048 | /* Create thread */ | |
5c6ff896 | 1049 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
9ca4d7fd JH |
1050 | thread = S_ithread_create(aTHX_ function_to_call, |
1051 | stack_size, | |
1052 | context, | |
1053 | exit_opt, | |
1054 | newRV_noinc((SV*)params)); | |
1055 | if (! thread) { | |
1056 | XSRETURN_UNDEF; /* Mutex already unlocked */ | |
1057 | } | |
861d5cbe | 1058 | ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE)); |
adc09a0e | 1059 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
9ca4d7fd JH |
1060 | |
1061 | /* Let thread run */ | |
1062 | MUTEX_UNLOCK(&thread->mutex); | |
9ca4d7fd | 1063 | |
f4cc38af JH |
1064 | /* XSRETURN(1); - implied */ |
1065 | ||
8222d950 | 1066 | |
68795e93 | 1067 | void |
f4cc38af JH |
1068 | ithread_list(...) |
1069 | PREINIT: | |
1070 | char *classname; | |
fc04eb16 | 1071 | ithread *thread; |
f4cc38af JH |
1072 | int list_context; |
1073 | IV count = 0; | |
11db694d | 1074 | int want_running = 0; |
8718f9a1 | 1075 | int state; |
5c6ff896 | 1076 | dMY_POOL; |
f4cc38af JH |
1077 | PPCODE: |
1078 | /* Class method only */ | |
fea7688c | 1079 | if (SvROK(ST(0))) { |
ead32952 | 1080 | Perl_croak(aTHX_ "Usage: threads->list(...)"); |
fea7688c | 1081 | } |
f4cc38af JH |
1082 | classname = (char *)SvPV_nolen(ST(0)); |
1083 | ||
1084 | /* Calling context */ | |
1085 | list_context = (GIMME_V == G_ARRAY); | |
1086 | ||
ead32952 JH |
1087 | /* Running or joinable parameter */ |
1088 | if (items > 1) { | |
1089 | want_running = SvTRUE(ST(1)); | |
1090 | } | |
1091 | ||
f4cc38af | 1092 | /* Walk through threads list */ |
5c6ff896 JH |
1093 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
1094 | for (thread = MY_POOL.main_thread.next; | |
1095 | thread != &MY_POOL.main_thread; | |
fc04eb16 | 1096 | thread = thread->next) |
f4cc38af | 1097 | { |
8718f9a1 JH |
1098 | MUTEX_LOCK(&thread->mutex); |
1099 | state = thread->state; | |
1100 | MUTEX_UNLOCK(&thread->mutex); | |
1101 | ||
f4cc38af | 1102 | /* Ignore detached or joined threads */ |
8718f9a1 | 1103 | if (state & PERL_ITHR_UNCALLABLE) { |
f4cc38af JH |
1104 | continue; |
1105 | } | |
ead32952 JH |
1106 | |
1107 | /* Filter per parameter */ | |
1108 | if (items > 1) { | |
1109 | if (want_running) { | |
8718f9a1 | 1110 | if (state & PERL_ITHR_FINISHED) { |
ead32952 JH |
1111 | continue; /* Not running */ |
1112 | } | |
1113 | } else { | |
8718f9a1 | 1114 | if (! (state & PERL_ITHR_FINISHED)) { |
ead32952 JH |
1115 | continue; /* Still running - not joinable yet */ |
1116 | } | |
1117 | } | |
1118 | } | |
1119 | ||
f4cc38af JH |
1120 | /* Push object on stack if list context */ |
1121 | if (list_context) { | |
861d5cbe | 1122 | XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE))); |
f4cc38af JH |
1123 | } |
1124 | count++; | |
1125 | } | |
5c6ff896 | 1126 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
f4cc38af JH |
1127 | /* If scalar context, send back count */ |
1128 | if (! list_context) { | |
1129 | XSRETURN_IV(count); | |
1130 | } | |
678a9b6c AB |
1131 | |
1132 | ||
1133 | void | |
f4cc38af JH |
1134 | ithread_self(...) |
1135 | PREINIT: | |
1136 | char *classname; | |
fcea4b7c | 1137 | ithread *thread; |
f4cc38af JH |
1138 | CODE: |
1139 | /* Class method only */ | |
11db694d | 1140 | if ((items != 1) || SvROK(ST(0))) { |
f4cc38af | 1141 | Perl_croak(aTHX_ "Usage: threads->self()"); |
fea7688c | 1142 | } |
f4cc38af JH |
1143 | classname = (char *)SvPV_nolen(ST(0)); |
1144 | ||
fcea4b7c JH |
1145 | thread = S_ithread_get(aTHX); |
1146 | ||
861d5cbe | 1147 | ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)); |
f4cc38af | 1148 | /* XSRETURN(1); - implied */ |
47ba8780 | 1149 | |
47ba8780 AB |
1150 | |
1151 | void | |
f4cc38af JH |
1152 | ithread_tid(...) |
1153 | PREINIT: | |
1154 | ithread *thread; | |
1155 | CODE: | |
11db694d | 1156 | PERL_UNUSED_VAR(items); |
861d5cbe | 1157 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
f4cc38af JH |
1158 | XST_mUV(0, thread->tid); |
1159 | /* XSRETURN(1); - implied */ | |
1160 | ||
e1c44605 | 1161 | |
f9dff5f5 | 1162 | void |
f4cc38af JH |
1163 | ithread_join(...) |
1164 | PREINIT: | |
fcea4b7c | 1165 | ithread *thread; |
8718f9a1 | 1166 | ithread *current_thread; |
fcea4b7c | 1167 | int join_err; |
6ebc233e | 1168 | AV *params = NULL; |
f4cc38af JH |
1169 | int len; |
1170 | int ii; | |
12701bb8 | 1171 | #ifndef WIN32 |
8718f9a1 | 1172 | int rc_join; |
fcea4b7c JH |
1173 | void *retval; |
1174 | #endif | |
5c6ff896 | 1175 | dMY_POOL; |
f4cc38af JH |
1176 | PPCODE: |
1177 | /* Object method only */ | |
11db694d | 1178 | if ((items != 1) || ! sv_isobject(ST(0))) { |
f4cc38af | 1179 | Perl_croak(aTHX_ "Usage: $thr->join()"); |
fea7688c | 1180 | } |
f4cc38af | 1181 | |
8718f9a1 | 1182 | /* Check if the thread is joinable and not ourselves */ |
861d5cbe | 1183 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
8718f9a1 JH |
1184 | current_thread = S_ithread_get(aTHX); |
1185 | ||
1186 | MUTEX_LOCK(&thread->mutex); | |
1187 | if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) { | |
1188 | MUTEX_UNLOCK(&thread->mutex); | |
1189 | Perl_croak(aTHX_ (join_err & PERL_ITHR_DETACHED) | |
1190 | ? "Cannot join a detached thread" | |
1191 | : "Thread already joined"); | |
1192 | } else if (thread->tid == current_thread->tid) { | |
1193 | MUTEX_UNLOCK(&thread->mutex); | |
1194 | Perl_croak(aTHX_ "Cannot join self"); | |
fcea4b7c JH |
1195 | } |
1196 | ||
8718f9a1 JH |
1197 | /* Mark as joined */ |
1198 | thread->state |= PERL_ITHR_JOINED; | |
1199 | MUTEX_UNLOCK(&thread->mutex); | |
1200 | ||
1201 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); | |
1202 | MY_POOL.joinable_threads--; | |
1203 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); | |
1204 | ||
fcea4b7c JH |
1205 | /* Join the thread */ |
1206 | #ifdef WIN32 | |
8718f9a1 JH |
1207 | if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) { |
1208 | /* Timeout/abandonment unexpected here; check $^E */ | |
1209 | Perl_croak(aTHX_ "PANIC: underlying join failed"); | |
1210 | }; | |
fcea4b7c | 1211 | #else |
8718f9a1 JH |
1212 | if ((rc_join = pthread_join(thread->thr, &retval)) != 0) { |
1213 | /* In progress/deadlock/unknown unexpected here; check $! */ | |
1214 | errno = rc_join; | |
1215 | Perl_croak(aTHX_ "PANIC: underlying join failed"); | |
1216 | }; | |
fcea4b7c JH |
1217 | #endif |
1218 | ||
1219 | MUTEX_LOCK(&thread->mutex); | |
fcea4b7c | 1220 | /* Get the return value from the call_sv */ |
955c272e | 1221 | /* Objects do not survive this process - FIXME */ |
7df0357e | 1222 | if ((thread->gimme & G_WANT) != G_VOID) { |
fcea4b7c JH |
1223 | AV *params_copy; |
1224 | PerlInterpreter *other_perl; | |
1225 | CLONE_PARAMS clone_params; | |
fcea4b7c JH |
1226 | |
1227 | params_copy = (AV *)SvRV(thread->params); | |
1228 | other_perl = thread->interp; | |
1229 | clone_params.stashes = newAV(); | |
1230 | clone_params.flags = CLONEf_JOIN_IN; | |
1231 | PL_ptr_table = ptr_table_new(); | |
fcea4b7c JH |
1232 | S_ithread_set(aTHX_ thread); |
1233 | /* Ensure 'meaningful' addresses retain their meaning */ | |
1234 | ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef); | |
1235 | ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no); | |
1236 | ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes); | |
1237 | params = (AV *)sv_dup((SV*)params_copy, &clone_params); | |
1238 | S_ithread_set(aTHX_ current_thread); | |
1239 | SvREFCNT_dec(clone_params.stashes); | |
d4315dd6 | 1240 | SvREFCNT_inc_void(params); |
fcea4b7c JH |
1241 | ptr_table_free(PL_ptr_table); |
1242 | PL_ptr_table = NULL; | |
1243 | } | |
1244 | ||
955c272e JH |
1245 | /* If thread didn't die, then we can free its interpreter */ |
1246 | if (! (thread->state & PERL_ITHR_DIED)) { | |
1247 | S_ithread_clear(aTHX_ thread); | |
1248 | } | |
6ebc233e | 1249 | S_ithread_free(aTHX_ thread); /* Releases MUTEX */ |
955c272e | 1250 | |
fcea4b7c | 1251 | /* If no return values, then just return */ |
f4cc38af JH |
1252 | if (! params) { |
1253 | XSRETURN_UNDEF; | |
1254 | } | |
1255 | ||
1256 | /* Put return values on stack */ | |
1257 | len = (int)AvFILL(params); | |
1258 | for (ii=0; ii <= len; ii++) { | |
1259 | SV* param = av_shift(params); | |
1260 | XPUSHs(sv_2mortal(param)); | |
1261 | } | |
1262 | ||
1263 | /* Free return value array */ | |
1264 | SvREFCNT_dec(params); | |
1265 | ||
1266 | ||
1267 | void | |
1268 | ithread_yield(...) | |
1269 | CODE: | |
11db694d | 1270 | PERL_UNUSED_VAR(items); |
f4cc38af JH |
1271 | YIELD; |
1272 | ||
1273 | ||
1274 | void | |
1275 | ithread_detach(...) | |
1276 | PREINIT: | |
1277 | ithread *thread; | |
fcea4b7c | 1278 | int detach_err; |
5c6ff896 | 1279 | dMY_POOL; |
f4cc38af | 1280 | CODE: |
11db694d JH |
1281 | PERL_UNUSED_VAR(items); |
1282 | ||
fcea4b7c | 1283 | /* Detach the thread */ |
8718f9a1 | 1284 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
5c6ff896 | 1285 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
9ca4d7fd | 1286 | MUTEX_LOCK(&thread->mutex); |
8718f9a1 JH |
1287 | if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) { |
1288 | /* Thread is detachable */ | |
1289 | thread->state |= PERL_ITHR_DETACHED; | |
fcea4b7c | 1290 | #ifdef WIN32 |
8718f9a1 | 1291 | /* Windows has no 'detach thread' function */ |
fcea4b7c | 1292 | #else |
8718f9a1 | 1293 | PERL_THREAD_DETACH(thread->thr); |
fcea4b7c | 1294 | #endif |
8718f9a1 JH |
1295 | if (thread->state & PERL_ITHR_FINISHED) { |
1296 | MY_POOL.joinable_threads--; | |
1297 | } else { | |
1298 | MY_POOL.running_threads--; | |
1299 | MY_POOL.detached_threads++; | |
1300 | } | |
4dcb9e53 | 1301 | } |
adc09a0e | 1302 | MUTEX_UNLOCK(&thread->mutex); |
5c6ff896 | 1303 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
4dcb9e53 | 1304 | |
8718f9a1 JH |
1305 | if (detach_err) { |
1306 | Perl_croak(aTHX_ (detach_err & PERL_ITHR_DETACHED) | |
1307 | ? "Thread already detached" | |
1308 | : "Cannot detach a joined thread"); | |
1309 | } | |
1310 | ||
955c272e JH |
1311 | /* If thread is finished and didn't die, |
1312 | * then we can free its interpreter */ | |
1313 | MUTEX_LOCK(&thread->mutex); | |
1314 | if ((thread->state & PERL_ITHR_FINISHED) && | |
1315 | ! (thread->state & PERL_ITHR_DIED)) | |
1316 | { | |
1317 | S_ithread_clear(aTHX_ thread); | |
1318 | } | |
6ebc233e | 1319 | S_ithread_free(aTHX_ thread); /* Releases MUTEX */ |
f4cc38af | 1320 | |
47ba8780 AB |
1321 | |
1322 | void | |
c0003851 JH |
1323 | ithread_kill(...) |
1324 | PREINIT: | |
1325 | ithread *thread; | |
1326 | char *sig_name; | |
1327 | IV signal; | |
1328 | CODE: | |
1329 | /* Must have safe signals */ | |
fea7688c | 1330 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) { |
4dcb9e53 | 1331 | Perl_croak(aTHX_ "Cannot signal threads without safe signals"); |
fea7688c | 1332 | } |
c0003851 JH |
1333 | |
1334 | /* Object method only */ | |
11db694d | 1335 | if ((items != 2) || ! sv_isobject(ST(0))) { |
c0003851 | 1336 | Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')"); |
fea7688c | 1337 | } |
c0003851 | 1338 | |
c0003851 JH |
1339 | /* Get signal */ |
1340 | sig_name = SvPV_nolen(ST(1)); | |
1341 | if (isALPHA(*sig_name)) { | |
fea7688c | 1342 | if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') { |
c0003851 | 1343 | sig_name += 3; |
fea7688c JH |
1344 | } |
1345 | if ((signal = whichsig(sig_name)) < 0) { | |
c0003851 | 1346 | Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name); |
fea7688c JH |
1347 | } |
1348 | } else { | |
c0003851 | 1349 | signal = SvIV(ST(1)); |
fea7688c | 1350 | } |
c0003851 JH |
1351 | |
1352 | /* Set the signal for the thread */ | |
861d5cbe | 1353 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
4dcb9e53 | 1354 | MUTEX_LOCK(&thread->mutex); |
3ceb02cd | 1355 | if (thread->interp) { |
c0003851 JH |
1356 | dTHXa(thread->interp); |
1357 | PL_psig_pend[signal]++; | |
1358 | PL_sig_pending = 1; | |
1359 | } | |
4dcb9e53 | 1360 | MUTEX_UNLOCK(&thread->mutex); |
c0003851 JH |
1361 | |
1362 | /* Return the thread to allow for method chaining */ | |
1363 | ST(0) = ST(0); | |
1364 | /* XSRETURN(1); - implied */ | |
1365 | ||
1366 | ||
1367 | void | |
f4cc38af JH |
1368 | ithread_DESTROY(...) |
1369 | CODE: | |
11db694d | 1370 | PERL_UNUSED_VAR(items); |
fcea4b7c | 1371 | sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar); |
f4cc38af JH |
1372 | |
1373 | ||
1374 | void | |
1375 | ithread_equal(...) | |
fc04eb16 JH |
1376 | PREINIT: |
1377 | int are_equal = 0; | |
f4cc38af | 1378 | CODE: |
11db694d JH |
1379 | PERL_UNUSED_VAR(items); |
1380 | ||
fc04eb16 | 1381 | /* Compares TIDs to determine thread equality */ |
f4cc38af JH |
1382 | if (sv_isobject(ST(0)) && sv_isobject(ST(1))) { |
1383 | ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0)))); | |
1384 | ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1)))); | |
fc04eb16 JH |
1385 | are_equal = (thr1->tid == thr2->tid); |
1386 | } | |
1387 | if (are_equal) { | |
1388 | XST_mYES(0); | |
f4cc38af | 1389 | } else { |
fc04eb16 | 1390 | /* Return 0 on false for backward compatibility */ |
f4cc38af JH |
1391 | XST_mIV(0, 0); |
1392 | } | |
1393 | /* XSRETURN(1); - implied */ | |
1394 | ||
47ba8780 | 1395 | |
47ba8780 | 1396 | void |
f4cc38af JH |
1397 | ithread_object(...) |
1398 | PREINIT: | |
1399 | char *classname; | |
1400 | UV tid; | |
fc04eb16 | 1401 | ithread *thread; |
8718f9a1 | 1402 | int state; |
9ca4d7fd | 1403 | int have_obj = 0; |
5c6ff896 | 1404 | dMY_POOL; |
f4cc38af JH |
1405 | CODE: |
1406 | /* Class method only */ | |
fea7688c | 1407 | if (SvROK(ST(0))) { |
f4cc38af | 1408 | Perl_croak(aTHX_ "Usage: threads->object($tid)"); |
fea7688c | 1409 | } |
f4cc38af JH |
1410 | classname = (char *)SvPV_nolen(ST(0)); |
1411 | ||
1412 | if ((items < 2) || ! SvOK(ST(1))) { | |
1413 | XSRETURN_UNDEF; | |
1414 | } | |
1415 | ||
fc04eb16 | 1416 | /* threads->object($tid) */ |
f4cc38af JH |
1417 | tid = SvUV(ST(1)); |
1418 | ||
1419 | /* Walk through threads list */ | |
5c6ff896 JH |
1420 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); |
1421 | for (thread = MY_POOL.main_thread.next; | |
1422 | thread != &MY_POOL.main_thread; | |
fc04eb16 | 1423 | thread = thread->next) |
f4cc38af | 1424 | { |
9ca4d7fd JH |
1425 | /* Look for TID */ |
1426 | if (thread->tid == tid) { | |
1427 | /* Ignore if detached or joined */ | |
8718f9a1 JH |
1428 | MUTEX_LOCK(&thread->mutex); |
1429 | state = thread->state; | |
1430 | MUTEX_UNLOCK(&thread->mutex); | |
1431 | if (! (state & PERL_ITHR_UNCALLABLE)) { | |
9ca4d7fd | 1432 | /* Put object on stack */ |
861d5cbe | 1433 | ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)); |
9ca4d7fd JH |
1434 | have_obj = 1; |
1435 | } | |
1436 | break; | |
f4cc38af | 1437 | } |
f4cc38af | 1438 | } |
5c6ff896 | 1439 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); |
9ca4d7fd JH |
1440 | |
1441 | if (! have_obj) { | |
f4cc38af JH |
1442 | XSRETURN_UNDEF; |
1443 | } | |
1444 | /* XSRETURN(1); - implied */ | |
1445 | ||
1446 | ||
1447 | void | |
1448 | ithread__handle(...); | |
1449 | PREINIT: | |
1450 | ithread *thread; | |
1451 | CODE: | |
11db694d | 1452 | PERL_UNUSED_VAR(items); |
861d5cbe | 1453 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
f4cc38af | 1454 | #ifdef WIN32 |
fcea4b7c | 1455 | XST_mUV(0, PTR2UV(&thread->handle)); |
f4cc38af | 1456 | #else |
75ba4ae2 | 1457 | XST_mUV(0, PTR2UV(&thread->thr)); |
f4cc38af JH |
1458 | #endif |
1459 | /* XSRETURN(1); - implied */ | |
68795e93 | 1460 | |
514612b7 JH |
1461 | |
1462 | void | |
1463 | ithread_get_stack_size(...) | |
1464 | PREINIT: | |
1465 | IV stack_size; | |
5c6ff896 | 1466 | dMY_POOL; |
514612b7 | 1467 | CODE: |
11db694d | 1468 | PERL_UNUSED_VAR(items); |
514612b7 JH |
1469 | if (sv_isobject(ST(0))) { |
1470 | /* $thr->get_stack_size() */ | |
1471 | ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0)))); | |
1472 | stack_size = thread->stack_size; | |
1473 | } else { | |
1474 | /* threads->get_stack_size() */ | |
5c6ff896 | 1475 | stack_size = MY_POOL.default_stack_size; |
514612b7 JH |
1476 | } |
1477 | XST_mIV(0, stack_size); | |
1478 | /* XSRETURN(1); - implied */ | |
1479 | ||
1480 | ||
1481 | void | |
1482 | ithread_set_stack_size(...) | |
1483 | PREINIT: | |
1484 | IV old_size; | |
5c6ff896 | 1485 | dMY_POOL; |
514612b7 | 1486 | CODE: |
fea7688c | 1487 | if (items != 2) { |
514612b7 | 1488 | Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)"); |
fea7688c JH |
1489 | } |
1490 | if (sv_isobject(ST(0))) { | |
514612b7 | 1491 | Perl_croak(aTHX_ "Cannot change stack size of an existing thread"); |
fea7688c | 1492 | } |
6ebc233e RGS |
1493 | if (! looks_like_number(ST(1))) { |
1494 | Perl_croak(aTHX_ "Stack size must be numeric"); | |
1495 | } | |
514612b7 | 1496 | |
5c6ff896 JH |
1497 | old_size = MY_POOL.default_stack_size; |
1498 | MY_POOL.default_stack_size = S_good_stack_size(aTHX_ SvIV(ST(1))); | |
514612b7 JH |
1499 | XST_mIV(0, old_size); |
1500 | /* XSRETURN(1); - implied */ | |
1501 | ||
ead32952 JH |
1502 | |
1503 | void | |
1504 | ithread_is_running(...) | |
1505 | PREINIT: | |
1506 | ithread *thread; | |
1507 | CODE: | |
1508 | /* Object method only */ | |
11db694d | 1509 | if ((items != 1) || ! sv_isobject(ST(0))) { |
ead32952 | 1510 | Perl_croak(aTHX_ "Usage: $thr->is_running()"); |
fea7688c | 1511 | } |
ead32952 JH |
1512 | |
1513 | thread = INT2PTR(ithread *, SvIV(SvRV(ST(0)))); | |
8718f9a1 | 1514 | MUTEX_LOCK(&thread->mutex); |
ead32952 | 1515 | ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes; |
8718f9a1 | 1516 | MUTEX_UNLOCK(&thread->mutex); |
ead32952 JH |
1517 | /* XSRETURN(1); - implied */ |
1518 | ||
1519 | ||
1520 | void | |
1521 | ithread_is_detached(...) | |
1522 | PREINIT: | |
1523 | ithread *thread; | |
1524 | CODE: | |
11db694d | 1525 | PERL_UNUSED_VAR(items); |
861d5cbe | 1526 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
8718f9a1 | 1527 | MUTEX_LOCK(&thread->mutex); |
ead32952 | 1528 | ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no; |
8718f9a1 | 1529 | MUTEX_UNLOCK(&thread->mutex); |
ead32952 JH |
1530 | /* XSRETURN(1); - implied */ |
1531 | ||
1532 | ||
1533 | void | |
1534 | ithread_is_joinable(...) | |
1535 | PREINIT: | |
1536 | ithread *thread; | |
1537 | CODE: | |
1538 | /* Object method only */ | |
11db694d | 1539 | if ((items != 1) || ! sv_isobject(ST(0))) { |
ead32952 | 1540 | Perl_croak(aTHX_ "Usage: $thr->is_joinable()"); |
fea7688c | 1541 | } |
ead32952 JH |
1542 | |
1543 | thread = INT2PTR(ithread *, SvIV(SvRV(ST(0)))); | |
1544 | MUTEX_LOCK(&thread->mutex); | |
1545 | ST(0) = ((thread->state & PERL_ITHR_FINISHED) && | |
8718f9a1 | 1546 | ! (thread->state & PERL_ITHR_UNCALLABLE)) |
ead32952 JH |
1547 | ? &PL_sv_yes : &PL_sv_no; |
1548 | MUTEX_UNLOCK(&thread->mutex); | |
1549 | /* XSRETURN(1); - implied */ | |
1550 | ||
1551 | ||
1552 | void | |
1553 | ithread_wantarray(...) | |
1554 | PREINIT: | |
1555 | ithread *thread; | |
1556 | CODE: | |
11db694d | 1557 | PERL_UNUSED_VAR(items); |
861d5cbe | 1558 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
7df0357e NC |
1559 | ST(0) = ((thread->gimme & G_WANT) == G_ARRAY) ? &PL_sv_yes : |
1560 | ((thread->gimme & G_WANT) == G_VOID) ? &PL_sv_undef | |
7ef93cb2 | 1561 | /* G_SCALAR */ : &PL_sv_no; |
ead32952 JH |
1562 | /* XSRETURN(1); - implied */ |
1563 | ||
69a9b4b8 RGS |
1564 | |
1565 | void | |
1566 | ithread_set_thread_exit_only(...) | |
1567 | PREINIT: | |
1568 | ithread *thread; | |
1569 | CODE: | |
fea7688c | 1570 | if (items != 2) { |
69a9b4b8 | 1571 | Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)"); |
fea7688c | 1572 | } |
861d5cbe | 1573 | thread = S_SV_to_ithread(aTHX_ ST(0)); |
69a9b4b8 RGS |
1574 | MUTEX_LOCK(&thread->mutex); |
1575 | if (SvTRUE(ST(1))) { | |
1576 | thread->state |= PERL_ITHR_THREAD_EXIT_ONLY; | |
1577 | } else { | |
1578 | thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY; | |
1579 | } | |
1580 | MUTEX_UNLOCK(&thread->mutex); | |
1581 | ||
955c272e JH |
1582 | |
1583 | void | |
1584 | ithread_error(...) | |
1585 | PREINIT: | |
1586 | ithread *thread; | |
1587 | SV *err = NULL; | |
1588 | CODE: | |
1589 | /* Object method only */ | |
1590 | if ((items != 1) || ! sv_isobject(ST(0))) { | |
1591 | Perl_croak(aTHX_ "Usage: $thr->err()"); | |
1592 | } | |
1593 | ||
1594 | thread = INT2PTR(ithread *, SvIV(SvRV(ST(0)))); | |
1595 | MUTEX_LOCK(&thread->mutex); | |
1596 | ||
1597 | /* If thread died, then clone the error into the calling thread */ | |
1598 | if (thread->state & PERL_ITHR_DIED) { | |
1599 | PerlInterpreter *other_perl; | |
1600 | CLONE_PARAMS clone_params; | |
1601 | ithread *current_thread; | |
1602 | ||
1603 | other_perl = thread->interp; | |
1604 | clone_params.stashes = newAV(); | |
1605 | clone_params.flags = CLONEf_JOIN_IN; | |
1606 | PL_ptr_table = ptr_table_new(); | |
1607 | current_thread = S_ithread_get(aTHX); | |
1608 | S_ithread_set(aTHX_ thread); | |
1609 | /* Ensure 'meaningful' addresses retain their meaning */ | |
1610 | ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef); | |
1611 | ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no); | |
1612 | ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes); | |
1613 | err = sv_dup(thread->err, &clone_params); | |
1614 | S_ithread_set(aTHX_ current_thread); | |
1615 | SvREFCNT_dec(clone_params.stashes); | |
1616 | SvREFCNT_inc_void(err); | |
1617 | /* If error was an object, bless it into the correct class */ | |
1618 | if (thread->err_class) { | |
1619 | sv_bless(err, gv_stashpv(thread->err_class, 1)); | |
1620 | } | |
1621 | ptr_table_free(PL_ptr_table); | |
1622 | PL_ptr_table = NULL; | |
1623 | } | |
1624 | ||
1625 | MUTEX_UNLOCK(&thread->mutex); | |
1626 | ||
1627 | if (! err) { | |
1628 | XSRETURN_UNDEF; | |
1629 | } | |
1630 | ||
1631 | ST(0) = sv_2mortal(err); | |
1632 | /* XSRETURN(1); - implied */ | |
1633 | ||
1634 | ||
73e09c8f JH |
1635 | #endif /* USE_ITHREADS */ |
1636 | ||
fc04eb16 | 1637 | |
68795e93 NIS |
1638 | BOOT: |
1639 | { | |
73e09c8f | 1640 | #ifdef USE_ITHREADS |
5c6ff896 JH |
1641 | SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, |
1642 | sizeof(MY_POOL_KEY)-1, TRUE); | |
1643 | my_pool_t *my_poolp = (my_pool_t*)SvPVX(newSV(sizeof(my_pool_t)-1)); | |
1644 | ||
fc04eb16 JH |
1645 | MY_CXT_INIT; |
1646 | ||
5c6ff896 JH |
1647 | Zero(my_poolp, 1, my_pool_t); |
1648 | sv_setuv(my_pool_sv, PTR2UV(my_poolp)); | |
1649 | ||
fc04eb16 | 1650 | PL_perl_destruct_level = 2; |
5c6ff896 JH |
1651 | MUTEX_INIT(&MY_POOL.create_destruct_mutex); |
1652 | MUTEX_LOCK(&MY_POOL.create_destruct_mutex); | |
fc04eb16 JH |
1653 | |
1654 | PL_threadhook = &Perl_ithread_hook; | |
1655 | ||
5c6ff896 JH |
1656 | MY_POOL.tid_counter = 1; |
1657 | # ifdef THREAD_CREATE_NEEDS_STACK | |
1658 | MY_POOL.default_stack_size = THREAD_CREATE_NEEDS_STACK; | |
1659 | # endif | |
1660 | ||
c372d929 JH |
1661 | /* The 'main' thread is thread 0. |
1662 | * It is detached (unjoinable) and immortal. | |
1663 | */ | |
fc04eb16 | 1664 | |
5c6ff896 | 1665 | MUTEX_INIT(&MY_POOL.main_thread.mutex); |
fc04eb16 JH |
1666 | |
1667 | /* Head of the threads list */ | |
5c6ff896 JH |
1668 | MY_POOL.main_thread.next = &MY_POOL.main_thread; |
1669 | MY_POOL.main_thread.prev = &MY_POOL.main_thread; | |
fc04eb16 | 1670 | |
5c6ff896 | 1671 | MY_POOL.main_thread.count = 1; /* Immortal */ |
fc04eb16 | 1672 | |
5c6ff896 JH |
1673 | MY_POOL.main_thread.interp = aTHX; |
1674 | MY_POOL.main_thread.state = PERL_ITHR_DETACHED; /* Detached */ | |
1675 | MY_POOL.main_thread.stack_size = MY_POOL.default_stack_size; | |
fc04eb16 | 1676 | # ifdef WIN32 |
5c6ff896 | 1677 | MY_POOL.main_thread.thr = GetCurrentThreadId(); |
fc04eb16 | 1678 | # else |
5c6ff896 | 1679 | MY_POOL.main_thread.thr = pthread_self(); |
fc04eb16 JH |
1680 | # endif |
1681 | ||
5c6ff896 JH |
1682 | S_ithread_set(aTHX_ &MY_POOL.main_thread); |
1683 | MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex); | |
73e09c8f | 1684 | #endif /* USE_ITHREADS */ |
68795e93 | 1685 | } |