Commit | Line | Data |
c5be433b |
1 | #define PERL_NO_GET_CONTEXT |
d9bb3666 |
2 | #include "EXTERN.h" |
3 | #include "perl.h" |
4 | #include "XSUB.h" |
5 | |
7d901afa |
6 | /* Magic signature for Thread's mg_private is "Th" */ |
7 | #define Thread_MAGIC_SIGNATURE 0x5468 |
8 | |
f0f333f4 |
9 | #ifdef __cplusplus |
10 | #ifdef I_UNISTD |
11 | #include <unistd.h> |
12 | #endif |
13 | #endif |
14 | #include <fcntl.h> |
15 | |
85ced67f |
16 | static int sig_pipe[2]; |
f0f333f4 |
17 | |
18 | #ifndef THREAD_RET_TYPE |
f0f333f4 |
19 | #define THREAD_RET_TYPE void * |
20 | #define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x) |
458fb581 |
21 | #endif |
683929b4 |
22 | |
7d901afa |
23 | static void |
54fb45e2 |
24 | remove_thread(pTHX_ Thread t) |
7d901afa |
25 | { |
f0f333f4 |
26 | #ifdef USE_THREADS |
bf49b057 |
27 | DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log, |
7d901afa |
28 | "%p: remove_thread %p\n", thr, t))); |
533c011a |
29 | MUTEX_LOCK(&PL_threads_mutex); |
0a00ffdb |
30 | MUTEX_DESTROY(&t->mutex); |
533c011a |
31 | PL_nthreads--; |
7d901afa |
32 | t->prev->next = t->next; |
33 | t->next->prev = t->prev; |
0655b981 |
34 | SvREFCNT_dec(t->oursv); |
533c011a |
35 | COND_BROADCAST(&PL_nthreads_cond); |
36 | MUTEX_UNLOCK(&PL_threads_mutex); |
f0f333f4 |
37 | #endif |
7d901afa |
38 | } |
39 | |
ea0efc06 |
40 | static THREAD_RET_TYPE |
f0f333f4 |
41 | threadstart(void *arg) |
d9bb3666 |
42 | { |
f0f333f4 |
43 | #ifdef USE_THREADS |
783070da |
44 | #ifdef FAKE_THREADS |
45 | Thread savethread = thr; |
46 | LOGOP myop; |
47 | dSP; |
6b88bc9c |
48 | I32 oldscope = PL_scopestack_ix; |
783070da |
49 | I32 retval; |
458fb581 |
50 | AV *av; |
783070da |
51 | int i; |
52 | |
bf49b057 |
53 | DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n", |
683929b4 |
54 | thr, SvPEEK(TOPs))); |
783070da |
55 | thr = (Thread) arg; |
56 | savemark = TOPMARK; |
57 | thr->prev = thr->prev_run = savethread; |
58 | thr->next = savethread->next; |
59 | thr->next_run = savethread->next_run; |
60 | savethread->next = savethread->next_run = thr; |
61 | thr->wait_queue = 0; |
62 | thr->private = 0; |
63 | |
64 | /* Now duplicate most of perl_call_sv but with a few twists */ |
6b88bc9c |
65 | PL_op = (OP*)&myop; |
66 | Zero(PL_op, 1, LOGOP); |
783070da |
67 | myop.op_flags = OPf_STACKED; |
68 | myop.op_next = Nullop; |
69 | myop.op_flags |= OPf_KNOW; |
70 | myop.op_flags |= OPf_WANT_LIST; |
6b88bc9c |
71 | PL_op = pp_entersub(ARGS); |
8b73bbec |
72 | DEBUG_S(if (!PL_op) |
bf49b057 |
73 | PerlIO_printf(Perl_debug_log, "thread starts at Nullop\n")); |
783070da |
74 | /* |
75 | * When this thread is next scheduled, we start in the right |
76 | * place. When the thread runs off the end of the sub, perl.c |
77 | * handles things, using savemark to figure out how much of the |
78 | * stack is the return value for any join. |
79 | */ |
80 | thr = savethread; /* back to the old thread */ |
81 | return 0; |
82 | #else |
d9bb3666 |
83 | Thread thr = (Thread) arg; |
76ef7183 |
84 | dSP; |
d9bb3666 |
85 | I32 oldmark = TOPMARK; |
d9bb3666 |
86 | I32 retval; |
458fb581 |
87 | SV *sv; |
0ae6046c |
88 | AV *av; |
05c1ce25 |
89 | int i; |
0ae6046c |
90 | |
91 | #if defined(MULTIPLICITY) |
92 | PERL_SET_INTERP(thr->interp); |
93 | #endif |
94 | |
bf49b057 |
95 | DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p waiting to start\n", |
0b9678a8 |
96 | thr)); |
783070da |
97 | |
d9bb3666 |
98 | /* |
99 | * Wait until our creator releases us. If we didn't do this, then |
100 | * it would be potentially possible for out thread to carry on and |
101 | * do stuff before our creator fills in our "self" field. For example, |
ea0efc06 |
102 | * if we went and created another thread which tried to JOIN with us, |
103 | * then we'd be in a mess. |
d9bb3666 |
104 | */ |
50112d62 |
105 | MUTEX_LOCK(&thr->mutex); |
106 | MUTEX_UNLOCK(&thr->mutex); |
d9bb3666 |
107 | |
d9bb3666 |
108 | /* |
109 | * It's safe to wait until now to set the thread-specific pointer |
52e1cb5e |
110 | * from our pthread_t structure to our struct perl_thread, since |
111 | * we're the only thread who can get at it anyway. |
d9bb3666 |
112 | */ |
06d86050 |
113 | PERL_SET_THX(thr); |
d9bb3666 |
114 | |
bf49b057 |
115 | DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n", |
683929b4 |
116 | thr, SvPEEK(TOPs))); |
783070da |
117 | |
0ae6046c |
118 | av = newAV(); |
458fb581 |
119 | sv = POPs; |
120 | PUTBACK; |
901b18f1 |
121 | ENTER; |
122 | SAVETMPS; |
458fb581 |
123 | perl_call_sv(sv, G_ARRAY|G_EVAL); |
734689b1 |
124 | SPAGAIN; |
533c011a |
125 | retval = SP - (PL_stack_base + oldmark); |
126 | SP = PL_stack_base + oldmark + 1; |
458fb581 |
127 | if (SvCUR(thr->errsv)) { |
128 | MUTEX_LOCK(&thr->mutex); |
129 | thr->flags |= THRf_DID_DIE; |
130 | MUTEX_UNLOCK(&thr->mutex); |
6b88bc9c |
131 | av_store(av, 0, &PL_sv_no); |
458fb581 |
132 | av_store(av, 1, newSVsv(thr->errsv)); |
bf49b057 |
133 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p died: %s\n", |
6b88bc9c |
134 | thr, SvPV(thr->errsv, PL_na))); |
0655b981 |
135 | } |
136 | else { |
8b73bbec |
137 | DEBUG_S(STMT_START { |
458fb581 |
138 | for (i = 1; i <= retval; i++) { |
bf49b057 |
139 | PerlIO_printf(Perl_debug_log, "%p return[%d] = %s\n", |
924508f0 |
140 | thr, i, SvPEEK(SP[i - 1])); |
458fb581 |
141 | } |
142 | } STMT_END); |
6b88bc9c |
143 | av_store(av, 0, &PL_sv_yes); |
924508f0 |
144 | for (i = 1; i <= retval; i++, SP++) |
145 | sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP)); |
458fb581 |
146 | } |
572eda1c |
147 | FREETMPS; |
901b18f1 |
148 | LEAVE; |
458fb581 |
149 | |
783070da |
150 | #if 0 |
151 | /* removed for debug */ |
6b88bc9c |
152 | SvREFCNT_dec(PL_curstack); |
783070da |
153 | #endif |
199100c8 |
154 | SvREFCNT_dec(thr->cvcache); |
54b9620d |
155 | SvREFCNT_dec(thr->threadsv); |
554b3eca |
156 | SvREFCNT_dec(thr->specific); |
38a03e6e |
157 | SvREFCNT_dec(thr->errsv); |
5c0ca799 |
158 | |
f7ac0805 |
159 | /*Safefree(cxstack);*/ |
84fee439 |
160 | while (PL_curstackinfo->si_next) |
161 | PL_curstackinfo = PL_curstackinfo->si_next; |
162 | while (PL_curstackinfo) { |
163 | PERL_SI *p = PL_curstackinfo->si_prev; |
164 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
165 | Safefree(PL_curstackinfo->si_cxstack); |
166 | Safefree(PL_curstackinfo); |
167 | PL_curstackinfo = p; |
f7ac0805 |
168 | } |
84fee439 |
169 | Safefree(PL_markstack); |
170 | Safefree(PL_scopestack); |
171 | Safefree(PL_savestack); |
172 | Safefree(PL_retstack); |
173 | Safefree(PL_tmps_stack); |
691b83fc |
174 | SvREFCNT_dec(PL_ofs_sv); |
d9bb3666 |
175 | |
84fee439 |
176 | SvREFCNT_dec(PL_rs); |
177 | SvREFCNT_dec(PL_nrs); |
178 | SvREFCNT_dec(PL_statname); |
5a844595 |
179 | SvREFCNT_dec(PL_errors); |
84fee439 |
180 | Safefree(PL_screamfirst); |
181 | Safefree(PL_screamnext); |
182 | Safefree(PL_reg_start_tmp); |
183 | SvREFCNT_dec(PL_lastscream); |
901b18f1 |
184 | SvREFCNT_dec(PL_defoutgv); |
82ba1be6 |
185 | Safefree(PL_reg_poscache); |
5c0ca799 |
186 | |
14fcddff |
187 | MUTEX_LOCK(&thr->mutex); |
e01a9ca0 |
188 | thr->thr_done = 1; |
bf49b057 |
189 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
50112d62 |
190 | "%p: threadstart finishing: state is %u\n", |
191 | thr, ThrSTATE(thr))); |
14fcddff |
192 | switch (ThrSTATE(thr)) { |
193 | case THRf_R_JOINABLE: |
194 | ThrSETSTATE(thr, THRf_ZOMBIE); |
195 | MUTEX_UNLOCK(&thr->mutex); |
bf49b057 |
196 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
14fcddff |
197 | "%p: R_JOINABLE thread finished\n", thr)); |
198 | break; |
199 | case THRf_R_JOINED: |
200 | ThrSETSTATE(thr, THRf_DEAD); |
201 | MUTEX_UNLOCK(&thr->mutex); |
cea2e8a9 |
202 | remove_thread(aTHX_ thr); |
bf49b057 |
203 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
14fcddff |
204 | "%p: R_JOINED thread finished\n", thr)); |
205 | break; |
50112d62 |
206 | case THRf_R_DETACHED: |
683929b4 |
207 | ThrSETSTATE(thr, THRf_DEAD); |
14fcddff |
208 | MUTEX_UNLOCK(&thr->mutex); |
458fb581 |
209 | SvREFCNT_dec(av); |
bf49b057 |
210 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
14fcddff |
211 | "%p: DETACHED thread finished\n", thr)); |
cea2e8a9 |
212 | remove_thread(aTHX_ thr); /* This might trigger main thread to finish */ |
14fcddff |
213 | break; |
214 | default: |
215 | MUTEX_UNLOCK(&thr->mutex); |
216 | croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr)); |
217 | /* NOTREACHED */ |
734689b1 |
218 | } |
458fb581 |
219 | return THREAD_RET_CAST(av); /* Available for anyone to join with */ |
ea0efc06 |
220 | /* us unless we're detached, in which */ |
221 | /* case noone sees the value anyway. */ |
783070da |
222 | #endif |
f0f333f4 |
223 | #else |
224 | return THREAD_RET_CAST(NULL); |
225 | #endif |
d9bb3666 |
226 | } |
227 | |
683929b4 |
228 | static SV * |
cea2e8a9 |
229 | newthread (pTHX_ SV *startsv, AV *initargs, char *classname) |
d9bb3666 |
230 | { |
f0f333f4 |
231 | #ifdef USE_THREADS |
d9bb3666 |
232 | dSP; |
233 | Thread savethread; |
234 | int i; |
683929b4 |
235 | SV *sv; |
ea0efc06 |
236 | int err; |
237 | #ifndef THREAD_CREATE |
940cb80d |
238 | static pthread_attr_t attr; |
239 | static int attr_inited = 0; |
f152979c |
240 | sigset_t fullmask, oldmask; |
13666627 |
241 | static int attr_joinable = PTHREAD_CREATE_JOINABLE; |
b86a2fa7 |
242 | #endif |
1cfa4ec7 |
243 | |
d9bb3666 |
244 | savethread = thr; |
a863c7d1 |
245 | thr = new_struct_thread(thr); |
c4e7bd8d |
246 | /* temporarily pretend to be the child thread in case the |
247 | * XPUSHs() below want to grow the child's stack. This is |
248 | * safe, since the other thread is not yet created, and we |
249 | * are the only ones who know about it */ |
06d86050 |
250 | PERL_SET_THX(thr); |
d9bb3666 |
251 | SPAGAIN; |
bf49b057 |
252 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
0b9678a8 |
253 | "%p: newthread (%p), tid is %u, preparing stack\n", |
254 | savethread, thr, thr->tid)); |
d9bb3666 |
255 | /* The following pushes the arg list and startsv onto the *new* stack */ |
924508f0 |
256 | PUSHMARK(SP); |
d9bb3666 |
257 | /* Could easily speed up the following greatly */ |
734689b1 |
258 | for (i = 0; i <= AvFILL(initargs); i++) |
d9bb3666 |
259 | XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE))); |
260 | XPUSHs(SvREFCNT_inc(startsv)); |
261 | PUTBACK; |
b099ddc0 |
262 | |
263 | /* On your marks... */ |
06d86050 |
264 | PERL_SET_THX(savethread); |
b099ddc0 |
265 | MUTEX_LOCK(&thr->mutex); |
266 | |
ea0efc06 |
267 | #ifdef THREAD_CREATE |
f0f333f4 |
268 | err = THREAD_CREATE(thr, threadstart); |
783070da |
269 | #else |
ea0efc06 |
270 | /* Get set... */ |
f152979c |
271 | sigfillset(&fullmask); |
272 | if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1) |
273 | croak("panic: sigprocmask"); |
940cb80d |
274 | err = 0; |
275 | if (!attr_inited) { |
276 | attr_inited = 1; |
52e1cb5e |
277 | err = pthread_attr_init(&attr); |
5cbe9849 |
278 | # ifdef THREAD_CREATE_NEEDS_STACK |
279 | if (err == 0) |
280 | err = pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK); |
281 | if (err) |
282 | croak("panic: pthread_attr_setstacksize failed"); |
5cbe9849 |
283 | # endif |
0d85d877 |
284 | # ifdef PTHREAD_ATTR_SETDETACHSTATE |
940cb80d |
285 | if (err == 0) |
0d85d877 |
286 | err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable); |
5cbe9849 |
287 | if (err) |
288 | croak("panic: pthread_attr_setdetachstate failed"); |
0d85d877 |
289 | # else |
1cfa4ec7 |
290 | croak("panic: can't pthread_attr_setdetachstate"); |
0d85d877 |
291 | # endif |
52e1cb5e |
292 | } |
940cb80d |
293 | if (err == 0) |
0d85d877 |
294 | err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr); |
ea0efc06 |
295 | #endif |
b099ddc0 |
296 | |
ea0efc06 |
297 | if (err) { |
b099ddc0 |
298 | MUTEX_UNLOCK(&thr->mutex); |
bf49b057 |
299 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
940cb80d |
300 | "%p: create of %p failed %d\n", |
301 | savethread, thr, err)); |
ea0efc06 |
302 | /* Thread creation failed--clean up */ |
199100c8 |
303 | SvREFCNT_dec(thr->cvcache); |
cea2e8a9 |
304 | remove_thread(aTHX_ thr); |
ea0efc06 |
305 | for (i = 0; i <= AvFILL(initargs); i++) |
306 | SvREFCNT_dec(*av_fetch(initargs, i, FALSE)); |
307 | SvREFCNT_dec(startsv); |
308 | return NULL; |
309 | } |
b099ddc0 |
310 | |
ea0efc06 |
311 | #ifdef THREAD_POST_CREATE |
312 | THREAD_POST_CREATE(thr); |
313 | #else |
f152979c |
314 | if (sigprocmask(SIG_SETMASK, &oldmask, 0)) |
315 | croak("panic: sigprocmask"); |
783070da |
316 | #endif |
b099ddc0 |
317 | |
7d901afa |
318 | sv = newSViv(thr->tid); |
199100c8 |
319 | sv_magic(sv, thr->oursv, '~', 0, 0); |
7d901afa |
320 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
b099ddc0 |
321 | sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE)); |
322 | |
323 | /* Go */ |
324 | MUTEX_UNLOCK(&thr->mutex); |
325 | |
326 | return sv; |
f0f333f4 |
327 | #else |
948a8a50 |
328 | # ifdef USE_ITHREADS |
329 | croak("This perl was built for \"ithreads\", which currently does not support Thread.pm.\n" |
330 | "Run \"perldoc Thread\" for more information"); |
331 | # else |
332 | croak("This perl was not built with support for 5.005-style threads.\n" |
333 | "Run \"perldoc Thread\" for more information"); |
334 | # endif |
6b88bc9c |
335 | return &PL_sv_undef; |
f0f333f4 |
336 | #endif |
d9bb3666 |
337 | } |
338 | |
20ce7b12 |
339 | static Signal_t handle_thread_signal (int sig); |
f0f333f4 |
340 | |
f152979c |
341 | static Signal_t |
f0f333f4 |
342 | handle_thread_signal(int sig) |
f152979c |
343 | { |
3aeed370 |
344 | unsigned char c = (unsigned char) sig; |
e8fcfee6 |
345 | dTHX; |
3aeed370 |
346 | /* |
347 | * We're not really allowed to call fprintf in a signal handler |
348 | * so don't be surprised if this isn't robust while debugging |
349 | * with -DL. |
350 | */ |
bf49b057 |
351 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
a835d317 |
352 | "handle_thread_signal: got signal %d\n", sig)); |
3aeed370 |
353 | write(sig_pipe[1], &c, 1); |
f152979c |
354 | } |
355 | |
d9bb3666 |
356 | MODULE = Thread PACKAGE = Thread |
0b9678a8 |
357 | PROTOTYPES: DISABLE |
d9bb3666 |
358 | |
683929b4 |
359 | void |
458fb581 |
360 | new(classname, startsv, ...) |
361 | char * classname |
d9bb3666 |
362 | SV * startsv |
734689b1 |
363 | AV * av = av_make(items - 2, &ST(2)); |
683929b4 |
364 | PPCODE: |
cea2e8a9 |
365 | XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname))); |
d9bb3666 |
366 | |
367 | void |
d9bb3666 |
368 | join(t) |
369 | Thread t |
370 | AV * av = NO_INIT |
371 | int i = NO_INIT |
372 | PPCODE: |
f0f333f4 |
373 | #ifdef USE_THREADS |
272b4648 |
374 | if (t == thr) |
375 | croak("Attempt to join self"); |
bf49b057 |
376 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n", |
a835d317 |
377 | thr, t, ThrSTATE(t))); |
50112d62 |
378 | MUTEX_LOCK(&t->mutex); |
379 | switch (ThrSTATE(t)) { |
14fcddff |
380 | case THRf_R_JOINABLE: |
381 | case THRf_R_JOINED: |
50112d62 |
382 | ThrSETSTATE(t, THRf_R_JOINED); |
383 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
384 | break; |
385 | case THRf_ZOMBIE: |
50112d62 |
386 | ThrSETSTATE(t, THRf_DEAD); |
387 | MUTEX_UNLOCK(&t->mutex); |
cea2e8a9 |
388 | remove_thread(aTHX_ t); |
14fcddff |
389 | break; |
390 | default: |
50112d62 |
391 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
392 | croak("can't join with thread"); |
393 | /* NOTREACHED */ |
394 | } |
ea0efc06 |
395 | JOIN(t, &av); |
7d901afa |
396 | |
0655b981 |
397 | sv_2mortal((SV*)av); |
398 | |
458fb581 |
399 | if (SvTRUE(*av_fetch(av, 0, FALSE))) { |
400 | /* Could easily speed up the following if necessary */ |
401 | for (i = 1; i <= AvFILL(av); i++) |
0655b981 |
402 | XPUSHs(*av_fetch(av, i, FALSE)); |
403 | } |
404 | else { |
2d8e6c8d |
405 | STRLEN n_a; |
406 | char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a); |
bf49b057 |
407 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
458fb581 |
408 | "%p: join propagating die message: %s\n", |
409 | thr, mess)); |
410 | croak(mess); |
411 | } |
f0f333f4 |
412 | #endif |
d9bb3666 |
413 | |
414 | void |
734689b1 |
415 | detach(t) |
d9bb3666 |
416 | Thread t |
417 | CODE: |
f0f333f4 |
418 | #ifdef USE_THREADS |
bf49b057 |
419 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n", |
a835d317 |
420 | thr, t, ThrSTATE(t))); |
50112d62 |
421 | MUTEX_LOCK(&t->mutex); |
422 | switch (ThrSTATE(t)) { |
14fcddff |
423 | case THRf_R_JOINABLE: |
50112d62 |
424 | ThrSETSTATE(t, THRf_R_DETACHED); |
14fcddff |
425 | /* fall through */ |
50112d62 |
426 | case THRf_R_DETACHED: |
14fcddff |
427 | DETACH(t); |
50112d62 |
428 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
429 | break; |
430 | case THRf_ZOMBIE: |
50112d62 |
431 | ThrSETSTATE(t, THRf_DEAD); |
7d901afa |
432 | DETACH(t); |
50112d62 |
433 | MUTEX_UNLOCK(&t->mutex); |
cea2e8a9 |
434 | remove_thread(aTHX_ t); |
14fcddff |
435 | break; |
436 | default: |
50112d62 |
437 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
438 | croak("can't detach thread"); |
439 | /* NOTREACHED */ |
734689b1 |
440 | } |
f0f333f4 |
441 | #endif |
d9bb3666 |
442 | |
443 | void |
7d901afa |
444 | equal(t1, t2) |
445 | Thread t1 |
446 | Thread t2 |
447 | PPCODE: |
6b88bc9c |
448 | PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no); |
7d901afa |
449 | |
450 | void |
451 | flags(t) |
452 | Thread t |
453 | PPCODE: |
f0f333f4 |
454 | #ifdef USE_THREADS |
7d901afa |
455 | PUSHs(sv_2mortal(newSViv(t->flags))); |
f0f333f4 |
456 | #endif |
7d901afa |
457 | |
458 | void |
8dcd6f7b |
459 | done(t) |
460 | Thread t |
461 | PPCODE: |
462 | #ifdef USE_THREADS |
e01a9ca0 |
463 | PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no); |
8dcd6f7b |
464 | #endif |
465 | |
466 | void |
458fb581 |
467 | self(classname) |
468 | char * classname |
7d901afa |
469 | PREINIT: |
470 | SV *sv; |
f0f333f4 |
471 | PPCODE: |
472 | #ifdef USE_THREADS |
7d901afa |
473 | sv = newSViv(thr->tid); |
199100c8 |
474 | sv_magic(sv, thr->oursv, '~', 0, 0); |
7d901afa |
475 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
458fb581 |
476 | PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), |
477 | gv_stashpv(classname, TRUE)))); |
f0f333f4 |
478 | #endif |
7d901afa |
479 | |
50112d62 |
480 | U32 |
481 | tid(t) |
482 | Thread t |
483 | CODE: |
f0f333f4 |
484 | #ifdef USE_THREADS |
50112d62 |
485 | MUTEX_LOCK(&t->mutex); |
486 | RETVAL = t->tid; |
487 | MUTEX_UNLOCK(&t->mutex); |
f0f333f4 |
488 | #else |
489 | RETVAL = 0; |
490 | #endif |
50112d62 |
491 | OUTPUT: |
492 | RETVAL |
493 | |
494 | void |
495 | DESTROY(t) |
496 | SV * t |
497 | PPCODE: |
05c1ce25 |
498 | PUSHs(t ? &PL_sv_yes : &PL_sv_no); |
50112d62 |
499 | |
7d901afa |
500 | void |
734689b1 |
501 | yield() |
d9bb3666 |
502 | CODE: |
f0f333f4 |
503 | { |
504 | #ifdef USE_THREADS |
ea0efc06 |
505 | YIELD; |
f0f333f4 |
506 | #endif |
507 | } |
d9bb3666 |
508 | |
509 | void |
734689b1 |
510 | cond_wait(sv) |
511 | SV * sv |
512 | MAGIC * mg = NO_INIT |
f0f333f4 |
513 | CODE: |
514 | #ifdef USE_THREADS |
2c127b02 |
515 | if (SvROK(sv)) |
734689b1 |
516 | sv = SvRV(sv); |
2c127b02 |
517 | |
734689b1 |
518 | mg = condpair_magic(sv); |
bf49b057 |
519 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv)); |
734689b1 |
520 | MUTEX_LOCK(MgMUTEXP(mg)); |
521 | if (MgOWNER(mg) != thr) { |
522 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
523 | croak("cond_wait for lock that we don't own\n"); |
524 | } |
525 | MgOWNER(mg) = 0; |
d3ef5668 |
526 | COND_SIGNAL(MgOWNERCONDP(mg)); |
734689b1 |
527 | COND_WAIT(MgCONDP(mg), MgMUTEXP(mg)); |
50112d62 |
528 | while (MgOWNER(mg)) |
529 | COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg)); |
734689b1 |
530 | MgOWNER(mg) = thr; |
531 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
532 | #endif |
533 | |
734689b1 |
534 | void |
535 | cond_signal(sv) |
536 | SV * sv |
537 | MAGIC * mg = NO_INIT |
538 | CODE: |
f0f333f4 |
539 | #ifdef USE_THREADS |
50112d62 |
540 | if (SvROK(sv)) |
734689b1 |
541 | sv = SvRV(sv); |
50112d62 |
542 | |
734689b1 |
543 | mg = condpair_magic(sv); |
bf49b057 |
544 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv)); |
734689b1 |
545 | MUTEX_LOCK(MgMUTEXP(mg)); |
546 | if (MgOWNER(mg) != thr) { |
547 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
548 | croak("cond_signal for lock that we don't own\n"); |
549 | } |
550 | COND_SIGNAL(MgCONDP(mg)); |
551 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
552 | #endif |
d9bb3666 |
553 | |
734689b1 |
554 | void |
555 | cond_broadcast(sv) |
556 | SV * sv |
557 | MAGIC * mg = NO_INIT |
f0f333f4 |
558 | CODE: |
559 | #ifdef USE_THREADS |
783070da |
560 | if (SvROK(sv)) |
734689b1 |
561 | sv = SvRV(sv); |
783070da |
562 | |
734689b1 |
563 | mg = condpair_magic(sv); |
bf49b057 |
564 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n", |
683929b4 |
565 | thr, sv)); |
734689b1 |
566 | MUTEX_LOCK(MgMUTEXP(mg)); |
567 | if (MgOWNER(mg) != thr) { |
568 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
569 | croak("cond_broadcast for lock that we don't own\n"); |
570 | } |
571 | COND_BROADCAST(MgCONDP(mg)); |
572 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
573 | #endif |
f152979c |
574 | |
7d901afa |
575 | void |
458fb581 |
576 | list(classname) |
577 | char * classname |
7d901afa |
578 | PREINIT: |
579 | Thread t; |
580 | AV * av; |
581 | SV ** svp; |
582 | int n = 0; |
583 | PPCODE: |
f0f333f4 |
584 | #ifdef USE_THREADS |
7d901afa |
585 | av = newAV(); |
586 | /* |
587 | * Iterate until we have enough dynamic storage for all threads. |
588 | * We mustn't do any allocation while holding threads_mutex though. |
589 | */ |
533c011a |
590 | MUTEX_LOCK(&PL_threads_mutex); |
7d901afa |
591 | do { |
533c011a |
592 | n = PL_nthreads; |
593 | MUTEX_UNLOCK(&PL_threads_mutex); |
7d901afa |
594 | if (AvFILL(av) < n - 1) { |
595 | int i = AvFILL(av); |
596 | for (i = AvFILL(av); i < n - 1; i++) { |
597 | SV *sv = newSViv(0); /* fill in tid later */ |
598 | sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */ |
599 | av_push(av, sv_bless(newRV_noinc(sv), |
458fb581 |
600 | gv_stashpv(classname, TRUE))); |
50112d62 |
601 | |
7d901afa |
602 | } |
603 | } |
533c011a |
604 | MUTEX_LOCK(&PL_threads_mutex); |
605 | } while (n < PL_nthreads); |
606 | n = PL_nthreads; /* Get the final correct value */ |
7d901afa |
607 | |
608 | /* |
609 | * At this point, there's enough room to fill in av. |
610 | * Note that we are holding threads_mutex so the list |
611 | * won't change out from under us but all the remaining |
612 | * processing is "fast" (no blocking, malloc etc.) |
613 | */ |
614 | t = thr; |
615 | svp = AvARRAY(av); |
616 | do { |
0a00ffdb |
617 | SV *sv = (SV*)SvRV(*svp); |
7d901afa |
618 | sv_setiv(sv, t->tid); |
199100c8 |
619 | SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv); |
7d901afa |
620 | SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED; |
621 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
622 | t = t->next; |
0a00ffdb |
623 | svp++; |
7d901afa |
624 | } while (t != thr); |
50112d62 |
625 | /* */ |
533c011a |
626 | MUTEX_UNLOCK(&PL_threads_mutex); |
7d901afa |
627 | /* Truncate any unneeded slots in av */ |
50112d62 |
628 | av_fill(av, n - 1); |
7d901afa |
629 | /* Finally, push all the new objects onto the stack and drop av */ |
924508f0 |
630 | EXTEND(SP, n); |
7d901afa |
631 | for (svp = AvARRAY(av); n > 0; n--, svp++) |
632 | PUSHs(*svp); |
633 | (void)sv_2mortal((SV*)av); |
f0f333f4 |
634 | #endif |
7d901afa |
635 | |
636 | |
f152979c |
637 | MODULE = Thread PACKAGE = Thread::Signal |
638 | |
639 | void |
640 | kill_sighandler_thread() |
641 | PPCODE: |
3aeed370 |
642 | write(sig_pipe[1], "\0", 1); |
6b88bc9c |
643 | PUSHs(&PL_sv_yes); |
f152979c |
644 | |
645 | void |
646 | init_thread_signals() |
647 | PPCODE: |
533c011a |
648 | PL_sighandlerp = handle_thread_signal; |
f152979c |
649 | if (pipe(sig_pipe) == -1) |
650 | XSRETURN_UNDEF; |
6b88bc9c |
651 | PUSHs(&PL_sv_yes); |
f152979c |
652 | |
3aeed370 |
653 | void |
f152979c |
654 | await_signal() |
655 | PREINIT: |
3aeed370 |
656 | unsigned char c; |
ea0efc06 |
657 | SSize_t ret; |
f152979c |
658 | CODE: |
659 | do { |
3aeed370 |
660 | ret = read(sig_pipe[0], &c, 1); |
f152979c |
661 | } while (ret == -1 && errno == EINTR); |
662 | if (ret == -1) |
663 | croak("panic: await_signal"); |
3aeed370 |
664 | ST(0) = sv_newmortal(); |
665 | if (ret) |
22c35a8c |
666 | sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no); |
bf49b057 |
667 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
a835d317 |
668 | "await_signal returning %s\n", SvPEEK(ST(0)))); |
4e35701f |
669 | |
458fb581 |
670 | MODULE = Thread PACKAGE = Thread::Specific |
671 | |
672 | void |
673 | data(classname = "Thread::Specific") |
674 | char * classname |
675 | PPCODE: |
fb223100 |
676 | #ifdef USE_THREADS |
458fb581 |
677 | if (AvFILL(thr->specific) == -1) { |
678 | GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV); |
679 | av_store(thr->specific, 0, newRV((SV*)GvHV(gv))); |
680 | } |
681 | XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE))); |
fb223100 |
682 | #endif |