]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/lwp.c
Include config.h.
[empserver] / src / lib / lwp / lwp.c
1 /*
2  * lwp.c -- lightweight process creation, destruction and manipulation.
3  * Copyright (C) 1991-3 Stephen Crane.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  * 
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
20  * Imperial College of Science, Technology and Medicine, 180 Queen's
21  * Gate, London SW7 2BZ, England.
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "lwp.h"
31 #include "lwpint.h"
32 #include "prototypes.h"
33
34 #if defined(_EMPTH_LWP)
35
36 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
37
38 struct lwpProc *LwpCurrent = NULL;
39 char **LwpContextPtr;
40 int LwpMaxpri = 0;              /* maximum priority so far */
41 int LwpStackGrowsDown;
42
43 static sigset_t oldmask;
44
45 static void lwpStackCheckInit(struct lwpProc *newp);
46 static void lwpStackCheck(struct lwpProc *newp);
47 static void lwpStackCheckUsed(struct lwpProc *newp);
48
49 /* check stack direction */
50 static int
51 growsdown(void *x)
52 {
53     int y;
54     y = (x > (void *)&y);
55     return y;
56 }
57
58 /*
59  * lwpReschedule -- schedule another process.  we also check for dead
60  * processes here and free them.
61  */
62 void
63 lwpReschedule(void)
64 {
65     static int lcount = LCOUNT;
66     static struct lwpProc *nextp;
67     static int i;
68     static sigset_t tmask;
69
70     if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
71         lwpStackCheck(LwpCurrent);
72     }
73     if (!--lcount) {
74         int p = lwpSetPriority(LWP_MAX_PRIO - 1);
75         lcount = LCOUNT;
76         sigprocmask(SIG_SETMASK, &oldmask, &tmask);
77         sigprocmask(SIG_SETMASK, &tmask, &oldmask);
78         LwpCurrent->pri = p;
79     }
80
81     /* destroy dead threads */
82     lwpStatus(LwpCurrent, "Cleaning dead queue");
83     while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
84         if (nextp == LwpCurrent) {
85             lwpStatus(nextp, "OOOPS, we are running already dead thread");
86             exit(1);
87         }
88         lwpDestroy(nextp);
89         lwpStatus(LwpCurrent, "Destroying done");
90     }
91
92     for (i = LwpMaxpri + 1; i--;) {
93         while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
94             if (!nextp->dead)
95                 break;
96             /* clean up after dead bodies */
97             lwpStatus(nextp, "got a dead body");
98             if (nextp == LwpCurrent) {
99                 lwpStatus(nextp, "we are in it -- will bury later");
100                 lwpAddTail(&LwpDeadQ, nextp);
101             } else {
102                 lwpDestroy(nextp);
103             }
104             nextp = 0;
105         }
106         if (nextp)
107             break;
108     }
109     if (LwpCurrent == 0 && nextp == 0) {
110         fprintf(stderr, "No processes to run!\n");
111         exit(1);
112     }
113     if (LwpCurrent != nextp) {
114         struct lwpProc *oldp = LwpCurrent;
115         if (oldp)
116             lwpStatus(oldp, "switch out");
117         LwpCurrent = nextp;
118         *LwpContextPtr = nextp->ud;
119         lwpSwitchContext(oldp, nextp);
120         lwpStatus(nextp, "switch in %d", nextp->pri);
121     }
122 }
123
124 /*
125  * lwpEntryPoint -- process entry point.
126  */
127 void
128 lwpEntryPoint(void)
129 {
130     sigset_t set;
131
132     sigemptyset(&set);
133     sigaddset(&set, SIGALRM);
134     sigprocmask(SIG_SETMASK, &set, &oldmask);
135     *LwpContextPtr = LwpCurrent->ud;
136
137     lwpStatus(LwpCurrent, "starting at entry point");
138     (*LwpCurrent->entry)(LwpCurrent->ud);
139     lwpExit();
140 }
141
142 /*
143  * lwpCreate -- create a process.
144  */
145 struct lwpProc *
146 lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
147 {
148     struct lwpProc *newp;
149
150     if (!(newp = malloc(sizeof(struct lwpProc))))
151         return NULL;
152     newp->flags = flags;
153     newp->name = strdup(name);
154     newp->desc = strdup(desc);
155     newp->entry = entry;
156     newp->argc = argc;
157     newp->argv = argv;
158     newp->ud = ud;
159     newp->dead = 0;
160     if (LWP_MAX_PRIO <= priority)
161         priority = LWP_MAX_PRIO - 1;
162     if (LwpMaxpri < (newp->pri = priority))
163         LwpMaxpri = priority;
164     if (lwpNewContext(newp, stacksz) < 0) {
165         free(newp->name);
166         free(newp->desc);
167         free(newp);
168         return NULL;
169     }
170     lwpStatus(newp, "creating process structure %p (sbtm %p)",
171               newp, newp->sbtm);
172     if (flags & LWP_STACKCHECK)
173         lwpStackCheckInit(newp);
174     lwpReady(newp);
175     lwpReady(LwpCurrent);
176     lwpReschedule();
177     return newp;
178 }
179
180 void
181 lwpDestroy(struct lwpProc *proc)
182 {
183     if (proc->flags & LWP_STACKCHECK) {
184         lwpStackCheckUsed(proc);
185         lwpStackCheck(proc);
186     }
187     lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
188     free(proc->sbtm);
189     free(proc->name);
190     free(proc->desc);
191     free(proc);
192 }
193
194 /*
195  * lwpReady -- put process on ready queue.  if null, assume current.
196  */
197 void
198 lwpReady(struct lwpProc *p)
199 {
200     if (!p)
201         p = LwpCurrent;
202     lwpStatus(p, "added to run queue");
203     lwpAddTail(&LwpSchedQ[p->pri], p);
204 }
205
206 /*
207  * return user's data
208  */
209 void *
210 lwpGetUD(struct lwpProc *p)
211 {
212     if (!p)
213         p = LwpCurrent;
214     return p->ud;
215 }
216
217 /*
218  * set user's data
219  */
220 void
221 lwpSetUD(struct lwpProc *p, char *ud)
222 {
223     if (!p)
224         p = LwpCurrent;
225     p->ud = ud;
226 }
227
228 /*
229  * set name & desc
230  */
231 void
232 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
233 {
234     if (!p)
235         p = LwpCurrent;
236     free(p->name);
237     free(p->desc);
238     p->name = strdup(name);
239     p->desc = strdup(desc);
240 }
241
242 /*
243  * lwpYield -- yield the processor to another thread.
244  */
245 void
246 lwpYield(void)
247 {
248     lwpStatus(LwpCurrent, "yielding control");
249     lwpReady(LwpCurrent);
250     lwpReschedule();
251 }
252
253 /*
254  * cause the current process to be scheduled for deletion.
255  */
256 void
257 lwpExit(void)
258 {
259     lwpStatus(LwpCurrent, "marking self as dead");
260     LwpCurrent->dead = 1;
261     lwpYield();
262 }
263
264 /*
265  * mark another process as dead, so it will never be rescheduled.
266  * remove any lingering FD action
267  */
268 void
269 lwpTerminate(struct lwpProc *p)
270 {
271     lwpStatus(p, "terminating process");
272     p->dead = 1;
273     if (p->fd >= 0)
274         lwpWakeupFd(p);
275 }
276
277 /*
278  * set the thread's priority, returning the old.
279  * if the new priority is lower than the old, we reschedule.
280  */
281 int
282 lwpSetPriority(int new)
283 {
284     int old = LwpCurrent->pri;
285
286     if (LWP_MAX_PRIO <= new)
287         new = LWP_MAX_PRIO - 1;
288     if (LwpMaxpri < new)
289         LwpMaxpri = new;
290     LwpCurrent->pri = new;
291     lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
292     if (new < old)
293         lwpYield();
294     return old;
295 }
296
297 /*
298  * initialise the coroutine structures
299  */
300 struct lwpProc *
301 lwpInitSystem(int pri, char **ctxptr, int flags)
302 {
303     struct lwpQueue *q;
304     int i, *stack, marker;
305     struct lwpProc *sel;
306
307     LwpContextPtr = ctxptr;
308     if (pri < 1)
309         pri = 1;
310     /* *LwpContextPtr = 0; */
311     LwpStackGrowsDown = growsdown(&marker);
312     if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
313         return 0;
314     if (!(stack = malloc(64)))
315         return 0;
316     if (LWP_MAX_PRIO <= pri)
317         pri = LWP_MAX_PRIO - 1;
318     if (LwpMaxpri < pri)
319         LwpMaxpri = pri;
320     LwpCurrent->next = 0;
321     LwpCurrent->sbtm = stack;   /* dummy stack for "main" */
322     LwpCurrent->pri = pri;
323     LwpCurrent->dead = 0;
324     LwpCurrent->flags = flags & ~LWP_STACKCHECK;
325     LwpCurrent->name = "Main";
326     for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
327         q->head = q->tail = 0;
328     LwpDeadQ.head = LwpDeadQ.tail = 0;
329     /* must be lower in priority than us for this to work right */
330     sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
331                     "Select (main loop) Event Handler", 0, 0, 0);
332     lwpInitSelect(sel);
333     return LwpCurrent;
334 }
335
336 /* lwpStackCheckInit
337  *
338  * Initialize the entire stack (including both redzones) with the stack
339  * check mark.  Thus, we can get some indication of how much stack was
340  * used.
341  */
342 static void
343 lwpStackCheckInit(struct lwpProc *newp)
344 {
345     int *p;
346     int *lim = (int *)((char *)newp->sbtm + newp->size);
347
348     for (p = newp->sbtm; p < lim; p++)
349         *p = LWP_CHECKMARK;
350 }
351
352 /* lwpStackCheck
353  *
354  * Check if the thread has overflowed/underflowed its stack.
355  * Should that happen, abort the process, as we cannot recover.
356  */
357 static void
358 lwpStackCheck(struct lwpProc *newp)
359 {
360     int *btm = (int *)(newp->ustack - LWP_REDZONE);
361     int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
362     int n = LWP_REDZONE / sizeof(int);
363     int i, lo_clean, hi_clean, overflow, underflow;
364
365     for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
366     lo_clean = i;
367
368     for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
369     hi_clean = i - 1;
370
371     if (LwpStackGrowsDown) {
372         overflow = n - lo_clean;
373         underflow = n - hi_clean;
374     } else {
375         overflow = n - hi_clean;
376         underflow = n - lo_clean;
377     }
378     if (overflow)
379         logerror("Thread %s stack overflow %d bytes",
380                  newp->name, overflow * (int)sizeof(int));
381     if (underflow)
382         logerror("Thread %s stack underflow %d bytes",
383                  newp->name, underflow * (int)sizeof(int));
384     if (overflow || underflow)
385         abort();
386 }
387
388 /* lwpStackCheckUsed
389  *
390  * Figure out how much stack was used by this thread.
391  */
392 static void
393 lwpStackCheckUsed(struct lwpProc *newp)
394 {
395     int *base = (int *)newp->ustack;
396     int *lim = (int *)(newp->ustack + newp->usize);
397     int total = (lim + 1 - base) * sizeof(int);
398     int used, *p;
399
400     if (LwpStackGrowsDown) {
401         for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
402         used = (lim - p) * sizeof(int);
403     } else {
404         for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
405         used = (p - base + 1) * sizeof(int);
406     }
407     lwpStatus(newp, "Thread stack %d used, %d left, %d total",
408               used, total - used, total);
409 }
410
411 #endif