Blob


1 #include <signal.h>
2 #include "threadimpl.h"
4 //static Thread *runthread(Proc*);
6 static char *_psstate[] = {
7 "Dead",
8 "Running",
9 "Ready",
10 "Rendezvous",
11 };
13 static char*
14 psstate(int s)
15 {
16 if(s < 0 || s >= nelem(_psstate))
17 return "unknown";
18 return _psstate[s];
19 }
21 void
22 _schedinit(void *arg)
23 {
24 Proc *p;
25 Thread *t;
26 extern void ignusr1(void), _threaddie(int);
27 ignusr1();
28 signal(SIGTERM, _threaddie);
30 p = arg;
31 lock(&p->lock);
32 p->pid = _threadgetpid();
33 _threadsetproc(p);
34 unlock(&p->lock);
35 while(_setlabel(&p->sched))
36 ;
37 _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
38 if(_threadexitsallstatus)
39 exits(_threadexitsallstatus);
40 lock(&p->lock);
41 if((t=p->thread) != nil){
42 p->thread = nil;
43 if(t->moribund){
44 if(t->moribund != 1)
45 fprint(2, "moribund %d\n", t->moribund);
46 assert(t->moribund == 1);
47 t->state = Dead;
48 if(t->prevt)
49 t->prevt->nextt = t->nextt;
50 else
51 p->threads.head = t->nextt;
52 if(t->nextt)
53 t->nextt->prevt = t->prevt;
54 else
55 p->threads.tail = t->prevt;
56 unlock(&p->lock);
57 if(t->inrendez){
58 _threadflagrendez(t);
59 _threadbreakrendez();
60 }
61 _stackfree(t->stk);
62 free(t->cmdname);
63 free(t); /* XXX how do we know there are no references? */
64 p->nthreads--;
65 t = nil;
66 _sched();
67 }
68 if(p->needexec){
69 t->ret = _schedexec(&p->exec);
70 p->needexec = 0;
71 }
72 if(p->newproc){
73 t->ret = _schedfork(p->newproc);
74 if(t->ret < 0){
75 //fprint(2, "_schedfork: %r\n");
76 abort();
77 }
78 p->newproc = nil;
79 }
80 t->state = t->nextstate;
81 if(t->state == Ready)
82 _threadready(t);
83 }
84 unlock(&p->lock);
85 _sched();
86 }
88 static Thread*
89 runthread(Proc *p)
90 {
91 Thread *t;
92 Tqueue *q;
94 if(p->nthreads==0)
95 return nil;
96 q = &p->ready;
97 lock(&p->readylock);
98 if(q->head == nil){
99 q->asleep = 1;
100 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
101 unlock(&p->readylock);
102 while(rendezvous((ulong)q, 0) == ~0){
103 if(_threadexitsallstatus)
104 exits(_threadexitsallstatus);
106 /* lock picked up from _threadready */
108 t = q->head;
109 q->head = t->next;
110 unlock(&p->readylock);
111 return t;
114 void
115 _sched(void)
117 Proc *p;
118 Thread *t;
120 Resched:
121 p = _threadgetproc();
122 //fprint(2, "p %p\n", p);
123 if((t = p->thread) != nil){
124 if((ulong)&p < (ulong)t->stk){ /* stack overflow */
125 fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
126 abort();
128 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
129 // psstate(t->state), &t->sched, &p->sched);
130 if(_setlabel(&t->sched)==0)
131 _gotolabel(&p->sched);
132 return;
133 }else{
134 t = runthread(p);
135 if(t == nil){
136 _threaddebug(DBGSCHED, "all threads gone; exiting");
137 _threaddelproc();
138 _schedexit(p);
140 // _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
141 p->thread = t;
142 if(t->moribund){
143 _threaddebug(DBGSCHED, "%d.%d marked to die");
144 goto Resched;
146 t->state = Running;
147 t->nextstate = Ready;
148 _gotolabel(&t->sched);
152 long
153 threadstack(void)
155 Proc *p;
156 Thread *t;
158 p = _threadgetproc();
159 t = p->thread;
160 return (ulong)&p - (ulong)t->stk;
163 void
164 _threadready(Thread *t)
166 Tqueue *q;
168 assert(t->state == Ready);
169 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
170 q = &t->proc->ready;
171 lock(&t->proc->readylock);
172 t->next = nil;
173 if(q->head==nil)
174 q->head = t;
175 else
176 q->tail->next = t;
177 q->tail = t;
178 if(q->asleep){
179 assert(q->asleep == 1);
180 q->asleep = 0;
181 /* lock passes to runthread */
182 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
183 while(rendezvous((ulong)q, 0) == ~0){
184 if(_threadexitsallstatus)
185 exits(_threadexitsallstatus);
187 }else
188 unlock(&t->proc->readylock);
191 void
192 yield(void)
194 _sched();