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 */ |
68795e93 |
125 | if (thread->interp) { |
1c3adb19 |
126 | dTHXa(thread->interp); |
68795e93 |
127 | PERL_SET_CONTEXT(thread->interp); |
1c3adb19 |
128 | SvREFCNT_dec(thread->params); |
129 | thread->params = Nullsv; |
68795e93 |
130 | perl_destruct(thread->interp); |
131 | perl_free(thread->interp); |
132 | thread->interp = NULL; |
133 | } |
134 | PERL_SET_CONTEXT(aTHX); |
d1400e48 |
135 | MUTEX_UNLOCK(&thread->mutex); |
1c3adb19 |
136 | MUTEX_DESTROY(&thread->mutex); |
137 | PerlMemShared_free(thread); |
68795e93 |
138 | } |
139 | |
62375a60 |
140 | int |
141 | Perl_ithread_hook(pTHX) |
142 | { |
143 | int veto_cleanup = 0; |
144 | MUTEX_LOCK(&create_destruct_mutex); |
145 | if (aTHX == PL_curinterp && active_threads != 1) { |
146 | Perl_warn(aTHX_ "Cleanup skipped %d active threads", active_threads); |
147 | veto_cleanup = 1; |
148 | } |
149 | MUTEX_UNLOCK(&create_destruct_mutex); |
150 | return veto_cleanup; |
151 | } |
152 | |
153 | void |
154 | Perl_ithread_detach(pTHX_ ithread *thread) |
155 | { |
156 | MUTEX_LOCK(&thread->mutex); |
157 | if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) { |
158 | thread->state |= PERL_ITHR_DETACHED; |
159 | #ifdef WIN32 |
160 | CloseHandle(thread->handle); |
161 | thread->handle = 0; |
162 | #else |
163 | PERL_THREAD_DETACH(thread->thr); |
164 | #endif |
165 | } |
166 | if ((thread->state & PERL_ITHR_FINISHED) && |
167 | (thread->state & PERL_ITHR_DETACHED)) { |
168 | MUTEX_UNLOCK(&thread->mutex); |
169 | Perl_ithread_destruct(aTHX_ thread, "detach"); |
170 | } |
171 | else { |
172 | MUTEX_UNLOCK(&thread->mutex); |
173 | } |
174 | } |
68795e93 |
175 | |
176 | /* MAGIC (in mg.h sense) hooks */ |
177 | |
178 | int |
179 | ithread_mg_get(pTHX_ SV *sv, MAGIC *mg) |
180 | { |
181 | ithread *thread = (ithread *) mg->mg_ptr; |
182 | SvIVX(sv) = PTR2IV(thread); |
183 | SvIOK_on(sv); |
184 | return 0; |
185 | } |
186 | |
187 | int |
188 | ithread_mg_free(pTHX_ SV *sv, MAGIC *mg) |
189 | { |
190 | ithread *thread = (ithread *) mg->mg_ptr; |
191 | MUTEX_LOCK(&thread->mutex); |
68795e93 |
192 | thread->count--; |
62375a60 |
193 | if (thread->count == 0) { |
1c3adb19 |
194 | if(thread->state & PERL_ITHR_FINISHED && |
195 | (thread->state & PERL_ITHR_DETACHED || |
196 | thread->state & PERL_ITHR_JOINED)) |
197 | { |
198 | MUTEX_UNLOCK(&thread->mutex); |
199 | Perl_ithread_destruct(aTHX_ thread, "no reference"); |
200 | } |
1ea20f42 |
201 | else { |
202 | MUTEX_UNLOCK(&thread->mutex); |
203 | } |
62375a60 |
204 | } |
205 | else { |
206 | MUTEX_UNLOCK(&thread->mutex); |
207 | } |
68795e93 |
208 | return 0; |
209 | } |
210 | |
211 | int |
212 | ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) |
213 | { |
214 | ithread *thread = (ithread *) mg->mg_ptr; |
215 | MUTEX_LOCK(&thread->mutex); |
68795e93 |
216 | thread->count++; |
217 | MUTEX_UNLOCK(&thread->mutex); |
218 | return 0; |
219 | } |
220 | |
221 | MGVTBL ithread_vtbl = { |
222 | ithread_mg_get, /* get */ |
223 | 0, /* set */ |
224 | 0, /* len */ |
225 | 0, /* clear */ |
226 | ithread_mg_free, /* free */ |
227 | 0, /* copy */ |
228 | ithread_mg_dup /* dup */ |
229 | }; |
230 | |
47ba8780 |
231 | |
47ba8780 |
232 | /* |
b1edfb69 |
233 | * Starts executing the thread. Needs to clean up memory a tad better. |
68795e93 |
234 | * Passed as the C level function to run in the new thread |
b1edfb69 |
235 | */ |
47ba8780 |
236 | |
237 | #ifdef WIN32 |
68795e93 |
238 | THREAD_RET_TYPE |
239 | Perl_ithread_run(LPVOID arg) { |
47ba8780 |
240 | #else |
68795e93 |
241 | void* |
242 | Perl_ithread_run(void * arg) { |
47ba8780 |
243 | #endif |
5b414d21 |
244 | ithread* thread = (ithread*) arg; |
47ba8780 |
245 | dTHXa(thread->interp); |
47ba8780 |
246 | PERL_SET_CONTEXT(thread->interp); |
68795e93 |
247 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
47ba8780 |
248 | |
68795e93 |
249 | #if 0 |
250 | /* Far from clear messing with ->thr child-side is a good idea */ |
251 | MUTEX_LOCK(&thread->mutex); |
47ba8780 |
252 | #ifdef WIN32 |
253 | thread->thr = GetCurrentThreadId(); |
254 | #else |
255 | thread->thr = pthread_self(); |
256 | #endif |
68795e93 |
257 | MUTEX_UNLOCK(&thread->mutex); |
258 | #endif |
47ba8780 |
259 | |
47ba8780 |
260 | PL_perl_destruct_level = 2; |
4f896ddc |
261 | |
47ba8780 |
262 | { |
68795e93 |
263 | AV* params = (AV*) SvRV(thread->params); |
264 | I32 len = av_len(params)+1; |
47ba8780 |
265 | int i; |
266 | dSP; |
47ba8780 |
267 | ENTER; |
268 | SAVETMPS; |
269 | PUSHMARK(SP); |
68795e93 |
270 | for(i = 0; i < len; i++) { |
271 | XPUSHs(av_shift(params)); |
47ba8780 |
272 | } |
273 | PUTBACK; |
a446a88f |
274 | len = call_sv(thread->init_function, thread->gimme|G_EVAL); |
68795e93 |
275 | SPAGAIN; |
a446a88f |
276 | for (i=len-1; i >= 0; i--) { |
e1c44605 |
277 | SV *sv = POPs; |
278 | av_store(params, i, SvREFCNT_inc(sv)); |
a446a88f |
279 | } |
280 | PUTBACK; |
281 | if (SvTRUE(ERRSV)) { |
282 | Perl_warn(aTHX_ "Died:%_",ERRSV); |
283 | } |
47ba8780 |
284 | FREETMPS; |
285 | LEAVE; |
68795e93 |
286 | SvREFCNT_dec(thread->init_function); |
47ba8780 |
287 | } |
288 | |
fd58862f |
289 | PerlIO_flush((PerlIO*)NULL); |
62375a60 |
290 | MUTEX_LOCK(&create_destruct_mutex); |
291 | active_threads--; |
292 | assert( active_threads >= 0 ); |
293 | MUTEX_UNLOCK(&create_destruct_mutex); |
68795e93 |
294 | MUTEX_LOCK(&thread->mutex); |
62375a60 |
295 | thread->state |= PERL_ITHR_FINISHED; |
296 | |
297 | if (thread->state & PERL_ITHR_DETACHED) { |
47ba8780 |
298 | MUTEX_UNLOCK(&thread->mutex); |
62375a60 |
299 | Perl_ithread_destruct(aTHX_ thread, "detached finish"); |
47ba8780 |
300 | } else { |
62375a60 |
301 | MUTEX_UNLOCK(&thread->mutex); |
302 | } |
47ba8780 |
303 | #ifdef WIN32 |
304 | return (DWORD)0; |
e8f2bb9a |
305 | #else |
306 | return 0; |
47ba8780 |
307 | #endif |
68795e93 |
308 | } |
309 | |
310 | SV * |
311 | ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc) |
312 | { |
313 | SV *sv; |
314 | MAGIC *mg; |
315 | if (inc) { |
316 | MUTEX_LOCK(&thread->mutex); |
317 | thread->count++; |
68795e93 |
318 | MUTEX_UNLOCK(&thread->mutex); |
319 | } |
320 | if (!obj) |
321 | obj = newSV(0); |
322 | sv = newSVrv(obj,classname); |
323 | sv_setiv(sv,PTR2IV(thread)); |
324 | mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0); |
325 | mg->mg_flags |= MGf_DUP; |
326 | SvREADONLY_on(sv); |
327 | return obj; |
328 | } |
47ba8780 |
329 | |
68795e93 |
330 | ithread * |
331 | SV_to_ithread(pTHX_ SV *sv) |
332 | { |
333 | ithread *thread; |
334 | if (SvROK(sv)) |
335 | { |
336 | thread = INT2PTR(ithread*, SvIV(SvRV(sv))); |
337 | } |
338 | else |
339 | { |
340 | PERL_THREAD_GETSPECIFIC(self_key,thread); |
341 | } |
342 | return thread; |
47ba8780 |
343 | } |
344 | |
47ba8780 |
345 | /* |
68795e93 |
346 | * iThread->create(); ( aka iThread->new() ) |
347 | * Called in context of parent thread |
b1edfb69 |
348 | */ |
47ba8780 |
349 | |
68795e93 |
350 | SV * |
351 | Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params) |
352 | { |
353 | ithread* thread; |
354 | CLONE_PARAMS clone_param; |
355 | |
58c2ef19 |
356 | MUTEX_LOCK(&create_destruct_mutex); |
68795e93 |
357 | thread = PerlMemShared_malloc(sizeof(ithread)); |
358 | Zero(thread,1,ithread); |
359 | thread->next = threads; |
360 | thread->prev = threads->prev; |
f42ad631 |
361 | threads->prev = thread; |
68795e93 |
362 | thread->prev->next = thread; |
363 | /* Set count to 1 immediately in case thread exits before |
364 | * we return to caller ! |
365 | */ |
366 | thread->count = 1; |
367 | MUTEX_INIT(&thread->mutex); |
368 | thread->tid = tid_counter++; |
a446a88f |
369 | thread->gimme = GIMME_V; |
4f896ddc |
370 | |
68795e93 |
371 | /* "Clone" our interpreter into the thread's interpreter |
372 | * This gives thread access to "static data" and code. |
373 | */ |
47ba8780 |
374 | |
68795e93 |
375 | PerlIO_flush((PerlIO*)NULL); |
cd8c9bf8 |
376 | |
47ba8780 |
377 | #ifdef WIN32 |
68795e93 |
378 | thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST); |
47ba8780 |
379 | #else |
68795e93 |
380 | thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE); |
47ba8780 |
381 | #endif |
ba14dd9a |
382 | /* perl_clone leaves us in new interpreter's context. |
383 | As it is tricky to spot implcit aTHX create a new scope |
a446a88f |
384 | with aTHX matching the context for the duration of |
ba14dd9a |
385 | our work for new interpreter. |
386 | */ |
387 | { |
388 | dTHXa(thread->interp); |
58c2ef19 |
389 | /* Here we remove END blocks since they should only run |
62375a60 |
390 | in the thread they are created |
58c2ef19 |
391 | */ |
392 | SvREFCNT_dec(PL_endav); |
393 | PL_endav = newAV(); |
d1400e48 |
394 | clone_param.flags = 0; |
ba14dd9a |
395 | thread->init_function = sv_dup(init_function, &clone_param); |
396 | if (SvREFCNT(thread->init_function) == 0) { |
397 | SvREFCNT_inc(thread->init_function); |
d1400e48 |
398 | } |
ba14dd9a |
399 | |
400 | thread->params = sv_dup(params, &clone_param); |
401 | SvREFCNT_inc(thread->params); |
402 | SvTEMP_off(thread->init_function); |
403 | ptr_table_free(PL_ptr_table); |
404 | PL_ptr_table = NULL; |
ffb29f90 |
405 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
ba14dd9a |
406 | } |
d1400e48 |
407 | |
68795e93 |
408 | PERL_SET_CONTEXT(aTHX); |
47ba8780 |
409 | |
68795e93 |
410 | /* Start the thread */ |
47ba8780 |
411 | |
412 | #ifdef WIN32 |
413 | |
68795e93 |
414 | thread->handle = CreateThread(NULL, 0, Perl_ithread_run, |
47ba8780 |
415 | (LPVOID)thread, 0, &thread->thr); |
416 | |
82c40bf6 |
417 | #else |
fa26028c |
418 | { |
419 | static pthread_attr_t attr; |
420 | static int attr_inited = 0; |
fa26028c |
421 | static int attr_joinable = PTHREAD_CREATE_JOINABLE; |
422 | if (!attr_inited) { |
423 | attr_inited = 1; |
424 | pthread_attr_init(&attr); |
425 | } |
426 | # ifdef PTHREAD_ATTR_SETDETACHSTATE |
427 | PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable); |
428 | # endif |
3eb37d38 |
429 | # ifdef THREAD_CREATE_NEEDS_STACK |
430 | if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK)) |
431 | croak("panic: pthread_attr_setstacksize failed"); |
432 | # endif |
433 | |
3ad0b7d6 |
434 | #ifdef OLD_PTHREADS_API |
68795e93 |
435 | pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread); |
47ba8780 |
436 | #else |
68795e93 |
437 | pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread); |
47ba8780 |
438 | #endif |
3ad0b7d6 |
439 | } |
82c40bf6 |
440 | #endif |
62375a60 |
441 | known_threads++; |
58c2ef19 |
442 | active_threads++; |
443 | MUTEX_UNLOCK(&create_destruct_mutex); |
95393226 |
444 | sv_2mortal(params); |
68795e93 |
445 | return ithread_to_SV(aTHX_ obj, thread, classname, FALSE); |
446 | } |
47ba8780 |
447 | |
68795e93 |
448 | SV* |
449 | Perl_ithread_self (pTHX_ SV *obj, char* Class) |
450 | { |
451 | ithread *thread; |
452 | PERL_THREAD_GETSPECIFIC(self_key,thread); |
453 | return ithread_to_SV(aTHX_ obj, thread, Class, TRUE); |
47ba8780 |
454 | } |
455 | |
456 | /* |
e1c44605 |
457 | * Joins the thread this code needs to take the returnvalue from the |
68795e93 |
458 | * call_sv and send it back |
b1edfb69 |
459 | */ |
47ba8780 |
460 | |
68795e93 |
461 | void |
462 | Perl_ithread_CLONE(pTHX_ SV *obj) |
463 | { |
464 | if (SvROK(obj)) |
465 | { |
466 | ithread *thread = SV_to_ithread(aTHX_ obj); |
467 | } |
468 | else |
469 | { |
470 | Perl_warn(aTHX_ "CLONE %_",obj); |
471 | } |
47ba8780 |
472 | } |
473 | |
62375a60 |
474 | AV* |
68795e93 |
475 | Perl_ithread_join(pTHX_ SV *obj) |
476 | { |
477 | ithread *thread = SV_to_ithread(aTHX_ obj); |
478 | MUTEX_LOCK(&thread->mutex); |
62375a60 |
479 | if (thread->state & PERL_ITHR_DETACHED) { |
a446a88f |
480 | MUTEX_UNLOCK(&thread->mutex); |
481 | Perl_croak(aTHX_ "Cannot join a detached thread"); |
482 | } |
62375a60 |
483 | else if (thread->state & PERL_ITHR_JOINED) { |
a446a88f |
484 | MUTEX_UNLOCK(&thread->mutex); |
485 | Perl_croak(aTHX_ "Thread already joined"); |
486 | } |
487 | else { |
e1c44605 |
488 | AV* retparam; |
47ba8780 |
489 | #ifdef WIN32 |
490 | DWORD waitcode; |
47ba8780 |
491 | #else |
492 | void *retval; |
47ba8780 |
493 | #endif |
47ba8780 |
494 | MUTEX_UNLOCK(&thread->mutex); |
68795e93 |
495 | #ifdef WIN32 |
496 | waitcode = WaitForSingleObject(thread->handle, INFINITE); |
497 | #else |
498 | pthread_join(thread->thr,&retval); |
499 | #endif |
47ba8780 |
500 | MUTEX_LOCK(&thread->mutex); |
e1c44605 |
501 | |
62375a60 |
502 | /* sv_dup over the args */ |
e1c44605 |
503 | { |
62375a60 |
504 | AV* params = (AV*) SvRV(thread->params); |
e1c44605 |
505 | CLONE_PARAMS clone_params; |
3275ba96 |
506 | clone_params.stashes = newAV(); |
e1c44605 |
507 | PL_ptr_table = ptr_table_new(); |
508 | retparam = (AV*) sv_dup((SV*)params, &clone_params); |
3275ba96 |
509 | SvREFCNT_dec(clone_params.stashes); |
e1c44605 |
510 | SvREFCNT_inc(retparam); |
511 | ptr_table_free(PL_ptr_table); |
512 | PL_ptr_table = NULL; |
513 | |
514 | } |
a446a88f |
515 | /* We have finished with it */ |
62375a60 |
516 | thread->state |= PERL_ITHR_JOINED; |
47ba8780 |
517 | MUTEX_UNLOCK(&thread->mutex); |
9684265f |
518 | sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar); |
e1c44605 |
519 | return retparam; |
68795e93 |
520 | } |
e1c44605 |
521 | return (AV*)NULL; |
47ba8780 |
522 | } |
523 | |
68795e93 |
524 | void |
68795e93 |
525 | Perl_ithread_DESTROY(pTHX_ SV *sv) |
526 | { |
527 | ithread *thread = SV_to_ithread(aTHX_ sv); |
68795e93 |
528 | sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar); |
529 | } |
8222d950 |
530 | |
73e09c8f |
531 | #endif /* USE_ITHREADS */ |
e1c44605 |
532 | |
68795e93 |
533 | MODULE = threads PACKAGE = threads PREFIX = ithread_ |
534 | PROTOTYPES: DISABLE |
8222d950 |
535 | |
73e09c8f |
536 | #ifdef USE_ITHREADS |
537 | |
68795e93 |
538 | void |
539 | ithread_new (classname, function_to_call, ...) |
540 | char * classname |
541 | SV * function_to_call |
542 | CODE: |
543 | { |
544 | AV* params = newAV(); |
545 | if (items > 2) { |
546 | int i; |
547 | for(i = 2; i < items ; i++) { |
95393226 |
548 | av_push(params, SvREFCNT_inc(ST(i))); |
68795e93 |
549 | } |
550 | } |
551 | ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params))); |
552 | XSRETURN(1); |
553 | } |
8222d950 |
554 | |
68795e93 |
555 | void |
678a9b6c |
556 | ithread_list(char *classname) |
557 | PPCODE: |
558 | { |
559 | ithread *curr_thread; |
560 | MUTEX_LOCK(&create_destruct_mutex); |
561 | curr_thread = threads; |
6794f985 |
562 | PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE))); |
678a9b6c |
563 | while(curr_thread) { |
678a9b6c |
564 | curr_thread = curr_thread->next; |
565 | if(curr_thread == threads) |
566 | break; |
6794f985 |
567 | if(curr_thread->state & PERL_ITHR_DETACHED || |
568 | curr_thread->state & PERL_ITHR_JOINED) |
569 | continue; |
570 | PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE))); |
678a9b6c |
571 | } |
572 | MUTEX_UNLOCK(&create_destruct_mutex); |
573 | } |
574 | |
575 | |
576 | void |
68795e93 |
577 | ithread_self(char *classname) |
578 | CODE: |
579 | { |
580 | ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname)); |
581 | XSRETURN(1); |
582 | } |
47ba8780 |
583 | |
584 | int |
68795e93 |
585 | ithread_tid(ithread *thread) |
47ba8780 |
586 | |
587 | void |
68795e93 |
588 | ithread_join(SV *obj) |
e1c44605 |
589 | PPCODE: |
590 | { |
591 | AV* params = Perl_ithread_join(aTHX_ obj); |
592 | int i; |
593 | I32 len = AvFILL(params); |
594 | for (i = 0; i <= len; i++) { |
1c3adb19 |
595 | SV* tmp = av_shift(params); |
596 | XPUSHs(tmp); |
597 | sv_2mortal(tmp); |
e1c44605 |
598 | } |
599 | SvREFCNT_dec(params); |
600 | } |
601 | |
f9dff5f5 |
602 | void |
603 | ithread_yield(ithread *thread) |
47ba8780 |
604 | |
605 | void |
68795e93 |
606 | ithread_detach(ithread *thread) |
47ba8780 |
607 | |
47ba8780 |
608 | void |
68795e93 |
609 | ithread_DESTROY(SV *thread) |
610 | |
73e09c8f |
611 | #endif /* USE_ITHREADS */ |
612 | |
68795e93 |
613 | BOOT: |
614 | { |
73e09c8f |
615 | #ifdef USE_ITHREADS |
68795e93 |
616 | ithread* thread; |
e1c44605 |
617 | PL_perl_destruct_level = 2; |
68795e93 |
618 | PERL_THREAD_ALLOC_SPECIFIC(self_key); |
58c2ef19 |
619 | MUTEX_INIT(&create_destruct_mutex); |
620 | MUTEX_LOCK(&create_destruct_mutex); |
62375a60 |
621 | PL_threadhook = &Perl_ithread_hook; |
68795e93 |
622 | thread = PerlMemShared_malloc(sizeof(ithread)); |
623 | Zero(thread,1,ithread); |
624 | PL_perl_destruct_level = 2; |
625 | MUTEX_INIT(&thread->mutex); |
626 | threads = thread; |
627 | thread->next = thread; |
628 | thread->prev = thread; |
629 | thread->interp = aTHX; |
630 | thread->count = 1; /* imortal */ |
631 | thread->tid = tid_counter++; |
62375a60 |
632 | known_threads++; |
58c2ef19 |
633 | active_threads++; |
62375a60 |
634 | thread->state = 1; |
68795e93 |
635 | #ifdef WIN32 |
636 | thread->thr = GetCurrentThreadId(); |
637 | #else |
638 | thread->thr = pthread_self(); |
639 | #endif |
62375a60 |
640 | |
68795e93 |
641 | PERL_THREAD_SETSPECIFIC(self_key,thread); |
58c2ef19 |
642 | MUTEX_UNLOCK(&create_destruct_mutex); |
73e09c8f |
643 | #endif /* USE_ITHREADS */ |
68795e93 |
644 | } |
645 | |