Commit | Line | Data |
68795e93 |
1 | #define PERL_NO_GET_CONTEXT |
2 | #include "EXTERN.h" |
3 | #include "perl.h" |
4 | #include "XSUB.h" |
5 | |
73e09c8f |
6 | #ifdef USE_ITHREADS |
7 | |
68795e93 |
8 | #ifdef WIN32 |
9 | #include <windows.h> |
10 | #include <win32thread.h> |
11 | #define PERL_THREAD_SETSPECIFIC(k,v) TlsSetValue(k,v) |
12 | #define PERL_THREAD_GETSPECIFIC(k,v) v = TlsGetValue(k) |
13 | #define PERL_THREAD_ALLOC_SPECIFIC(k) \ |
14 | STMT_START {\ |
15 | if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\ |
16 | PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\ |
17 | exit(1);\ |
18 | }\ |
19 | } STMT_END |
20 | #else |
21 | #include <pthread.h> |
22 | #include <thread.h> |
23 | |
24 | #define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v) |
25 | #ifdef OLD_PTHREADS_API |
26 | #define PERL_THREAD_DETACH(t) pthread_detach(&(t)) |
27 | #define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v) |
28 | #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\ |
29 | if(pthread_keycreate(&(k),0)) {\ |
30 | PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\ |
31 | exit(1);\ |
32 | }\ |
33 | } STMT_END |
34 | #else |
35 | #define PERL_THREAD_DETACH(t) pthread_detach((t)) |
36 | #define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k) |
37 | #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\ |
38 | if(pthread_key_create(&(k),0)) {\ |
39 | PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\ |
40 | exit(1);\ |
41 | }\ |
42 | } STMT_END |
43 | #endif |
44 | #endif |
45 | |
62375a60 |
46 | /* Values for 'state' member */ |
47 | #define PERL_ITHR_JOINABLE 0 |
48 | #define PERL_ITHR_DETACHED 1 |
49 | #define PERL_ITHR_FINISHED 4 |
50 | #define PERL_ITHR_JOINED 2 |
51 | |
68795e93 |
52 | typedef struct ithread_s { |
53 | struct ithread_s *next; /* next thread in the list */ |
54 | struct ithread_s *prev; /* prev thread in the list */ |
55 | PerlInterpreter *interp; /* The threads interpreter */ |
56 | I32 tid; /* threads module's thread id */ |
57 | perl_mutex mutex; /* mutex for updating things in this struct */ |
58 | I32 count; /* how many SVs have a reference to us */ |
62375a60 |
59 | signed char state; /* are we detached ? */ |
a446a88f |
60 | int gimme; /* Context of create */ |
68795e93 |
61 | SV* init_function; /* Code to run */ |
62 | SV* params; /* args to pass function */ |
63 | #ifdef WIN32 |
64 | DWORD thr; /* OS's idea if thread id */ |
65 | HANDLE handle; /* OS's waitable handle */ |
66 | #else |
67 | pthread_t thr; /* OS's handle for the thread */ |
68 | #endif |
69 | } ithread; |
70 | |
71 | ithread *threads; |
72 | |
73 | /* Macros to supply the aTHX_ in an embed.h like manner */ |
74 | #define ithread_join(thread) Perl_ithread_join(aTHX_ thread) |
75 | #define ithread_DESTROY(thread) Perl_ithread_DESTROY(aTHX_ thread) |
76 | #define ithread_CLONE(thread) Perl_ithread_CLONE(aTHX_ thread) |
77 | #define ithread_detach(thread) Perl_ithread_detach(aTHX_ thread) |
78 | #define ithread_tid(thread) ((thread)->tid) |
f9dff5f5 |
79 | #define ithread_yield(thread) (YIELD); |
68795e93 |
80 | |
58c2ef19 |
81 | static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/ |
68795e93 |
82 | |
83 | I32 tid_counter = 0; |
62375a60 |
84 | I32 known_threads = 0; |
58c2ef19 |
85 | I32 active_threads = 0; |
68795e93 |
86 | perl_key self_key; |
87 | |
88 | /* |
89 | * Clear up after thread is done with |
90 | */ |
91 | void |
62375a60 |
92 | Perl_ithread_destruct (pTHX_ ithread* thread, const char *why) |
68795e93 |
93 | { |
94 | MUTEX_LOCK(&thread->mutex); |
62375a60 |
95 | if (!thread->next) { |
96 | Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why); |
97 | } |
68795e93 |
98 | if (thread->count != 0) { |
99 | MUTEX_UNLOCK(&thread->mutex); |
d1400e48 |
100 | return; |
68795e93 |
101 | } |
58c2ef19 |
102 | MUTEX_LOCK(&create_destruct_mutex); |
68795e93 |
103 | /* Remove from circular list of threads */ |
104 | if (thread->next == thread) { |
105 | /* last one should never get here ? */ |
106 | threads = NULL; |
107 | } |
108 | else { |
f42ad631 |
109 | thread->next->prev = thread->prev; |
110 | thread->prev->next = thread->next; |
68795e93 |
111 | if (threads == thread) { |
112 | threads = thread->next; |
113 | } |
62375a60 |
114 | thread->next = NULL; |
115 | thread->prev = NULL; |
68795e93 |
116 | } |
62375a60 |
117 | known_threads--; |
118 | assert( known_threads >= 0 ); |
ba14dd9a |
119 | #if 0 |
62375a60 |
120 | Perl_warn(aTHX_ "destruct %d @ %p by %p now %d", |
121 | thread->tid,thread->interp,aTHX, known_threads); |
ba14dd9a |
122 | #endif |
62375a60 |
123 | MUTEX_UNLOCK(&create_destruct_mutex); |
124 | /* Thread is now disowned */ |
c2f2a82b |
125 | |
126 | if(thread->interp) { |
1c3adb19 |
127 | dTHXa(thread->interp); |
c2f2a82b |
128 | ithread* current_thread; |
68795e93 |
129 | PERL_SET_CONTEXT(thread->interp); |
c2f2a82b |
130 | PERL_THREAD_GETSPECIFIC(self_key,current_thread); |
131 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
3b1c3273 |
132 | |
133 | |
134 | |
1c3adb19 |
135 | SvREFCNT_dec(thread->params); |
3b1c3273 |
136 | |
137 | |
138 | |
1c3adb19 |
139 | thread->params = Nullsv; |
c2f2a82b |
140 | perl_destruct(thread->interp); |
141 | perl_free(thread->interp); |
68795e93 |
142 | thread->interp = NULL; |
c2f2a82b |
143 | PERL_THREAD_SETSPECIFIC(self_key,current_thread); |
144 | |
68795e93 |
145 | } |
d1400e48 |
146 | MUTEX_UNLOCK(&thread->mutex); |
1c3adb19 |
147 | MUTEX_DESTROY(&thread->mutex); |
148 | PerlMemShared_free(thread); |
1d784c90 |
149 | |
64821230 |
150 | PERL_SET_CONTEXT(aTHX); |
68795e93 |
151 | } |
152 | |
62375a60 |
153 | int |
154 | Perl_ithread_hook(pTHX) |
155 | { |
156 | int veto_cleanup = 0; |
157 | MUTEX_LOCK(&create_destruct_mutex); |
158 | if (aTHX == PL_curinterp && active_threads != 1) { |
c133c03f |
159 | Perl_warn(aTHX_ "A thread exited while %" IVdf " other threads were still running", |
436c6dd3 |
160 | (IV)active_threads); |
62375a60 |
161 | veto_cleanup = 1; |
162 | } |
163 | MUTEX_UNLOCK(&create_destruct_mutex); |
164 | return veto_cleanup; |
165 | } |
166 | |
167 | void |
168 | Perl_ithread_detach(pTHX_ ithread *thread) |
169 | { |
170 | MUTEX_LOCK(&thread->mutex); |
171 | if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) { |
172 | thread->state |= PERL_ITHR_DETACHED; |
173 | #ifdef WIN32 |
174 | CloseHandle(thread->handle); |
175 | thread->handle = 0; |
176 | #else |
177 | PERL_THREAD_DETACH(thread->thr); |
178 | #endif |
179 | } |
180 | if ((thread->state & PERL_ITHR_FINISHED) && |
181 | (thread->state & PERL_ITHR_DETACHED)) { |
182 | MUTEX_UNLOCK(&thread->mutex); |
183 | Perl_ithread_destruct(aTHX_ thread, "detach"); |
184 | } |
185 | else { |
186 | MUTEX_UNLOCK(&thread->mutex); |
187 | } |
188 | } |
68795e93 |
189 | |
190 | /* MAGIC (in mg.h sense) hooks */ |
191 | |
192 | int |
193 | ithread_mg_get(pTHX_ SV *sv, MAGIC *mg) |
194 | { |
195 | ithread *thread = (ithread *) mg->mg_ptr; |
196 | SvIVX(sv) = PTR2IV(thread); |
197 | SvIOK_on(sv); |
198 | return 0; |
199 | } |
200 | |
201 | int |
202 | ithread_mg_free(pTHX_ SV *sv, MAGIC *mg) |
203 | { |
204 | ithread *thread = (ithread *) mg->mg_ptr; |
205 | MUTEX_LOCK(&thread->mutex); |
68795e93 |
206 | thread->count--; |
62375a60 |
207 | if (thread->count == 0) { |
1c3adb19 |
208 | if(thread->state & PERL_ITHR_FINISHED && |
209 | (thread->state & PERL_ITHR_DETACHED || |
210 | thread->state & PERL_ITHR_JOINED)) |
211 | { |
212 | MUTEX_UNLOCK(&thread->mutex); |
213 | Perl_ithread_destruct(aTHX_ thread, "no reference"); |
214 | } |
1ea20f42 |
215 | else { |
216 | MUTEX_UNLOCK(&thread->mutex); |
217 | } |
62375a60 |
218 | } |
219 | else { |
220 | MUTEX_UNLOCK(&thread->mutex); |
221 | } |
68795e93 |
222 | return 0; |
223 | } |
224 | |
225 | int |
226 | ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) |
227 | { |
228 | ithread *thread = (ithread *) mg->mg_ptr; |
229 | MUTEX_LOCK(&thread->mutex); |
68795e93 |
230 | thread->count++; |
231 | MUTEX_UNLOCK(&thread->mutex); |
232 | return 0; |
233 | } |
234 | |
235 | MGVTBL ithread_vtbl = { |
236 | ithread_mg_get, /* get */ |
237 | 0, /* set */ |
238 | 0, /* len */ |
239 | 0, /* clear */ |
240 | ithread_mg_free, /* free */ |
241 | 0, /* copy */ |
242 | ithread_mg_dup /* dup */ |
243 | }; |
244 | |
47ba8780 |
245 | |
47ba8780 |
246 | /* |
b1edfb69 |
247 | * Starts executing the thread. Needs to clean up memory a tad better. |
68795e93 |
248 | * Passed as the C level function to run in the new thread |
b1edfb69 |
249 | */ |
47ba8780 |
250 | |
251 | #ifdef WIN32 |
68795e93 |
252 | THREAD_RET_TYPE |
253 | Perl_ithread_run(LPVOID arg) { |
47ba8780 |
254 | #else |
68795e93 |
255 | void* |
256 | Perl_ithread_run(void * arg) { |
47ba8780 |
257 | #endif |
5b414d21 |
258 | ithread* thread = (ithread*) arg; |
47ba8780 |
259 | dTHXa(thread->interp); |
47ba8780 |
260 | PERL_SET_CONTEXT(thread->interp); |
68795e93 |
261 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
47ba8780 |
262 | |
68795e93 |
263 | #if 0 |
264 | /* Far from clear messing with ->thr child-side is a good idea */ |
265 | MUTEX_LOCK(&thread->mutex); |
47ba8780 |
266 | #ifdef WIN32 |
267 | thread->thr = GetCurrentThreadId(); |
268 | #else |
269 | thread->thr = pthread_self(); |
270 | #endif |
68795e93 |
271 | MUTEX_UNLOCK(&thread->mutex); |
272 | #endif |
47ba8780 |
273 | |
47ba8780 |
274 | PL_perl_destruct_level = 2; |
4f896ddc |
275 | |
47ba8780 |
276 | { |
68795e93 |
277 | AV* params = (AV*) SvRV(thread->params); |
278 | I32 len = av_len(params)+1; |
47ba8780 |
279 | int i; |
280 | dSP; |
47ba8780 |
281 | ENTER; |
282 | SAVETMPS; |
283 | PUSHMARK(SP); |
68795e93 |
284 | for(i = 0; i < len; i++) { |
285 | XPUSHs(av_shift(params)); |
47ba8780 |
286 | } |
287 | PUTBACK; |
a446a88f |
288 | len = call_sv(thread->init_function, thread->gimme|G_EVAL); |
0405e91e |
289 | |
68795e93 |
290 | SPAGAIN; |
a446a88f |
291 | for (i=len-1; i >= 0; i--) { |
e1c44605 |
292 | SV *sv = POPs; |
293 | av_store(params, i, SvREFCNT_inc(sv)); |
a446a88f |
294 | } |
a446a88f |
295 | if (SvTRUE(ERRSV)) { |
6b3c7930 |
296 | Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV); |
a446a88f |
297 | } |
47ba8780 |
298 | FREETMPS; |
299 | LEAVE; |
68795e93 |
300 | SvREFCNT_dec(thread->init_function); |
47ba8780 |
301 | } |
302 | |
fd58862f |
303 | PerlIO_flush((PerlIO*)NULL); |
68795e93 |
304 | MUTEX_LOCK(&thread->mutex); |
62375a60 |
305 | thread->state |= PERL_ITHR_FINISHED; |
306 | |
307 | if (thread->state & PERL_ITHR_DETACHED) { |
47ba8780 |
308 | MUTEX_UNLOCK(&thread->mutex); |
62375a60 |
309 | Perl_ithread_destruct(aTHX_ thread, "detached finish"); |
47ba8780 |
310 | } else { |
62375a60 |
311 | MUTEX_UNLOCK(&thread->mutex); |
312 | } |
91604d21 |
313 | MUTEX_LOCK(&create_destruct_mutex); |
314 | active_threads--; |
315 | assert( active_threads >= 0 ); |
316 | MUTEX_UNLOCK(&create_destruct_mutex); |
317 | |
47ba8780 |
318 | #ifdef WIN32 |
319 | return (DWORD)0; |
e8f2bb9a |
320 | #else |
321 | return 0; |
47ba8780 |
322 | #endif |
68795e93 |
323 | } |
324 | |
325 | SV * |
326 | ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc) |
327 | { |
328 | SV *sv; |
329 | MAGIC *mg; |
330 | if (inc) { |
331 | MUTEX_LOCK(&thread->mutex); |
332 | thread->count++; |
68795e93 |
333 | MUTEX_UNLOCK(&thread->mutex); |
334 | } |
335 | if (!obj) |
336 | obj = newSV(0); |
337 | sv = newSVrv(obj,classname); |
338 | sv_setiv(sv,PTR2IV(thread)); |
339 | mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0); |
340 | mg->mg_flags |= MGf_DUP; |
341 | SvREADONLY_on(sv); |
342 | return obj; |
343 | } |
47ba8780 |
344 | |
68795e93 |
345 | ithread * |
346 | SV_to_ithread(pTHX_ SV *sv) |
347 | { |
348 | ithread *thread; |
349 | if (SvROK(sv)) |
350 | { |
351 | thread = INT2PTR(ithread*, SvIV(SvRV(sv))); |
352 | } |
353 | else |
354 | { |
355 | PERL_THREAD_GETSPECIFIC(self_key,thread); |
356 | } |
357 | return thread; |
47ba8780 |
358 | } |
359 | |
47ba8780 |
360 | /* |
68795e93 |
361 | * iThread->create(); ( aka iThread->new() ) |
362 | * Called in context of parent thread |
b1edfb69 |
363 | */ |
47ba8780 |
364 | |
68795e93 |
365 | SV * |
366 | Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params) |
367 | { |
368 | ithread* thread; |
369 | CLONE_PARAMS clone_param; |
1d784c90 |
370 | ithread* current_thread; |
3b1c3273 |
371 | |
372 | SV** tmps_tmp = PL_tmps_stack; |
373 | I32 tmps_ix = PL_tmps_ix; |
374 | |
1d784c90 |
375 | PERL_THREAD_GETSPECIFIC(self_key,current_thread); |
58c2ef19 |
376 | MUTEX_LOCK(&create_destruct_mutex); |
68795e93 |
377 | thread = PerlMemShared_malloc(sizeof(ithread)); |
378 | Zero(thread,1,ithread); |
379 | thread->next = threads; |
380 | thread->prev = threads->prev; |
f42ad631 |
381 | threads->prev = thread; |
68795e93 |
382 | thread->prev->next = thread; |
383 | /* Set count to 1 immediately in case thread exits before |
384 | * we return to caller ! |
385 | */ |
386 | thread->count = 1; |
387 | MUTEX_INIT(&thread->mutex); |
388 | thread->tid = tid_counter++; |
a446a88f |
389 | thread->gimme = GIMME_V; |
4f896ddc |
390 | |
68795e93 |
391 | /* "Clone" our interpreter into the thread's interpreter |
392 | * This gives thread access to "static data" and code. |
393 | */ |
47ba8780 |
394 | |
68795e93 |
395 | PerlIO_flush((PerlIO*)NULL); |
1d784c90 |
396 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
3b1c3273 |
397 | |
398 | |
399 | |
47ba8780 |
400 | #ifdef WIN32 |
68795e93 |
401 | thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST); |
47ba8780 |
402 | #else |
68795e93 |
403 | thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE); |
47ba8780 |
404 | #endif |
ba14dd9a |
405 | /* perl_clone leaves us in new interpreter's context. |
c8dae523 |
406 | As it is tricky to spot an implicit aTHX, create a new scope |
a446a88f |
407 | with aTHX matching the context for the duration of |
ba14dd9a |
408 | our work for new interpreter. |
409 | */ |
410 | { |
411 | dTHXa(thread->interp); |
58c2ef19 |
412 | /* Here we remove END blocks since they should only run |
62375a60 |
413 | in the thread they are created |
58c2ef19 |
414 | */ |
415 | SvREFCNT_dec(PL_endav); |
416 | PL_endav = newAV(); |
d1400e48 |
417 | clone_param.flags = 0; |
ba14dd9a |
418 | thread->init_function = sv_dup(init_function, &clone_param); |
419 | if (SvREFCNT(thread->init_function) == 0) { |
420 | SvREFCNT_inc(thread->init_function); |
d1400e48 |
421 | } |
3b1c3273 |
422 | |
423 | |
ba14dd9a |
424 | |
425 | thread->params = sv_dup(params, &clone_param); |
426 | SvREFCNT_inc(thread->params); |
3b1c3273 |
427 | |
428 | |
429 | /* The code below checks that anything living on |
430 | the tmps stack and has been cloned (so it lives in the |
431 | ptr_table) has a refcount higher than 0 |
432 | |
433 | If the refcount is 0 it means that a something on the |
434 | stack/context was holding a reference to it and |
435 | since we init_stacks() in perl_clone that won't get |
436 | cleaned and we will get a leaked scalar. |
437 | The reason it was cloned was that it lived on the |
438 | @_ stack. |
439 | |
440 | Example of this can be found in bugreport 15837 |
441 | where calls in the parameter list end up as a temp |
442 | |
443 | One could argue that this fix should be in perl_clone |
444 | */ |
445 | |
446 | |
447 | while (tmps_ix > 0) { |
448 | SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]); |
449 | tmps_ix--; |
450 | if (sv && SvREFCNT(sv) == 0) { |
451 | SvREFCNT_inc(sv); |
452 | SvREFCNT_dec(sv); |
453 | } |
454 | } |
455 | |
456 | |
457 | |
ba14dd9a |
458 | SvTEMP_off(thread->init_function); |
459 | ptr_table_free(PL_ptr_table); |
460 | PL_ptr_table = NULL; |
ffb29f90 |
461 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
ba14dd9a |
462 | } |
1d784c90 |
463 | PERL_THREAD_SETSPECIFIC(self_key,current_thread); |
68795e93 |
464 | PERL_SET_CONTEXT(aTHX); |
47ba8780 |
465 | |
68795e93 |
466 | /* Start the thread */ |
47ba8780 |
467 | |
468 | #ifdef WIN32 |
469 | |
68795e93 |
470 | thread->handle = CreateThread(NULL, 0, Perl_ithread_run, |
47ba8780 |
471 | (LPVOID)thread, 0, &thread->thr); |
472 | |
82c40bf6 |
473 | #else |
fa26028c |
474 | { |
475 | static pthread_attr_t attr; |
476 | static int attr_inited = 0; |
fa26028c |
477 | static int attr_joinable = PTHREAD_CREATE_JOINABLE; |
478 | if (!attr_inited) { |
479 | attr_inited = 1; |
480 | pthread_attr_init(&attr); |
481 | } |
482 | # ifdef PTHREAD_ATTR_SETDETACHSTATE |
483 | PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable); |
484 | # endif |
3eb37d38 |
485 | # ifdef THREAD_CREATE_NEEDS_STACK |
486 | if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK)) |
487 | croak("panic: pthread_attr_setstacksize failed"); |
488 | # endif |
489 | |
3ad0b7d6 |
490 | #ifdef OLD_PTHREADS_API |
68795e93 |
491 | pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread); |
47ba8780 |
492 | #else |
68795e93 |
493 | pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread); |
47ba8780 |
494 | #endif |
3ad0b7d6 |
495 | } |
82c40bf6 |
496 | #endif |
62375a60 |
497 | known_threads++; |
58c2ef19 |
498 | active_threads++; |
499 | MUTEX_UNLOCK(&create_destruct_mutex); |
95393226 |
500 | sv_2mortal(params); |
3b1c3273 |
501 | |
68795e93 |
502 | return ithread_to_SV(aTHX_ obj, thread, classname, FALSE); |
503 | } |
47ba8780 |
504 | |
68795e93 |
505 | SV* |
506 | Perl_ithread_self (pTHX_ SV *obj, char* Class) |
507 | { |
508 | ithread *thread; |
509 | PERL_THREAD_GETSPECIFIC(self_key,thread); |
510 | return ithread_to_SV(aTHX_ obj, thread, Class, TRUE); |
47ba8780 |
511 | } |
512 | |
513 | /* |
e1c44605 |
514 | * Joins the thread this code needs to take the returnvalue from the |
68795e93 |
515 | * call_sv and send it back |
b1edfb69 |
516 | */ |
47ba8780 |
517 | |
68795e93 |
518 | void |
519 | Perl_ithread_CLONE(pTHX_ SV *obj) |
520 | { |
521 | if (SvROK(obj)) |
522 | { |
523 | ithread *thread = SV_to_ithread(aTHX_ obj); |
524 | } |
525 | else |
526 | { |
436c6dd3 |
527 | Perl_warn(aTHX_ "CLONE %" SVf,obj); |
68795e93 |
528 | } |
47ba8780 |
529 | } |
530 | |
62375a60 |
531 | AV* |
68795e93 |
532 | Perl_ithread_join(pTHX_ SV *obj) |
533 | { |
534 | ithread *thread = SV_to_ithread(aTHX_ obj); |
535 | MUTEX_LOCK(&thread->mutex); |
62375a60 |
536 | if (thread->state & PERL_ITHR_DETACHED) { |
a446a88f |
537 | MUTEX_UNLOCK(&thread->mutex); |
538 | Perl_croak(aTHX_ "Cannot join a detached thread"); |
539 | } |
62375a60 |
540 | else if (thread->state & PERL_ITHR_JOINED) { |
a446a88f |
541 | MUTEX_UNLOCK(&thread->mutex); |
542 | Perl_croak(aTHX_ "Thread already joined"); |
543 | } |
544 | else { |
e1c44605 |
545 | AV* retparam; |
47ba8780 |
546 | #ifdef WIN32 |
547 | DWORD waitcode; |
47ba8780 |
548 | #else |
549 | void *retval; |
47ba8780 |
550 | #endif |
47ba8780 |
551 | MUTEX_UNLOCK(&thread->mutex); |
68795e93 |
552 | #ifdef WIN32 |
553 | waitcode = WaitForSingleObject(thread->handle, INFINITE); |
554 | #else |
555 | pthread_join(thread->thr,&retval); |
556 | #endif |
47ba8780 |
557 | MUTEX_LOCK(&thread->mutex); |
e1c44605 |
558 | |
62375a60 |
559 | /* sv_dup over the args */ |
e1c44605 |
560 | { |
1d784c90 |
561 | ithread* current_thread; |
62375a60 |
562 | AV* params = (AV*) SvRV(thread->params); |
e1c44605 |
563 | CLONE_PARAMS clone_params; |
3275ba96 |
564 | clone_params.stashes = newAV(); |
0405e91e |
565 | clone_params.flags |= CLONEf_JOIN_IN; |
e1c44605 |
566 | PL_ptr_table = ptr_table_new(); |
1d784c90 |
567 | PERL_THREAD_GETSPECIFIC(self_key,current_thread); |
568 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
0405e91e |
569 | |
767c1403 |
570 | #if 0 |
0405e91e |
571 | { |
572 | I32 len = av_len(params)+1; |
573 | I32 i; |
574 | for(i = 0; i < len; i++) { |
767c1403 |
575 | sv_dump(SvRV(AvARRAY(params)[i])); |
0405e91e |
576 | } |
577 | } |
767c1403 |
578 | #endif |
e1c44605 |
579 | retparam = (AV*) sv_dup((SV*)params, &clone_params); |
b4cb676b |
580 | #if 0 |
0405e91e |
581 | { |
582 | I32 len = av_len(retparam)+1; |
583 | I32 i; |
584 | for(i = 0; i < len; i++) { |
b4cb676b |
585 | sv_dump(SvRV(AvARRAY(retparam)[i])); |
0405e91e |
586 | } |
587 | } |
b4cb676b |
588 | #endif |
1d784c90 |
589 | PERL_THREAD_SETSPECIFIC(self_key,current_thread); |
3275ba96 |
590 | SvREFCNT_dec(clone_params.stashes); |
e1c44605 |
591 | SvREFCNT_inc(retparam); |
592 | ptr_table_free(PL_ptr_table); |
593 | PL_ptr_table = NULL; |
594 | |
595 | } |
a446a88f |
596 | /* We have finished with it */ |
62375a60 |
597 | thread->state |= PERL_ITHR_JOINED; |
47ba8780 |
598 | MUTEX_UNLOCK(&thread->mutex); |
9684265f |
599 | sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar); |
e1c44605 |
600 | return retparam; |
68795e93 |
601 | } |
e1c44605 |
602 | return (AV*)NULL; |
47ba8780 |
603 | } |
604 | |
68795e93 |
605 | void |
68795e93 |
606 | Perl_ithread_DESTROY(pTHX_ SV *sv) |
607 | { |
608 | ithread *thread = SV_to_ithread(aTHX_ sv); |
68795e93 |
609 | sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar); |
610 | } |
8222d950 |
611 | |
73e09c8f |
612 | #endif /* USE_ITHREADS */ |
e1c44605 |
613 | |
68795e93 |
614 | MODULE = threads PACKAGE = threads PREFIX = ithread_ |
615 | PROTOTYPES: DISABLE |
8222d950 |
616 | |
73e09c8f |
617 | #ifdef USE_ITHREADS |
618 | |
68795e93 |
619 | void |
620 | ithread_new (classname, function_to_call, ...) |
621 | char * classname |
622 | SV * function_to_call |
623 | CODE: |
624 | { |
625 | AV* params = newAV(); |
626 | if (items > 2) { |
627 | int i; |
628 | for(i = 2; i < items ; i++) { |
95393226 |
629 | av_push(params, SvREFCNT_inc(ST(i))); |
68795e93 |
630 | } |
631 | } |
632 | ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params))); |
633 | XSRETURN(1); |
634 | } |
8222d950 |
635 | |
68795e93 |
636 | void |
678a9b6c |
637 | ithread_list(char *classname) |
638 | PPCODE: |
639 | { |
640 | ithread *curr_thread; |
641 | MUTEX_LOCK(&create_destruct_mutex); |
642 | curr_thread = threads; |
5eb9fe8f |
643 | if(curr_thread->tid != 0) |
644 | PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE))); |
678a9b6c |
645 | while(curr_thread) { |
678a9b6c |
646 | curr_thread = curr_thread->next; |
647 | if(curr_thread == threads) |
648 | break; |
6794f985 |
649 | if(curr_thread->state & PERL_ITHR_DETACHED || |
5eb9fe8f |
650 | curr_thread->state & PERL_ITHR_JOINED) |
651 | continue; |
652 | PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE))); |
678a9b6c |
653 | } |
654 | MUTEX_UNLOCK(&create_destruct_mutex); |
655 | } |
656 | |
657 | |
658 | void |
68795e93 |
659 | ithread_self(char *classname) |
660 | CODE: |
661 | { |
662 | ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname)); |
663 | XSRETURN(1); |
664 | } |
47ba8780 |
665 | |
666 | int |
68795e93 |
667 | ithread_tid(ithread *thread) |
47ba8780 |
668 | |
669 | void |
68795e93 |
670 | ithread_join(SV *obj) |
e1c44605 |
671 | PPCODE: |
672 | { |
673 | AV* params = Perl_ithread_join(aTHX_ obj); |
674 | int i; |
675 | I32 len = AvFILL(params); |
676 | for (i = 0; i <= len; i++) { |
1c3adb19 |
677 | SV* tmp = av_shift(params); |
678 | XPUSHs(tmp); |
679 | sv_2mortal(tmp); |
e1c44605 |
680 | } |
681 | SvREFCNT_dec(params); |
682 | } |
683 | |
f9dff5f5 |
684 | void |
9d7debe1 |
685 | yield(...) |
70f2e746 |
686 | CODE: |
687 | { |
688 | YIELD; |
689 | } |
690 | |
47ba8780 |
691 | |
692 | void |
68795e93 |
693 | ithread_detach(ithread *thread) |
47ba8780 |
694 | |
47ba8780 |
695 | void |
68795e93 |
696 | ithread_DESTROY(SV *thread) |
697 | |
73e09c8f |
698 | #endif /* USE_ITHREADS */ |
699 | |
68795e93 |
700 | BOOT: |
701 | { |
73e09c8f |
702 | #ifdef USE_ITHREADS |
68795e93 |
703 | ithread* thread; |
e1c44605 |
704 | PL_perl_destruct_level = 2; |
68795e93 |
705 | PERL_THREAD_ALLOC_SPECIFIC(self_key); |
58c2ef19 |
706 | MUTEX_INIT(&create_destruct_mutex); |
707 | MUTEX_LOCK(&create_destruct_mutex); |
62375a60 |
708 | PL_threadhook = &Perl_ithread_hook; |
68795e93 |
709 | thread = PerlMemShared_malloc(sizeof(ithread)); |
710 | Zero(thread,1,ithread); |
711 | PL_perl_destruct_level = 2; |
712 | MUTEX_INIT(&thread->mutex); |
713 | threads = thread; |
714 | thread->next = thread; |
715 | thread->prev = thread; |
716 | thread->interp = aTHX; |
717 | thread->count = 1; /* imortal */ |
718 | thread->tid = tid_counter++; |
62375a60 |
719 | known_threads++; |
58c2ef19 |
720 | active_threads++; |
62375a60 |
721 | thread->state = 1; |
68795e93 |
722 | #ifdef WIN32 |
723 | thread->thr = GetCurrentThreadId(); |
724 | #else |
725 | thread->thr = pthread_self(); |
726 | #endif |
62375a60 |
727 | |
68795e93 |
728 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
58c2ef19 |
729 | MUTEX_UNLOCK(&create_destruct_mutex); |
73e09c8f |
730 | #endif /* USE_ITHREADS */ |
68795e93 |
731 | } |
732 | |