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