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); |
0d85d877 |
282 | # ifdef PTHREAD_ATTR_SETDETACHSTATE |
940cb80d |
283 | if (err == 0) |
0d85d877 |
284 | err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable); |
285 | |
286 | # else |
1cfa4ec7 |
287 | croak("panic: can't pthread_attr_setdetachstate"); |
0d85d877 |
288 | # endif |
52e1cb5e |
289 | } |
940cb80d |
290 | if (err == 0) |
0d85d877 |
291 | err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr); |
ea0efc06 |
292 | #endif |
b099ddc0 |
293 | |
ea0efc06 |
294 | if (err) { |
b099ddc0 |
295 | MUTEX_UNLOCK(&thr->mutex); |
bf49b057 |
296 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
940cb80d |
297 | "%p: create of %p failed %d\n", |
298 | savethread, thr, err)); |
ea0efc06 |
299 | /* Thread creation failed--clean up */ |
199100c8 |
300 | SvREFCNT_dec(thr->cvcache); |
cea2e8a9 |
301 | remove_thread(aTHX_ thr); |
ea0efc06 |
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 | |
0655b981 |
388 | sv_2mortal((SV*)av); |
389 | |
458fb581 |
390 | if (SvTRUE(*av_fetch(av, 0, FALSE))) { |
391 | /* Could easily speed up the following if necessary */ |
392 | for (i = 1; i <= AvFILL(av); i++) |
0655b981 |
393 | XPUSHs(*av_fetch(av, i, FALSE)); |
394 | } |
395 | else { |
2d8e6c8d |
396 | STRLEN n_a; |
397 | char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a); |
bf49b057 |
398 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
458fb581 |
399 | "%p: join propagating die message: %s\n", |
400 | thr, mess)); |
401 | croak(mess); |
402 | } |
f0f333f4 |
403 | #endif |
d9bb3666 |
404 | |
405 | void |
734689b1 |
406 | detach(t) |
d9bb3666 |
407 | Thread t |
408 | CODE: |
f0f333f4 |
409 | #ifdef USE_THREADS |
bf49b057 |
410 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n", |
7d901afa |
411 | thr, t, ThrSTATE(t));); |
50112d62 |
412 | MUTEX_LOCK(&t->mutex); |
413 | switch (ThrSTATE(t)) { |
14fcddff |
414 | case THRf_R_JOINABLE: |
50112d62 |
415 | ThrSETSTATE(t, THRf_R_DETACHED); |
14fcddff |
416 | /* fall through */ |
50112d62 |
417 | case THRf_R_DETACHED: |
14fcddff |
418 | DETACH(t); |
50112d62 |
419 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
420 | break; |
421 | case THRf_ZOMBIE: |
50112d62 |
422 | ThrSETSTATE(t, THRf_DEAD); |
7d901afa |
423 | DETACH(t); |
50112d62 |
424 | MUTEX_UNLOCK(&t->mutex); |
cea2e8a9 |
425 | remove_thread(aTHX_ t); |
14fcddff |
426 | break; |
427 | default: |
50112d62 |
428 | MUTEX_UNLOCK(&t->mutex); |
14fcddff |
429 | croak("can't detach thread"); |
430 | /* NOTREACHED */ |
734689b1 |
431 | } |
f0f333f4 |
432 | #endif |
d9bb3666 |
433 | |
434 | void |
7d901afa |
435 | equal(t1, t2) |
436 | Thread t1 |
437 | Thread t2 |
438 | PPCODE: |
6b88bc9c |
439 | PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no); |
7d901afa |
440 | |
441 | void |
442 | flags(t) |
443 | Thread t |
444 | PPCODE: |
f0f333f4 |
445 | #ifdef USE_THREADS |
7d901afa |
446 | PUSHs(sv_2mortal(newSViv(t->flags))); |
f0f333f4 |
447 | #endif |
7d901afa |
448 | |
449 | void |
8dcd6f7b |
450 | done(t) |
451 | Thread t |
452 | PPCODE: |
453 | #ifdef USE_THREADS |
e01a9ca0 |
454 | PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no); |
8dcd6f7b |
455 | #endif |
456 | |
457 | void |
458fb581 |
458 | self(classname) |
459 | char * classname |
7d901afa |
460 | PREINIT: |
461 | SV *sv; |
f0f333f4 |
462 | PPCODE: |
463 | #ifdef USE_THREADS |
7d901afa |
464 | sv = newSViv(thr->tid); |
199100c8 |
465 | sv_magic(sv, thr->oursv, '~', 0, 0); |
7d901afa |
466 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
458fb581 |
467 | PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), |
468 | gv_stashpv(classname, TRUE)))); |
f0f333f4 |
469 | #endif |
7d901afa |
470 | |
50112d62 |
471 | U32 |
472 | tid(t) |
473 | Thread t |
474 | CODE: |
f0f333f4 |
475 | #ifdef USE_THREADS |
50112d62 |
476 | MUTEX_LOCK(&t->mutex); |
477 | RETVAL = t->tid; |
478 | MUTEX_UNLOCK(&t->mutex); |
f0f333f4 |
479 | #else |
480 | RETVAL = 0; |
481 | #endif |
50112d62 |
482 | OUTPUT: |
483 | RETVAL |
484 | |
485 | void |
486 | DESTROY(t) |
487 | SV * t |
488 | PPCODE: |
6b88bc9c |
489 | PUSHs(&PL_sv_yes); |
50112d62 |
490 | |
7d901afa |
491 | void |
734689b1 |
492 | yield() |
d9bb3666 |
493 | CODE: |
f0f333f4 |
494 | { |
495 | #ifdef USE_THREADS |
ea0efc06 |
496 | YIELD; |
f0f333f4 |
497 | #endif |
498 | } |
d9bb3666 |
499 | |
500 | void |
734689b1 |
501 | cond_wait(sv) |
502 | SV * sv |
503 | MAGIC * mg = NO_INIT |
f0f333f4 |
504 | CODE: |
505 | #ifdef USE_THREADS |
2c127b02 |
506 | if (SvROK(sv)) |
734689b1 |
507 | sv = SvRV(sv); |
2c127b02 |
508 | |
734689b1 |
509 | mg = condpair_magic(sv); |
bf49b057 |
510 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv)); |
734689b1 |
511 | MUTEX_LOCK(MgMUTEXP(mg)); |
512 | if (MgOWNER(mg) != thr) { |
513 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
514 | croak("cond_wait for lock that we don't own\n"); |
515 | } |
516 | MgOWNER(mg) = 0; |
d3ef5668 |
517 | COND_SIGNAL(MgOWNERCONDP(mg)); |
734689b1 |
518 | COND_WAIT(MgCONDP(mg), MgMUTEXP(mg)); |
50112d62 |
519 | while (MgOWNER(mg)) |
520 | COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg)); |
734689b1 |
521 | MgOWNER(mg) = thr; |
522 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
523 | #endif |
524 | |
734689b1 |
525 | void |
526 | cond_signal(sv) |
527 | SV * sv |
528 | MAGIC * mg = NO_INIT |
529 | CODE: |
f0f333f4 |
530 | #ifdef USE_THREADS |
50112d62 |
531 | if (SvROK(sv)) |
734689b1 |
532 | sv = SvRV(sv); |
50112d62 |
533 | |
734689b1 |
534 | mg = condpair_magic(sv); |
bf49b057 |
535 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv)); |
734689b1 |
536 | MUTEX_LOCK(MgMUTEXP(mg)); |
537 | if (MgOWNER(mg) != thr) { |
538 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
539 | croak("cond_signal for lock that we don't own\n"); |
540 | } |
541 | COND_SIGNAL(MgCONDP(mg)); |
542 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
543 | #endif |
d9bb3666 |
544 | |
734689b1 |
545 | void |
546 | cond_broadcast(sv) |
547 | SV * sv |
548 | MAGIC * mg = NO_INIT |
f0f333f4 |
549 | CODE: |
550 | #ifdef USE_THREADS |
783070da |
551 | if (SvROK(sv)) |
734689b1 |
552 | sv = SvRV(sv); |
783070da |
553 | |
734689b1 |
554 | mg = condpair_magic(sv); |
bf49b057 |
555 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n", |
683929b4 |
556 | thr, sv)); |
734689b1 |
557 | MUTEX_LOCK(MgMUTEXP(mg)); |
558 | if (MgOWNER(mg) != thr) { |
559 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
560 | croak("cond_broadcast for lock that we don't own\n"); |
561 | } |
562 | COND_BROADCAST(MgCONDP(mg)); |
563 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
f0f333f4 |
564 | #endif |
f152979c |
565 | |
7d901afa |
566 | void |
458fb581 |
567 | list(classname) |
568 | char * classname |
7d901afa |
569 | PREINIT: |
570 | Thread t; |
571 | AV * av; |
572 | SV ** svp; |
573 | int n = 0; |
574 | PPCODE: |
f0f333f4 |
575 | #ifdef USE_THREADS |
7d901afa |
576 | av = newAV(); |
577 | /* |
578 | * Iterate until we have enough dynamic storage for all threads. |
579 | * We mustn't do any allocation while holding threads_mutex though. |
580 | */ |
533c011a |
581 | MUTEX_LOCK(&PL_threads_mutex); |
7d901afa |
582 | do { |
533c011a |
583 | n = PL_nthreads; |
584 | MUTEX_UNLOCK(&PL_threads_mutex); |
7d901afa |
585 | if (AvFILL(av) < n - 1) { |
586 | int i = AvFILL(av); |
587 | for (i = AvFILL(av); i < n - 1; i++) { |
588 | SV *sv = newSViv(0); /* fill in tid later */ |
589 | sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */ |
590 | av_push(av, sv_bless(newRV_noinc(sv), |
458fb581 |
591 | gv_stashpv(classname, TRUE))); |
50112d62 |
592 | |
7d901afa |
593 | } |
594 | } |
533c011a |
595 | MUTEX_LOCK(&PL_threads_mutex); |
596 | } while (n < PL_nthreads); |
597 | n = PL_nthreads; /* Get the final correct value */ |
7d901afa |
598 | |
599 | /* |
600 | * At this point, there's enough room to fill in av. |
601 | * Note that we are holding threads_mutex so the list |
602 | * won't change out from under us but all the remaining |
603 | * processing is "fast" (no blocking, malloc etc.) |
604 | */ |
605 | t = thr; |
606 | svp = AvARRAY(av); |
607 | do { |
0a00ffdb |
608 | SV *sv = (SV*)SvRV(*svp); |
7d901afa |
609 | sv_setiv(sv, t->tid); |
199100c8 |
610 | SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv); |
7d901afa |
611 | SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED; |
612 | SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE; |
613 | t = t->next; |
0a00ffdb |
614 | svp++; |
7d901afa |
615 | } while (t != thr); |
50112d62 |
616 | /* */ |
533c011a |
617 | MUTEX_UNLOCK(&PL_threads_mutex); |
7d901afa |
618 | /* Truncate any unneeded slots in av */ |
50112d62 |
619 | av_fill(av, n - 1); |
7d901afa |
620 | /* Finally, push all the new objects onto the stack and drop av */ |
924508f0 |
621 | EXTEND(SP, n); |
7d901afa |
622 | for (svp = AvARRAY(av); n > 0; n--, svp++) |
623 | PUSHs(*svp); |
624 | (void)sv_2mortal((SV*)av); |
f0f333f4 |
625 | #endif |
7d901afa |
626 | |
627 | |
f152979c |
628 | MODULE = Thread PACKAGE = Thread::Signal |
629 | |
630 | void |
631 | kill_sighandler_thread() |
632 | PPCODE: |
3aeed370 |
633 | write(sig_pipe[1], "\0", 1); |
6b88bc9c |
634 | PUSHs(&PL_sv_yes); |
f152979c |
635 | |
636 | void |
637 | init_thread_signals() |
638 | PPCODE: |
533c011a |
639 | PL_sighandlerp = handle_thread_signal; |
f152979c |
640 | if (pipe(sig_pipe) == -1) |
641 | XSRETURN_UNDEF; |
6b88bc9c |
642 | PUSHs(&PL_sv_yes); |
f152979c |
643 | |
3aeed370 |
644 | void |
f152979c |
645 | await_signal() |
646 | PREINIT: |
3aeed370 |
647 | unsigned char c; |
ea0efc06 |
648 | SSize_t ret; |
f152979c |
649 | CODE: |
650 | do { |
3aeed370 |
651 | ret = read(sig_pipe[0], &c, 1); |
f152979c |
652 | } while (ret == -1 && errno == EINTR); |
653 | if (ret == -1) |
654 | croak("panic: await_signal"); |
3aeed370 |
655 | ST(0) = sv_newmortal(); |
656 | if (ret) |
22c35a8c |
657 | sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no); |
bf49b057 |
658 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
3aeed370 |
659 | "await_signal returning %s\n", SvPEEK(ST(0)));); |
4e35701f |
660 | |
458fb581 |
661 | MODULE = Thread PACKAGE = Thread::Specific |
662 | |
663 | void |
664 | data(classname = "Thread::Specific") |
665 | char * classname |
666 | PPCODE: |
fb223100 |
667 | #ifdef USE_THREADS |
458fb581 |
668 | if (AvFILL(thr->specific) == -1) { |
669 | GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV); |
670 | av_store(thr->specific, 0, newRV((SV*)GvHV(gv))); |
671 | } |
672 | XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE))); |
fb223100 |
673 | #endif |