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