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; |
84 | LOGOP myop; |
76ef7183 |
85 | dSP; |
d9bb3666 |
86 | I32 oldmark = TOPMARK; |
533c011a |
87 | I32 oldscope = PL_scopestack_ix; |
d9bb3666 |
88 | I32 retval; |
458fb581 |
89 | SV *sv; |
0ae6046c |
90 | AV *av; |
14fcddff |
91 | int i, ret; |
783070da |
92 | dJMPENV; |
0ae6046c |
93 | |
94 | #if defined(MULTIPLICITY) |
95 | PERL_SET_INTERP(thr->interp); |
96 | #endif |
97 | |
bf49b057 |
98 | DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p waiting to start\n", |
0b9678a8 |
99 | thr)); |
783070da |
100 | |
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 | */ |
06d86050 |
116 | PERL_SET_THX(thr); |
d9bb3666 |
117 | |
bf49b057 |
118 | DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n", |
683929b4 |
119 | thr, SvPEEK(TOPs))); |
783070da |
120 | |
0ae6046c |
121 | av = newAV(); |
458fb581 |
122 | sv = POPs; |
123 | PUTBACK; |
901b18f1 |
124 | ENTER; |
125 | SAVETMPS; |
458fb581 |
126 | perl_call_sv(sv, G_ARRAY|G_EVAL); |
734689b1 |
127 | SPAGAIN; |
533c011a |
128 | retval = SP - (PL_stack_base + oldmark); |
129 | SP = PL_stack_base + oldmark + 1; |
458fb581 |
130 | if (SvCUR(thr->errsv)) { |
131 | MUTEX_LOCK(&thr->mutex); |
132 | thr->flags |= THRf_DID_DIE; |
133 | MUTEX_UNLOCK(&thr->mutex); |
6b88bc9c |
134 | av_store(av, 0, &PL_sv_no); |
458fb581 |
135 | av_store(av, 1, newSVsv(thr->errsv)); |
bf49b057 |
136 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p died: %s\n", |
6b88bc9c |
137 | thr, SvPV(thr->errsv, PL_na))); |
0655b981 |
138 | } |
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); |
691b83fc |
178 | SvREFCNT_dec(PL_ofs_sv); |
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); |
e01a9ca0 |
192 | thr->thr_done = 1; |
bf49b057 |
193 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
50112d62 |
194 | "%p: threadstart finishing: state is %u\n", |
195 | thr, ThrSTATE(thr))); |
14fcddff |
196 | switch (ThrSTATE(thr)) { |
197 | case THRf_R_JOINABLE: |
198 | ThrSETSTATE(thr, THRf_ZOMBIE); |
199 | MUTEX_UNLOCK(&thr->mutex); |
bf49b057 |
200 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
14fcddff |
201 | "%p: R_JOINABLE thread finished\n", thr)); |
202 | break; |
203 | case THRf_R_JOINED: |
204 | ThrSETSTATE(thr, THRf_DEAD); |
205 | MUTEX_UNLOCK(&thr->mutex); |
cea2e8a9 |
206 | remove_thread(aTHX_ thr); |
bf49b057 |
207 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
14fcddff |
208 | "%p: R_JOINED thread finished\n", thr)); |
209 | break; |
50112d62 |
210 | case THRf_R_DETACHED: |
683929b4 |
211 | ThrSETSTATE(thr, THRf_DEAD); |
14fcddff |
212 | MUTEX_UNLOCK(&thr->mutex); |
458fb581 |
213 | SvREFCNT_dec(av); |
bf49b057 |
214 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
14fcddff |
215 | "%p: DETACHED thread finished\n", thr)); |
cea2e8a9 |
216 | remove_thread(aTHX_ thr); /* This might trigger main thread to finish */ |
14fcddff |
217 | break; |
218 | default: |
219 | MUTEX_UNLOCK(&thr->mutex); |
220 | croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr)); |
221 | /* NOTREACHED */ |
734689b1 |
222 | } |
458fb581 |
223 | return THREAD_RET_CAST(av); /* Available for anyone to join with */ |
ea0efc06 |
224 | /* us unless we're detached, in which */ |
225 | /* case noone sees the value anyway. */ |
783070da |
226 | #endif |
f0f333f4 |
227 | #else |
228 | return THREAD_RET_CAST(NULL); |
229 | #endif |
d9bb3666 |
230 | } |
231 | |
683929b4 |
232 | static SV * |
cea2e8a9 |
233 | newthread (pTHX_ SV *startsv, AV *initargs, char *classname) |
d9bb3666 |
234 | { |
f0f333f4 |
235 | #ifdef USE_THREADS |
d9bb3666 |
236 | dSP; |
237 | Thread savethread; |
238 | int i; |
683929b4 |
239 | SV *sv; |
ea0efc06 |
240 | int err; |
241 | #ifndef THREAD_CREATE |
940cb80d |
242 | static pthread_attr_t attr; |
243 | static int attr_inited = 0; |
f152979c |
244 | sigset_t fullmask, oldmask; |
13666627 |
245 | static int attr_joinable = PTHREAD_CREATE_JOINABLE; |
b86a2fa7 |
246 | #endif |
1cfa4ec7 |
247 | |
d9bb3666 |
248 | savethread = thr; |
a863c7d1 |
249 | thr = new_struct_thread(thr); |
c4e7bd8d |
250 | /* temporarily pretend to be the child thread in case the |
251 | * XPUSHs() below want to grow the child's stack. This is |
252 | * safe, since the other thread is not yet created, and we |
253 | * are the only ones who know about it */ |
06d86050 |
254 | PERL_SET_THX(thr); |
d9bb3666 |
255 | SPAGAIN; |
bf49b057 |
256 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
0b9678a8 |
257 | "%p: newthread (%p), tid is %u, preparing stack\n", |
258 | savethread, thr, thr->tid)); |
d9bb3666 |
259 | /* The following pushes the arg list and startsv onto the *new* stack */ |
924508f0 |
260 | PUSHMARK(SP); |
d9bb3666 |
261 | /* Could easily speed up the following greatly */ |
734689b1 |
262 | for (i = 0; i <= AvFILL(initargs); i++) |
d9bb3666 |
263 | XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE))); |
264 | XPUSHs(SvREFCNT_inc(startsv)); |
265 | PUTBACK; |
b099ddc0 |
266 | |
267 | /* On your marks... */ |
06d86050 |
268 | PERL_SET_THX(savethread); |
b099ddc0 |
269 | MUTEX_LOCK(&thr->mutex); |
270 | |
ea0efc06 |
271 | #ifdef THREAD_CREATE |
f0f333f4 |
272 | err = THREAD_CREATE(thr, threadstart); |
783070da |
273 | #else |
ea0efc06 |
274 | /* Get set... */ |
f152979c |
275 | sigfillset(&fullmask); |
276 | if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1) |
277 | croak("panic: sigprocmask"); |
940cb80d |
278 | err = 0; |
279 | if (!attr_inited) { |
280 | attr_inited = 1; |
52e1cb5e |
281 | err = pthread_attr_init(&attr); |
5cbe9849 |
282 | # ifdef THREAD_CREATE_NEEDS_STACK |
283 | if (err == 0) |
284 | err = pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK); |
285 | if (err) |
286 | croak("panic: pthread_attr_setstacksize failed"); |
287 | #else |
288 | croak("panic: can't pthread_attr_setstacksize"); |
289 | # endif |
0d85d877 |
290 | # ifdef PTHREAD_ATTR_SETDETACHSTATE |
940cb80d |
291 | if (err == 0) |
0d85d877 |
292 | err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable); |
5cbe9849 |
293 | if (err) |
294 | croak("panic: pthread_attr_setdetachstate failed"); |
0d85d877 |
295 | # else |
1cfa4ec7 |
296 | croak("panic: can't pthread_attr_setdetachstate"); |
0d85d877 |
297 | # endif |
52e1cb5e |
298 | } |
940cb80d |
299 | if (err == 0) |
0d85d877 |
300 | err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr); |
ea0efc06 |
301 | #endif |
b099ddc0 |
302 | |
ea0efc06 |
303 | if (err) { |
b099ddc0 |
304 | MUTEX_UNLOCK(&thr->mutex); |
bf49b057 |
305 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
940cb80d |
306 | "%p: create of %p failed %d\n", |
307 | savethread, thr, err)); |
ea0efc06 |
308 | /* Thread creation failed--clean up */ |
199100c8 |
309 | SvREFCNT_dec(thr->cvcache); |
cea2e8a9 |
310 | remove_thread(aTHX_ thr); |
ea0efc06 |
311 | for (i = 0; i <= AvFILL(initargs); i++) |
312 | SvREFCNT_dec(*av_fetch(initargs, i, FALSE)); |
313 | SvREFCNT_dec(startsv); |
314 | return NULL; |
315 | } |
b099ddc0 |
316 | |
ea0efc06 |
317 | #ifdef THREAD_POST_CREATE |
318 | THREAD_POST_CREATE(thr); |
319 | #else |
f152979c |
320 | if (sigprocmask(SIG_SETMASK, &oldmask, 0)) |
321 | croak("panic: sigprocmask"); |
783070da |
322 | #endif |
b099ddc0 |
323 | |
7d901afa |
324 | sv = newSViv(thr->tid); |
199100c8 |
325 | sv_magic(sv, thr->oursv, '~', 0, 0); |
7d901afa |
326 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
b099ddc0 |
327 | sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE)); |
328 | |
329 | /* Go */ |
330 | MUTEX_UNLOCK(&thr->mutex); |
331 | |
332 | return sv; |
f0f333f4 |
333 | #else |
948a8a50 |
334 | # ifdef USE_ITHREADS |
335 | croak("This perl was built for \"ithreads\", which currently does not support Thread.pm.\n" |
336 | "Run \"perldoc Thread\" for more information"); |
337 | # else |
338 | croak("This perl was not built with support for 5.005-style threads.\n" |
339 | "Run \"perldoc Thread\" for more information"); |
340 | # endif |
6b88bc9c |
341 | return &PL_sv_undef; |
f0f333f4 |
342 | #endif |
d9bb3666 |
343 | } |
344 | |
20ce7b12 |
345 | static Signal_t handle_thread_signal (int sig); |
f0f333f4 |
346 | |
f152979c |
347 | static Signal_t |
f0f333f4 |
348 | handle_thread_signal(int sig) |
f152979c |
349 | { |
c5be433b |
350 | dTHXo; |
3aeed370 |
351 | unsigned char c = (unsigned char) sig; |
352 | /* |
353 | * We're not really allowed to call fprintf in a signal handler |
354 | * so don't be surprised if this isn't robust while debugging |
355 | * with -DL. |
356 | */ |
bf49b057 |
357 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
3aeed370 |
358 | "handle_thread_signal: got signal %d\n", sig);); |
359 | write(sig_pipe[1], &c, 1); |
f152979c |
360 | } |
361 | |
d9bb3666 |
362 | MODULE = Thread PACKAGE = Thread |
0b9678a8 |
363 | PROTOTYPES: DISABLE |
d9bb3666 |
364 | |
683929b4 |
365 | void |
458fb581 |
366 | new(classname, startsv, ...) |
367 | char * classname |
d9bb3666 |
368 | SV * startsv |
734689b1 |
369 | AV * av = av_make(items - 2, &ST(2)); |
683929b4 |
370 | PPCODE: |
cea2e8a9 |
371 | XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname))); |
d9bb3666 |
372 | |
373 | void |
d9bb3666 |
374 | join(t) |
375 | Thread t |
376 | AV * av = NO_INIT |
377 | int i = NO_INIT |
378 | PPCODE: |
f0f333f4 |
379 | #ifdef USE_THREADS |
272b4648 |
380 | if (t == thr) |
381 | croak("Attempt to join self"); |
bf49b057 |
382 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n", |
7d901afa |
383 | thr, t, ThrSTATE(t));); |
50112d62 |
384 | MUTEX_LOCK(&t->mutex); |
385 | switch (ThrSTATE(t)) { |
14fcddff |
386 | case THRf_R_JOINABLE: |
387 | case THRf_R_JOINED: |
50112d62 |
388 | ThrSETSTATE(t, THRf_R_JOINED); |
389 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
390 | break; |
391 | case THRf_ZOMBIE: |
50112d62 |
392 | ThrSETSTATE(t, THRf_DEAD); |
393 | MUTEX_UNLOCK(&t->mutex); |
cea2e8a9 |
394 | remove_thread(aTHX_ t); |
14fcddff |
395 | break; |
396 | default: |
50112d62 |
397 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
398 | croak("can't join with thread"); |
399 | /* NOTREACHED */ |
400 | } |
ea0efc06 |
401 | JOIN(t, &av); |
7d901afa |
402 | |
0655b981 |
403 | sv_2mortal((SV*)av); |
404 | |
458fb581 |
405 | if (SvTRUE(*av_fetch(av, 0, FALSE))) { |
406 | /* Could easily speed up the following if necessary */ |
407 | for (i = 1; i <= AvFILL(av); i++) |
0655b981 |
408 | XPUSHs(*av_fetch(av, i, FALSE)); |
409 | } |
410 | else { |
2d8e6c8d |
411 | STRLEN n_a; |
412 | char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a); |
bf49b057 |
413 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
458fb581 |
414 | "%p: join propagating die message: %s\n", |
415 | thr, mess)); |
416 | croak(mess); |
417 | } |
f0f333f4 |
418 | #endif |
d9bb3666 |
419 | |
420 | void |
734689b1 |
421 | detach(t) |
d9bb3666 |
422 | Thread t |
423 | CODE: |
f0f333f4 |
424 | #ifdef USE_THREADS |
bf49b057 |
425 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n", |
7d901afa |
426 | thr, t, ThrSTATE(t));); |
50112d62 |
427 | MUTEX_LOCK(&t->mutex); |
428 | switch (ThrSTATE(t)) { |
14fcddff |
429 | case THRf_R_JOINABLE: |
50112d62 |
430 | ThrSETSTATE(t, THRf_R_DETACHED); |
14fcddff |
431 | /* fall through */ |
50112d62 |
432 | case THRf_R_DETACHED: |
14fcddff |
433 | DETACH(t); |
50112d62 |
434 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
435 | break; |
436 | case THRf_ZOMBIE: |
50112d62 |
437 | ThrSETSTATE(t, THRf_DEAD); |
7d901afa |
438 | DETACH(t); |
50112d62 |
439 | MUTEX_UNLOCK(&t->mutex); |
cea2e8a9 |
440 | remove_thread(aTHX_ t); |
14fcddff |
441 | break; |
442 | default: |
50112d62 |
443 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
444 | croak("can't detach thread"); |
445 | /* NOTREACHED */ |
734689b1 |
446 | } |
f0f333f4 |
447 | #endif |
d9bb3666 |
448 | |
449 | void |
7d901afa |
450 | equal(t1, t2) |
451 | Thread t1 |
452 | Thread t2 |
453 | PPCODE: |
6b88bc9c |
454 | PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no); |
7d901afa |
455 | |
456 | void |
457 | flags(t) |
458 | Thread t |
459 | PPCODE: |
f0f333f4 |
460 | #ifdef USE_THREADS |
7d901afa |
461 | PUSHs(sv_2mortal(newSViv(t->flags))); |
f0f333f4 |
462 | #endif |
7d901afa |
463 | |
464 | void |
8dcd6f7b |
465 | done(t) |
466 | Thread t |
467 | PPCODE: |
468 | #ifdef USE_THREADS |
e01a9ca0 |
469 | PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no); |
8dcd6f7b |
470 | #endif |
471 | |
472 | void |
458fb581 |
473 | self(classname) |
474 | char * classname |
7d901afa |
475 | PREINIT: |
476 | SV *sv; |
f0f333f4 |
477 | PPCODE: |
478 | #ifdef USE_THREADS |
7d901afa |
479 | sv = newSViv(thr->tid); |
199100c8 |
480 | sv_magic(sv, thr->oursv, '~', 0, 0); |
7d901afa |
481 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
458fb581 |
482 | PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), |
483 | gv_stashpv(classname, TRUE)))); |
f0f333f4 |
484 | #endif |
7d901afa |
485 | |
50112d62 |
486 | U32 |
487 | tid(t) |
488 | Thread t |
489 | CODE: |
f0f333f4 |
490 | #ifdef USE_THREADS |
50112d62 |
491 | MUTEX_LOCK(&t->mutex); |
492 | RETVAL = t->tid; |
493 | MUTEX_UNLOCK(&t->mutex); |
f0f333f4 |
494 | #else |
495 | RETVAL = 0; |
496 | #endif |
50112d62 |
497 | OUTPUT: |
498 | RETVAL |
499 | |
500 | void |
501 | DESTROY(t) |
502 | SV * t |
503 | PPCODE: |
6b88bc9c |
504 | PUSHs(&PL_sv_yes); |
50112d62 |
505 | |
7d901afa |
506 | void |
734689b1 |
507 | yield() |
d9bb3666 |
508 | CODE: |
f0f333f4 |
509 | { |
510 | #ifdef USE_THREADS |
ea0efc06 |
511 | YIELD; |
f0f333f4 |
512 | #endif |
513 | } |
d9bb3666 |
514 | |
515 | void |
734689b1 |
516 | cond_wait(sv) |
517 | SV * sv |
518 | MAGIC * mg = NO_INIT |
f0f333f4 |
519 | CODE: |
520 | #ifdef USE_THREADS |
2c127b02 |
521 | if (SvROK(sv)) |
734689b1 |
522 | sv = SvRV(sv); |
2c127b02 |
523 | |
734689b1 |
524 | mg = condpair_magic(sv); |
bf49b057 |
525 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv)); |
734689b1 |
526 | MUTEX_LOCK(MgMUTEXP(mg)); |
527 | if (MgOWNER(mg) != thr) { |
528 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
529 | croak("cond_wait for lock that we don't own\n"); |
530 | } |
531 | MgOWNER(mg) = 0; |
d3ef5668 |
532 | COND_SIGNAL(MgOWNERCONDP(mg)); |
734689b1 |
533 | COND_WAIT(MgCONDP(mg), MgMUTEXP(mg)); |
50112d62 |
534 | while (MgOWNER(mg)) |
535 | COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg)); |
734689b1 |
536 | MgOWNER(mg) = thr; |
537 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
538 | #endif |
539 | |
734689b1 |
540 | void |
541 | cond_signal(sv) |
542 | SV * sv |
543 | MAGIC * mg = NO_INIT |
544 | CODE: |
f0f333f4 |
545 | #ifdef USE_THREADS |
50112d62 |
546 | if (SvROK(sv)) |
734689b1 |
547 | sv = SvRV(sv); |
50112d62 |
548 | |
734689b1 |
549 | mg = condpair_magic(sv); |
bf49b057 |
550 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv)); |
734689b1 |
551 | MUTEX_LOCK(MgMUTEXP(mg)); |
552 | if (MgOWNER(mg) != thr) { |
553 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
554 | croak("cond_signal for lock that we don't own\n"); |
555 | } |
556 | COND_SIGNAL(MgCONDP(mg)); |
557 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
558 | #endif |
d9bb3666 |
559 | |
734689b1 |
560 | void |
561 | cond_broadcast(sv) |
562 | SV * sv |
563 | MAGIC * mg = NO_INIT |
f0f333f4 |
564 | CODE: |
565 | #ifdef USE_THREADS |
783070da |
566 | if (SvROK(sv)) |
734689b1 |
567 | sv = SvRV(sv); |
783070da |
568 | |
734689b1 |
569 | mg = condpair_magic(sv); |
bf49b057 |
570 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n", |
683929b4 |
571 | thr, sv)); |
734689b1 |
572 | MUTEX_LOCK(MgMUTEXP(mg)); |
573 | if (MgOWNER(mg) != thr) { |
574 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
575 | croak("cond_broadcast for lock that we don't own\n"); |
576 | } |
577 | COND_BROADCAST(MgCONDP(mg)); |
578 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
579 | #endif |
f152979c |
580 | |
7d901afa |
581 | void |
458fb581 |
582 | list(classname) |
583 | char * classname |
7d901afa |
584 | PREINIT: |
585 | Thread t; |
586 | AV * av; |
587 | SV ** svp; |
588 | int n = 0; |
589 | PPCODE: |
f0f333f4 |
590 | #ifdef USE_THREADS |
7d901afa |
591 | av = newAV(); |
592 | /* |
593 | * Iterate until we have enough dynamic storage for all threads. |
594 | * We mustn't do any allocation while holding threads_mutex though. |
595 | */ |
533c011a |
596 | MUTEX_LOCK(&PL_threads_mutex); |
7d901afa |
597 | do { |
533c011a |
598 | n = PL_nthreads; |
599 | MUTEX_UNLOCK(&PL_threads_mutex); |
7d901afa |
600 | if (AvFILL(av) < n - 1) { |
601 | int i = AvFILL(av); |
602 | for (i = AvFILL(av); i < n - 1; i++) { |
603 | SV *sv = newSViv(0); /* fill in tid later */ |
604 | sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */ |
605 | av_push(av, sv_bless(newRV_noinc(sv), |
458fb581 |
606 | gv_stashpv(classname, TRUE))); |
50112d62 |
607 | |
7d901afa |
608 | } |
609 | } |
533c011a |
610 | MUTEX_LOCK(&PL_threads_mutex); |
611 | } while (n < PL_nthreads); |
612 | n = PL_nthreads; /* Get the final correct value */ |
7d901afa |
613 | |
614 | /* |
615 | * At this point, there's enough room to fill in av. |
616 | * Note that we are holding threads_mutex so the list |
617 | * won't change out from under us but all the remaining |
618 | * processing is "fast" (no blocking, malloc etc.) |
619 | */ |
620 | t = thr; |
621 | svp = AvARRAY(av); |
622 | do { |
0a00ffdb |
623 | SV *sv = (SV*)SvRV(*svp); |
7d901afa |
624 | sv_setiv(sv, t->tid); |
199100c8 |
625 | SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv); |
7d901afa |
626 | SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED; |
627 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
628 | t = t->next; |
0a00ffdb |
629 | svp++; |
7d901afa |
630 | } while (t != thr); |
50112d62 |
631 | /* */ |
533c011a |
632 | MUTEX_UNLOCK(&PL_threads_mutex); |
7d901afa |
633 | /* Truncate any unneeded slots in av */ |
50112d62 |
634 | av_fill(av, n - 1); |
7d901afa |
635 | /* Finally, push all the new objects onto the stack and drop av */ |
924508f0 |
636 | EXTEND(SP, n); |
7d901afa |
637 | for (svp = AvARRAY(av); n > 0; n--, svp++) |
638 | PUSHs(*svp); |
639 | (void)sv_2mortal((SV*)av); |
f0f333f4 |
640 | #endif |
7d901afa |
641 | |
642 | |
f152979c |
643 | MODULE = Thread PACKAGE = Thread::Signal |
644 | |
645 | void |
646 | kill_sighandler_thread() |
647 | PPCODE: |
3aeed370 |
648 | write(sig_pipe[1], "\0", 1); |
6b88bc9c |
649 | PUSHs(&PL_sv_yes); |
f152979c |
650 | |
651 | void |
652 | init_thread_signals() |
653 | PPCODE: |
533c011a |
654 | PL_sighandlerp = handle_thread_signal; |
f152979c |
655 | if (pipe(sig_pipe) == -1) |
656 | XSRETURN_UNDEF; |
6b88bc9c |
657 | PUSHs(&PL_sv_yes); |
f152979c |
658 | |
3aeed370 |
659 | void |
f152979c |
660 | await_signal() |
661 | PREINIT: |
3aeed370 |
662 | unsigned char c; |
ea0efc06 |
663 | SSize_t ret; |
f152979c |
664 | CODE: |
665 | do { |
3aeed370 |
666 | ret = read(sig_pipe[0], &c, 1); |
f152979c |
667 | } while (ret == -1 && errno == EINTR); |
668 | if (ret == -1) |
669 | croak("panic: await_signal"); |
3aeed370 |
670 | ST(0) = sv_newmortal(); |
671 | if (ret) |
22c35a8c |
672 | sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no); |
bf49b057 |
673 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
3aeed370 |
674 | "await_signal returning %s\n", SvPEEK(ST(0)));); |
4e35701f |
675 | |
458fb581 |
676 | MODULE = Thread PACKAGE = Thread::Specific |
677 | |
678 | void |
679 | data(classname = "Thread::Specific") |
680 | char * classname |
681 | PPCODE: |
fb223100 |
682 | #ifdef USE_THREADS |
458fb581 |
683 | if (AvFILL(thr->specific) == -1) { |
684 | GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV); |
685 | av_store(thr->specific, 0, newRV((SV*)GvHV(gv))); |
686 | } |
687 | XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE))); |
fb223100 |
688 | #endif |