Blob


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