-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwp.c
261 lines (231 loc) · 5.43 KB
/
lwp.c
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
/*
* lwp.c -- lightweight process creation, destruction and manipulation.
* Copyright (C) 1991-3 Stephen Crane.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
* Imperial College of Science, Technology and Medicine, 180 Queen's
* Gate, London SW7 2BZ, England.
*/
#include <malloc.h>
#include "lwp.h"
extern int sigpause(int);
extern int sigsetmask(int);
struct pcb *currp;
static struct lpq schedq[MAXTPRI], deadq;
static int maxpri; /* maximum priority so far */
static int oldmask;
/*
* growsdown -- check stack direction
*/
static int growsdown (void *x) { int y; return x > (void *)&y; }
/*
* reschedp -- schedule another process. we also check for dead
* processes here and free them.
*/
void reschedp (void)
{
struct pcb *volatile nextp;
long i;
for (i=maxpri+1; i--; )
while ((nextp = hoq (&schedq[i])))
if (nextp->dead) {
if (nextp != currp) {
free (nextp->sbtm);
free (nextp);
} else {
currp = 0;
}
} else
goto change;
change:
/* change context? ... save current context */
if (currp != nextp) {
if (currp) {
if (savep (currp->context) == 0) {
/* restore previous context */
currp = nextp;
restorep (currp->context);
}
} else {
currp = nextp;
restorep (currp->context);
}
}
}
/*
* wrapp -- process entry point.
*/
void wrapp (void)
{
sigsetmask (SIGNALS);
(*currp->entry) (currp->argc, currp->argv, currp->envp);
suicidep ();
}
/*
* nullp -- a null process, always ready to run.
* it (1) sets its priority to maximum to prevent a signal doing a
* reschedule (2) enables signals, waits for one and handles it
* and (3) resets its priority causing a reschedule.
*/
static void nullp (void)
{
for (;;) {
int p = prisetp (MAXTPRI-1);
sigpause (oldmask);
prisetp (p);
}
}
/*
* creatp -- create a process.
*/
struct pcb *creatp (int priority, void (*entry)(), int size, int argc, char *argv[], char *envp)
{
struct pcb *newp;
int *s, x;
void *sp;
if (!(newp = (struct pcb *)calloc (sizeof(struct pcb),1)))
return 0;
size += sizeof(stkalign_t);
if (!(s = (int *)malloc (size)))
return 0;
newp->entry = entry;
newp->argc = argc;
newp->argv = argv;
newp->envp = envp;
sp = (void *)(growsdown (&x)? (size+(int)s)&-sizeof(stkalign_t): (int)s);
if (MAXTPRI <= priority)
priority = MAXTPRI-1;
if (maxpri < (newp->pri = priority))
maxpri = priority;
newp->sbtm = s;
newp->size = size;
newp->dead = 0;
readyp (newp);
readyp (currp);
initp (newp, sp); /* architecture-dependent: from $(ARCH).c */
reschedp ();
return newp;
}
/*
* readyp -- put process on ready queue. if null, assume current.
*/
void readyp (struct pcb *p)
{
if (!p)
p = currp;
toq (&schedq[p->pri], p);
}
/*
* getenvp -- return back-pointer to user's data
*/
void *getenvp (struct pcb *p)
{
if (!p) p = currp;
return p->envp;
}
/*
* setenvp -- set back-pointer to user's data
*/
void setenvp (struct pcb *p, void *ud)
{
if (!p) p = currp;
p->envp = ud;
}
/*
* yieldp -- yield the processor to another thread.
*/
void yieldp (void)
{
readyp (currp);
reschedp ();
}
/*
* suicidep -- cause the current process to be scheduled for deletion.
*/
void suicidep (void)
{
currp->dead = 1;
yieldp ();
}
/*
* destroyp -- mark a process as dead, so it will never be rescheduled.
*/
void destroyp (struct pcb *p) { p->dead = 1; }
/*
* prisetp -- set the thread's priority, returning the old.
* if the new priority is lower than the old, we reschedule.
*/
int prisetp (int new)
{
int old = currp->pri;
if (MAXTPRI <= new)
new = MAXTPRI-1;
if (maxpri < new)
maxpri = new;
currp->pri = new;
if (new < old)
yieldp ();
return old;
}
/*
* prisetp -- set the thread's priority, returning the old.
* Does not do any rescheduling. The thread will run with
* this priority until the next yieldp()
*/
int prichangep (int new)
{
int old = currp->pri;
if (MAXTPRI <= new)
new = MAXTPRI-1;
if (maxpri < new)
maxpri = new;
currp->pri = new;
return old;
}
/*
* initlp -- initialise the coroutine structures
*/
struct pcb *initlp (int pri)
{
extern void onalarm ();
struct lpq *q;
int i, *stack;
struct sigaction s;
if (!(currp = (struct pcb *)calloc (sizeof (struct pcb),1)))
return 0;
if (!(stack = (int *)malloc (64)))
return 0;
if (MAXTPRI <= pri)
pri = MAXTPRI-1;
if (maxpri < pri)
maxpri = pri;
currp->next = 0;
currp->sbtm = stack; /* dummy stack */
currp->pri = pri;
currp->dead = 0;
currp->size = 0; /* dummy size */
for (i=MAXTPRI, q=schedq; i--; q++)
q->head = q->tail = 0;
deadq.head = deadq.tail = 0;
s.sa_handler = &onalarm;
s.sa_flags = SA_INTERRUPT;
sigemptyset(&s.sa_mask);
sigaction (SIGALRM, &s, (struct sigaction *)0);
oldmask = sigsetmask (SIGNALS);
creatp (0, nullp, 8192, 0, 0, 0);
return currp;
}