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