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