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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
|
/* $NetBSD: pthread_sa.c,v 1.26 2004/01/02 19:14:00 cl Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nathan J. Williams.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: pthread_sa.c,v 1.26 2004/01/02 19:14:00 cl Exp $");
#include <err.h>
#include <errno.h>
#include <lwp.h>
#include <sa.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ucontext.h>
#include <unistd.h>
#include <sys/time.h>
#include "pthread.h"
#include "pthread_int.h"
#ifdef PTHREAD_SA_DEBUG
#define SDPRINTF(x) DPRINTF(x)
#else
#define SDPRINTF(x)
#endif
#define UC(t) ((t)->pt_trapuc ? (t)->pt_trapuc : (t)->pt_uc)
#define PUC(t) ((t)->pt_trapuc ? 'T':'U') , UC(t)
extern struct pthread_queue_t pthread__allqueue;
extern pthread_spin_t pthread__deadqueue_lock;
extern struct pthread_queue_t pthread__reidlequeue;
#define PTHREAD_RRTIMER_INTERVAL_DEFAULT 100
static pthread_mutex_t rrtimer_mutex = PTHREAD_MUTEX_INITIALIZER;
static timer_t pthread_rrtimer;
static int pthread_rrtimer_interval = PTHREAD_RRTIMER_INTERVAL_DEFAULT;
int pthread__maxlwps;
#define pthread__sa_id(sap) (pthread__id((sap)->sa_context))
void pthread__upcall(int type, struct sa_t *sas[], int ev, int intr,
void *arg);
void pthread__find_interrupted(int type, struct sa_t *sas[], int ev, int intr,
pthread_t *qhead, pthread_t *schedqhead, pthread_t self);
void pthread__resolve_locks(pthread_t self, pthread_t *interrupted);
extern void pthread__switch_return_point(void);
void
pthread__upcall(int type, struct sa_t *sas[], int ev, int intr, void *arg)
{
pthread_t t, self, next, intqueue, schedqueue;
int flags, i;
siginfo_t *si;
PTHREADD_ADD(PTHREADD_UPCALLS);
self = pthread__self();
self->pt_state = PT_STATE_RUNNING;
if (sas[0]->sa_id > pthread__maxlwps)
pthread__maxlwps = sas[0]->sa_id;
SDPRINTF(("(up %p) type %d LWP %d ev %d intr %d\n", self,
type, sas[0]->sa_id, ev, intr));
if (type == SA_UPCALL_BLOCKED) {
/* Don't handle this SA in the usual processing. */
t = pthread__sa_id(sas[1]);
pthread__assert(self->pt_spinlocks == 0);
if (t->pt_type == PT_THREAD_IDLE) {
pthread_spinlock(self, &pthread__deadqueue_lock);
if (t->pt_flags & PT_FLAG_IDLED) {
PTQ_REMOVE(&pthread__reidlequeue, t, pt_runq);
t->pt_flags &= PT_FLAG_IDLED;
}
pthread_spinunlock(self, &pthread__deadqueue_lock);
}
} else {
/*
* Do per-thread work, including saving the context.
* Briefly run any threads that were in a critical section.
* This includes any upcalls that have been interupted, so
* they can do their own version of this dance.
*/
pthread__find_interrupted(type, sas, ev, intr,
&intqueue, &schedqueue, self);
if (intqueue != self)
pthread__resolve_locks(self, &intqueue);
/* We can take spinlocks now */
if (intqueue != self)
pthread__sched_bulk(self, intqueue);
if (schedqueue != self)
pthread__sched_bulk(self, schedqueue);
pthread__sched_idle2(self);
}
switch (type) {
case SA_UPCALL_BLOCKED:
PTHREADD_ADD(PTHREADD_UP_BLOCK);
t = pthread__sa_id(sas[1]);
SDPRINTF(("(up %p) blocker %d %p(%d)\n", self,
sas[1]->sa_id, t, t->pt_type));
t->pt_blockuc = sas[1]->sa_context;
t->pt_blockedlwp = sas[1]->sa_id;
t->pt_blockgen++;
if (t->pt_cancel)
_lwp_wakeup(t->pt_blockedlwp);
#ifdef PTHREAD__DEBUG
t->blocks++;
#endif
break;
case SA_UPCALL_NEWPROC:
PTHREADD_ADD(PTHREADD_UP_NEW);
break;
case SA_UPCALL_PREEMPTED:
PTHREADD_ADD(PTHREADD_UP_PREEMPT);
break;
case SA_UPCALL_UNBLOCKED:
PTHREADD_ADD(PTHREADD_UP_UNBLOCK);
schedqueue = NULL;
for (i = 0; i < ev; i++) {
t = pthread__sa_id(sas[1 + i]);
/*
* A signal may have been presented to this
* thread while it was in the kernel.
*/
pthread_spinlock(self, &t->pt_flaglock);
flags = t->pt_flags;
pthread_spinunlock(self, &t->pt_flaglock);
if (flags & PT_FLAG_SIGDEFERRED)
pthread__signal_deferred(self, t);
}
if (schedqueue != NULL)
pthread__sched_bulk(self, schedqueue);
break;
case SA_UPCALL_SIGNAL:
PTHREADD_ADD(PTHREADD_UP_SIGNAL);
/*
* Note that we handle signals after handling
* spinlock preemption. This is because spinlocks are only
* used internally to the thread library and we don't want to
* expose the middle of them to a signal. While this means
* that synchronous instruction traps that occur inside
* critical sections in this library (SIGFPE, SIGILL, SIGBUS,
* SIGSEGV) won't be handled at the precise location where
* they occured, that's okay, because (1) we don't use any FP
* and (2) SIGILL/SIGBUS/SIGSEGV should really just core dump.
*
* This also means that a thread that was interrupted to take
* a signal will be on a run queue, and not in upcall limbo.
*/
si = arg;
if (ev)
pthread__signal(self, pthread__sa_id(sas[1]), si);
else
pthread__signal(self, NULL, si);
break;
case SA_UPCALL_SIGEV:
PTHREADD_ADD(PTHREADD_UP_SIGEV);
si = arg;
SDPRINTF(("(up %p) sigev val %x\n", self,
si->si_sigval.sival_int));
if (si->si_sigval.sival_int == PT_ALARMTIMER_MAGIC)
pthread__alarm_process(self, arg);
/*
* PT_RRTIMER_MAGIC doesn't need explicit handling;
* the per-thread work above will put the interrupted
* thread on the back of the run queue, and
* pthread_next() will get one from the front.
*/
break;
case SA_UPCALL_USER:
/* We don't send ourselves one of these. */
default:
pthread__abort();
}
/*
* At this point everything on our list should be scheduled
* (or was an upcall).
*/
pthread__assert(self->pt_spinlocks == 0);
if (self->pt_next) {
SDPRINTF(("(up %p) chain switching to %p (uc: %c %p pc: %lx)\n",
self, self->pt_next, PUC(self->pt_next),
pthread__uc_pc(UC(self->pt_next))));
self->pt_switchtouc = UC(self);
self->pt_switchto = self;
pthread__switch(self, self->pt_next);
/*NOTREACHED*/
pthread__abort();
}
next = pthread__next(self);
next->pt_state = PT_STATE_RUNNING;
SDPRINTF(("(up %p) switching to %p (uc: %c %p pc: %lx)\n",
self, next, PUC(next), pthread__uc_pc(UC(next))));
pthread__upcall_switch(self, next);
/*NOTREACHED*/
pthread__abort();
}
/*
* Build a chain of the threads that were interrupted by the upcall.
* Determine if any of them were upcalls or lock-holders that
* need to be continued early.
*/
void
pthread__find_interrupted(int type, struct sa_t *sas[], int ev, int intr,
pthread_t *intqhead, pthread_t *schedqhead, pthread_t self)
{
int i, resume;
pthread_t victim, nextint, nextsched;
nextint = nextsched = self;
for (i = 0; i < ev + intr; i++) {
resume = 0;
victim = pthread__sa_id(sas[1 + i]);
#ifdef PTHREAD__DEBUG
victim->preempts++;
#endif
victim->pt_trapuc = sas[1 + i]->sa_context;
victim->pt_trapuc->uc_flags &= ~_UC_SIGMASK;
SDPRINTF(("(fi %p) victim %d %p(%d)", self, sas[1 + i]->sa_id,
victim, victim->pt_type));
if (type == SA_UPCALL_UNBLOCKED && i < ev) {
victim->pt_unblockgen++;
#ifdef PTHREAD__DEBUG
if (victim->pt_blockgen != victim->pt_unblockgen) {
SDPRINTF((" unblock before block"));
} else if (victim->pt_type == PT_THREAD_UPCALL ||
victim->pt_spinlocks > 0 ||
victim->pt_next) {
SDPRINTF((" critical"));
} else {
SDPRINTF((" event"));
}
#endif
}
if (victim->pt_type == PT_THREAD_UPCALL) {
/* Case 1: Upcall. Must be resumed. */
SDPRINTF((" upcall"));
resume = 1;
if (victim->pt_next) {
/*
* Case 1A: Upcall in a chain.
*
* Already part of a chain. We want to
* splice this chain into our chain, so
* we have to find the root.
*/
SDPRINTF((" chain"));
for ( ; victim->pt_parent != NULL;
victim = victim->pt_parent) {
SDPRINTF((" parent %p", victim->pt_parent));
pthread__assert(victim->pt_parent != victim);
}
}
} else {
/* Case 2: Normal or idle thread. */
if (victim->pt_spinlocks > 0) {
/* Case 2A: Lockholder. Must be resumed. */
SDPRINTF((" lockholder %d",
victim->pt_spinlocks));
resume = 1;
if (victim->pt_next) {
/*
* Case 2A1: Lockholder on a chain.
* Same deal as 1A.
*/
SDPRINTF((" chain"));
for ( ; victim->pt_parent != NULL;
victim = victim->pt_parent) {
SDPRINTF((" parent %p", victim->pt_parent));
pthread__assert(victim->pt_parent != victim);
}
}
} else {
/* Case 2B: Non-lockholder. */
SDPRINTF((" nonlockholder"));
if (victim->pt_next) {
/*
* Case 2B1: Non-lockholder on a chain
* (must have just released a lock).
*/
SDPRINTF((" chain"));
resume = 1;
for ( ; victim->pt_parent != NULL;
victim = victim->pt_parent) {
SDPRINTF((" parent %p", victim->pt_parent));
pthread__assert(victim->pt_parent != victim);
}
} else if (victim->pt_flags & PT_FLAG_IDLED) {
/*
* Idle threads that have already
* idled must be skipped so
* that we don't (a) idle-queue them
* twice and (b) get the pt_next
* queue of threads to put on the run
* queue mangled by
* pthread__sched_idle2()
*/
SDPRINTF((" idled\n"));
continue;
}
}
}
pthread__assert(victim != self);
if (resume) {
pthread__assert(victim->pt_parent == NULL);
victim->pt_parent = self;
pthread__assert(victim->pt_next == NULL);
victim->pt_next = nextint;
nextint = victim;
} else {
pthread__assert(victim->pt_parent == NULL);
pthread__assert(victim->pt_next == NULL);
victim->pt_next = nextsched;
nextsched = victim;
}
SDPRINTF(("\n"));
}
*intqhead = nextint;
*schedqhead = nextsched;
}
void
pthread__resolve_locks(pthread_t self, pthread_t *intqueuep)
{
pthread_t victim, prev, next, switchto, runq, intqueue;
pthread_t tmp;
pthread_spin_t *lock;
PTHREADD_ADD(PTHREADD_RESOLVELOCKS);
runq = self;
intqueue = *intqueuep;
switchto = NULL;
victim = intqueue;
SDPRINTF(("(rl %p) entered\n", self));
while (intqueue != self) {
/*
* Make a pass over the interrupted queue, cleaning out
* any threads that have dropped all their locks and any
* upcalls that have finished.
*/
SDPRINTF(("(rl %p) intqueue %p\n", self, intqueue));
prev = NULL;
for (victim = intqueue; victim != self; victim = next) {
next = victim->pt_next;
SDPRINTF(("(rl %p) victim %p (uc %c %p)", self,
victim, PUC(victim)));
if (victim->pt_type == PT_THREAD_NORMAL) {
SDPRINTF((" normal"));
if ((victim->pt_spinlocks == 0) &&
((victim->pt_switchto != NULL) ||
(pthread__uc_pc(victim->pt_uc) ==
(intptr_t)pthread__switch_return_point))) {
/*
* We can remove this thread
* from the interrupted queue.
*/
if (prev)
prev->pt_next = next;
else
intqueue = next;
/*
* Clear trap context, which is
* no longer useful.
*/
victim->pt_trapuc = NULL;
/*
* Check whether the victim was
* making a locked switch.
*/
if (victim->pt_heldlock) {
/*
* Yes. Therefore, it's on
* some sleep queue and
* all we have to do is
* release the lock.
*/
lock = victim->pt_heldlock;
victim->pt_heldlock = NULL;
pthread__simple_unlock(lock);
victim->pt_next = NULL;
victim->pt_parent = NULL;
SDPRINTF((" heldlock: %p",lock));
} else {
/*
* No. Queue it for the
* run queue.
*/
victim->pt_next = runq;
runq = victim;
}
} else {
SDPRINTF((" spinlocks: %d",
victim->pt_spinlocks));
/*
* Still holding locks.
* Leave it in the interrupted queue.
*/
prev = victim;
}
} else if (victim->pt_type == PT_THREAD_UPCALL) {
SDPRINTF((" upcall"));
/* Okay, an upcall. */
if (victim->pt_switchto) {
/* We're done with you. */
SDPRINTF((" recyclable"));
/*
* Clear trap context, which is
* no longer useful.
*/
victim->pt_trapuc = NULL;
if (prev)
prev->pt_next = next;
else
intqueue = next;
if (victim->pt_switchto == victim) {
victim->pt_switchto = NULL;
victim->pt_switchtouc = NULL;
SDPRINTF((" switchto self"));
}
pthread__sa_recycle(victim, self);
} else {
/*
* Not finished yet.
* Leave it in the interrupted queue.
*/
prev = victim;
}
} else {
SDPRINTF((" idle"));
/*
* Idle threads should be given an opportunity
* to put themselves on the reidle queue.
* We know that they're done when they have no
* locks and PT_FLAG_IDLED is set.
*/
if (victim->pt_spinlocks != 0) {
/* Still holding locks. */
SDPRINTF((" spinlocks: %d",
victim->pt_spinlocks));
prev = victim;
} else if (!(victim->pt_flags & PT_FLAG_IDLED)) {
/*
* Hasn't yet put itself on the
* reidle queue.
*/
SDPRINTF((" not done"));
prev = victim;
} else {
/* Done! */
if (prev)
prev->pt_next = next;
else
intqueue = next;
/* Permit moving off the reidlequeue */
victim->pt_next = NULL;
}
}
if (victim->pt_switchto) {
PTHREADD_ADD(PTHREADD_SWITCHTO);
switchto = victim->pt_switchto;
switchto->pt_uc = victim->pt_switchtouc;
switchto->pt_trapuc = NULL;
victim->pt_switchto = NULL;
victim->pt_switchtouc = NULL;
SDPRINTF((" switchto: %p (uc %p pc %lx)",
switchto, switchto->pt_uc,
pthread__uc_pc(switchto->pt_uc)));
/*
* Threads can have switchto set to themselves
* if they hit new_preempt. Don't put them
* on the run queue twice.
*/
if (switchto != victim) {
if ((switchto->pt_next) ||
(switchto->pt_spinlocks != 0) ||
(switchto->pt_type == PT_THREAD_UPCALL)) {
/*
* The thread being switched
* to was preempted and
* continued. Find the
* preempter and put it on
* our continuation chain.
*/
SDPRINTF((" switchto chained"));
for ( tmp = switchto;
tmp->pt_parent != NULL;
tmp = tmp->pt_parent)
SDPRINTF((" parent: %p", tmp->pt_parent));
pthread__assert(tmp->pt_parent == NULL);
tmp->pt_parent = self;
pthread__assert(tmp->pt_next == NULL);
tmp->pt_next = intqueue;
intqueue = tmp;
if (switchto->pt_type == PT_THREAD_NORMAL &&
switchto->pt_spinlocks == 0) {
/*
* We set switchto to
* ourselves so that we
* get off the intqueue
*/
switchto->pt_switchto = switchto;
switchto->pt_switchtouc = switchto->pt_uc;
}
} else if (switchto->pt_type ==
PT_THREAD_IDLE &&
switchto->pt_flags & PT_FLAG_IDLED) {
SDPRINTF((" idle done"));
} else {
switchto->pt_next = runq;
runq = switchto;
}
}
switchto = NULL;
}
SDPRINTF(("\n"));
}
if (intqueue != self) {
/*
* There is a chain. Run through the elements
* of the chain. If one of them is preempted again,
* the upcall that handles it will have us on its
* chain, and we will continue here, having
* returned from the switch.
*/
SDPRINTF(("(rl %p) starting chain %p (uc %c %p pc %lx sp %lx)\n",
self, intqueue, PUC(intqueue),
pthread__uc_pc(UC(intqueue)),
pthread__uc_sp(UC(intqueue))));
pthread__switch(self, intqueue);
SDPRINTF(("(rl %p) returned from chain\n",
self));
}
if (self->pt_next) {
/*
* We're on a chain ourselves. Let the other
* threads in the chain run; our parent upcall
* will resume us here after a pass around its
* interrupted queue.
*/
SDPRINTF(("(rl %p) upcall chain switch to %p (uc %c %p pc %lx sp %lx)\n",
self, self->pt_next,
PUC(self->pt_next),
pthread__uc_pc(UC(self->pt_next)),
pthread__uc_sp(UC(self->pt_next))));
pthread__switch(self, self->pt_next);
}
}
SDPRINTF(("(rl %p) exiting\n", self));
*intqueuep = runq;
}
/*
* Stash away an upcall and its stack, possibly recycling it to the kernel.
* Must be running in the context of "new".
*/
void
pthread__sa_recycle(pthread_t old, pthread_t new)
{
old->pt_next = NULL;
old->pt_parent = NULL;
old->pt_state = PT_STATE_RUNNABLE;
#ifdef PTHREAD__DEBUG
if (pthread__debug_newline == 1)
SDPRINTF(("(recycle %p) recycling %p\n", new, old));
else
SDPRINTF((" (recycling %p)", old));
#endif
old->pt_stackinfo.sasi_stackgen++;
}
/*
* Set the round-robin timeslice timer.
*/
static int
pthread__setrrtimer(int msec, int startit)
{
static int rrtimer_created;
struct itimerspec it;
/*
* This check is safe -- we will either be called before there
* are any threads, or with the rrtimer_mutex held.
*/
if (rrtimer_created == 0) {
struct sigevent ev;
ev.sigev_notify = SIGEV_SA;
ev.sigev_signo = 0;
ev.sigev_value.sival_int = (int) PT_RRTIMER_MAGIC;
if (timer_create(CLOCK_VIRTUAL, &ev, &pthread_rrtimer) == -1)
return (errno);
rrtimer_created = 1;
}
if (startit) {
it.it_interval.tv_sec = 0;
it.it_interval.tv_nsec = (long)msec * 1000000;
it.it_value = it.it_interval;
if (timer_settime(pthread_rrtimer, 0, &it, NULL) == -1)
return (errno);
}
pthread_rrtimer_interval = msec;
return (0);
}
/* Get things rolling. */
void
pthread__sa_start(void)
{
pthread_t self, t;
stack_t upcall_stacks[PT_UPCALLSTACKS];
int ret, i, errnosave, flags, rr;
char *value;
flags = 0;
value = getenv("PTHREAD_PREEMPT");
if (value && strcmp(value, "yes") == 0)
flags |= SA_FLAG_PREEMPT;
/*
* It's possible the user's program has set the round-robin
* interval before starting any threads.
*
* Allow the environment variable to override the default.
*
* XXX Should we just nuke the environment variable?
*/
rr = pthread_rrtimer_interval;
value = getenv("PTHREAD_RRTIME");
if (value)
rr = atoi(value);
flags |= SA_FLAG_STACKINFO;
ret = sa_register(pthread__upcall, NULL, flags,
pthread__stackinfo_offset());
if (ret) {
if (errno == ENOSYS)
errx(1,
"libpthread: SA system calls are not available.\n"
);
err(1, "libpthread: sa_register failed\n");
}
self = pthread__self();
for (i = 0; i < PT_UPCALLSTACKS; i++) {
if (0 != (ret = pthread__stackalloc(&t)))
abort();
upcall_stacks[i] = t->pt_stack;
pthread__initthread(self, t);
t->pt_type = PT_THREAD_UPCALL;
t->pt_flags = PT_FLAG_DETACHED;
sigfillset(&t->pt_sigmask); /* XXX hmmmmmm */
/* No locking needed, there are no threads yet. */
PTQ_INSERT_HEAD(&pthread__allqueue, t, pt_allq);
}
ret = sa_stacks(i, upcall_stacks);
if (ret == -1)
abort();
/* XXX
* Calling sa_enable() can mess with errno in bizzare ways,
* because the kernel doesn't really return from it as a
* normal system call. The kernel will launch an upcall
* handler which will jump back to the inside of sa_enable()
* and permit us to continue here. However, since the kernel
* doesn't get a chance to set up the return-state properly,
* the syscall stub may interpret the unmodified register
* state as an error return and stuff an inappropriate value
* into errno.
*
* Therefore, we need to keep errno from being changed by this
* slightly weird control flow.
*/
errnosave = errno;
sa_enable();
errno = errnosave;
/* Start the round-robin timer. */
if (rr != 0 && pthread__setrrtimer(rr, 1) != 0)
abort();
}
/*
* Interface routines to get/set the round-robin timer interval.
*
* XXX Sanity check the behavior for MP systems.
*/
int
pthread_getrrtimer_np(void)
{
return (pthread_rrtimer_interval);
}
int
pthread_setrrtimer_np(int msec)
{
extern int pthread__started;
int ret = 0;
if (msec < 0)
return (EINVAL);
pthread_mutex_lock(&rrtimer_mutex);
ret = pthread__setrrtimer(msec, pthread__started);
pthread_mutex_unlock(&rrtimer_mutex);
return (ret);
}
|