]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/lwp.c
(lwpSave, lwpReschedule) [hpc]: Move machine-dependent code to arch.c,
[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 #ifdef BOUNDS_CHECK
35 #include <bounds/fix-args.h>
36 #include <bounds/unchecked.h>
37 #endif
38
39 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
40
41 struct lwpProc *LwpCurrent = NULL;
42 char **LwpContextPtr;
43 int LwpMaxpri = 0;              /* maximum priority so far */
44
45 static sigset_t oldmask;
46
47 static void lwpStackCheckInit(struct lwpProc *newp);
48 static void lwpStackCheck(struct lwpProc *newp);
49 static void lwpStackCheckUsed(struct lwpProc *newp);
50
51 /* check stack direction */
52 static int
53 growsdown(void *x)
54 {
55     int y;
56
57 #ifdef BOUNDS_CHECK
58     BOUNDS_CHECKING_OFF;
59 #endif
60     y = (x > (void *)&y);
61
62 #ifdef BOUNDS_CHECK
63     BOUNDS_CHECKING_ON;
64 #endif
65
66     return y;
67 }
68
69 /*
70  * lwpReschedule -- schedule another process.  we also check for dead
71  * processes here and free them.
72  */
73 void
74 lwpReschedule(void)
75 {
76     static int lcount = LCOUNT;
77     static struct lwpProc *nextp;
78     static int i;
79     static sigset_t tmask;
80
81     if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
82         lwpStackCheck(LwpCurrent);
83     }
84     if (!--lcount) {
85         int p = lwpSetPriority(LWP_MAX_PRIO - 1);
86         lcount = LCOUNT;
87         sigprocmask(SIG_SETMASK, &oldmask, &tmask);
88         sigprocmask(SIG_SETMASK, &tmask, &oldmask);
89         LwpCurrent->pri = p;
90     }
91
92     /* destroy dead threads */
93     lwpStatus(LwpCurrent, "Cleaning dead queue");
94     while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
95         if (nextp == LwpCurrent) {
96             lwpStatus(nextp, "OOOPS, we are running already dead thread");
97             exit(1);
98         }
99         lwpDestroy(nextp);
100         lwpStatus(LwpCurrent, "Destroying done");
101     }
102
103     for (i = LwpMaxpri + 1; i--;) {
104         while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
105             if (!nextp->dead)
106                 break;
107             /* clean up after dead bodies */
108             lwpStatus(nextp, "got a dead body");
109             if (nextp == LwpCurrent) {
110                 lwpStatus(nextp, "we are in it -- will bury later");
111                 lwpAddTail(&LwpDeadQ, nextp);
112             } else {
113                 lwpDestroy(nextp);
114 /*                              fprintf(stderr,  "Destroying done\n"); */
115             }
116             nextp = 0;
117         }
118         if (nextp)
119             break;
120     }
121     if (LwpCurrent == 0 && nextp == 0) {
122         fprintf(stderr, "No processes to run!\n");
123         exit(1);
124     }
125     if (LwpCurrent)
126         lwpStatus(LwpCurrent, "switch out");
127     /* do context switch */
128 #ifdef BOUNDS_CHECK
129     BOUNDS_CHECKING_OFF;
130 #endif
131
132     i = lwpSave(LwpCurrent->context);
133 #ifdef BOUNDS_CHECK
134     BOUNDS_CHECKING_ON;
135 #endif
136
137     if (LwpCurrent != nextp && !(LwpCurrent && i)) {
138         /* restore previous context */
139         lwpStatus(nextp, "switch in %d", nextp->pri);
140         LwpCurrent = nextp;
141         *LwpContextPtr = LwpCurrent->ud;
142 #ifdef BOUNDS_CHECK
143         BOUNDS_CHECKING_OFF;
144 #endif
145         lwpRestore(LwpCurrent->context);
146
147 #ifdef BOUNDS_CHECK
148         BOUNDS_CHECKING_ON;
149 #endif
150     }
151 }
152
153 /*
154  * lwpEntryPoint -- process entry point.
155  */
156 void
157 lwpEntryPoint(void)
158 {
159     sigset_t set;
160
161 #ifdef BOUNDS_CHECK
162     BOUNDS_CHECKING_OFF;
163 #endif
164     sigemptyset(&set);
165     sigaddset(&set, SIGALRM);
166     sigprocmask(SIG_SETMASK, &set, &oldmask);
167     *LwpContextPtr = LwpCurrent->ud;
168
169     lwpStatus(LwpCurrent, "starting at entry point");
170     (*LwpCurrent->entry)(LwpCurrent->ud);
171     lwpExit();
172 #ifdef BOUNDS_CHECK
173     BOUNDS_CHECKING_ON;
174 #endif
175
176
177 }
178
179 /*
180  * lwpCreate -- create a process.
181  */
182 struct lwpProc *
183 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
184 {
185     struct lwpProc *newp;
186     char *s, *sp;
187     int redsize, x;
188 #ifdef UCONTEXT
189     stack_t usp;
190 #endif /* UCONTEXT */
191
192     if (!(newp = malloc(sizeof(struct lwpProc))))
193         return 0;
194     /* Add a 1K buffer on each side of the stack */
195     redsize = flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
196     size += 2 * redsize;
197     size += LWP_EXTRASTACK;
198     size += sizeof(stkalign_t);
199     if (!(s = malloc(size)))
200         return 0;
201     newp->flags = flags;
202     newp->name = strdup(name);
203     newp->desc = strdup(desc);
204     newp->entry = entry;
205     newp->argc = argc;
206     newp->argv = argv;
207     newp->ud = ud;
208     if (growsdown(&x)) {
209         sp = s + size - sizeof(stkalign_t) - LWP_EXTRASTACK - redsize;
210         sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
211         newp->lowmark = sp + LWP_EXTRASTACK;
212         newp->himark = s;
213     } else {
214         sp = s + LWP_EXTRASTACK + redsize;
215         sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
216         newp->lowmark = s;
217         newp->himark = s + size - LWP_REDZONE;
218     }
219     if (LWP_MAX_PRIO <= priority)
220         priority = LWP_MAX_PRIO - 1;
221     if (LwpMaxpri < (newp->pri = priority))
222         LwpMaxpri = priority;
223     newp->sbtm = s;
224     newp->size = size;
225     newp->dead = 0;
226     if (flags & LWP_STACKCHECK)
227         lwpStackCheckInit(newp);
228     lwpStatus(newp, "creating process structure sbtm: %p",
229               newp->sbtm);
230     lwpReady(newp);
231     lwpReady(LwpCurrent);
232 #ifdef UCONTEXT
233     usp.ss_sp = sp;
234     usp.ss_size = size;
235     usp.ss_flags = 0;
236     lwpInitContext(newp, &usp); /* architecture-dependent: from arch.c */
237 #else  /* UCONTEXT */
238     lwpInitContext(newp, sp);   /* architecture-dependent: from arch.c */
239 #endif /* UCONTEXT */
240     lwpReschedule();
241     return newp;
242 }
243
244 void
245 lwpDestroy(struct lwpProc *proc)
246 {
247     if (proc->flags & LWP_STACKCHECK) {
248         lwpStackCheckUsed(proc);
249         lwpStackCheck(proc);
250     }
251     lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
252     proc->entry = 0;
253     proc->ud = 0;
254     proc->argv = 0;
255     free(proc->sbtm);
256     free(proc->name);
257     free(proc->desc);
258     proc->name = 0;
259     proc->desc = 0;
260     proc->sbtm = 0;
261     proc->lowmark = 0;
262     proc->himark = 0;
263     free(proc);
264 }
265
266 /*
267  * lwpReady -- put process on ready queue.  if null, assume current.
268  */
269 void
270 lwpReady(struct lwpProc *p)
271 {
272     if (!p)
273         p = LwpCurrent;
274     lwpStatus(p, "added to run queue");
275     lwpAddTail(&LwpSchedQ[p->pri], p);
276 }
277
278 /*
279  * return user's data
280  */
281 void *
282 lwpGetUD(struct lwpProc *p)
283 {
284     if (!p)
285         p = LwpCurrent;
286     return p->ud;
287 }
288
289 /*
290  * set user's data
291  */
292 void
293 lwpSetUD(struct lwpProc *p, char *ud)
294 {
295     if (!p)
296         p = LwpCurrent;
297     p->ud = ud;
298 }
299
300 /*
301  * set name & desc
302  */
303 void
304 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
305 {
306     if (!p)
307         p = LwpCurrent;
308     free(p->name);
309     free(p->desc);
310     p->name = strdup(name);
311     p->desc = strdup(desc);
312 }
313
314 /*
315  * lwpYield -- yield the processor to another thread.
316  */
317 void
318 lwpYield(void)
319 {
320     lwpStatus(LwpCurrent, "yielding control");
321     lwpReady(LwpCurrent);
322     lwpReschedule();
323 }
324
325 /*
326  * cause the current process to be scheduled for deletion.
327  */
328 void
329 lwpExit(void)
330 {
331     lwpStatus(LwpCurrent, "marking self as dead");
332     LwpCurrent->dead = 1;
333     lwpYield();
334 }
335
336 /*
337  * mark another process as dead, so it will never be rescheduled.
338  * remove any lingering FD action
339  */
340 void
341 lwpTerminate(struct lwpProc *p)
342 {
343     lwpStatus(p, "terminating process");
344     p->dead = 1;
345     if (p->fd >= 0)
346         lwpWakeupFd(p);
347 }
348
349 /*
350  * set the thread's priority, returning the old.
351  * if the new priority is lower than the old, we reschedule.
352  */
353 int
354 lwpSetPriority(int new)
355 {
356     int old = LwpCurrent->pri;
357
358     if (LWP_MAX_PRIO <= new)
359         new = LWP_MAX_PRIO - 1;
360     if (LwpMaxpri < new)
361         LwpMaxpri = new;
362     LwpCurrent->pri = new;
363     lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
364     if (new < old)
365         lwpYield();
366     return old;
367 }
368
369 /*
370  * initialise the coroutine structures
371  */
372 struct lwpProc *
373 lwpInitSystem(int pri, char **ctxptr, int flags)
374 {
375     struct lwpQueue *q;
376     int i, *stack;
377     struct lwpProc *sel;
378
379     LwpContextPtr = ctxptr;
380     if (pri < 1)
381         pri = 1;
382     /* *LwpContextPtr = 0; */
383     if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
384         return 0;
385     if (!(stack = malloc(64)))
386         return 0;
387     if (LWP_MAX_PRIO <= pri)
388         pri = LWP_MAX_PRIO - 1;
389     if (LwpMaxpri < pri)
390         LwpMaxpri = pri;
391     LwpCurrent->next = 0;
392     LwpCurrent->sbtm = stack;   /* dummy stack for "main" */
393     LwpCurrent->pri = pri;
394     LwpCurrent->dead = 0;
395     LwpCurrent->flags = flags & ~LWP_STACKCHECK;
396     LwpCurrent->name = "Main";
397     for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
398         q->head = q->tail = 0;
399     LwpDeadQ.head = LwpDeadQ.tail = 0;
400     /* must be lower in priority than us for this to work right */
401     sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
402                     "Select (main loop) Event Handler", 0, 0, 0);
403     lwpInitSelect(sel);
404     return LwpCurrent;
405 }
406
407 /* lwpStackCheckInit
408  *
409  * Initialize the entire stack (including both redzones) with the stack
410  * check mark.  Thus, we can get some indication of how much stack was
411  * used.
412  */
413 static void
414 lwpStackCheckInit(struct lwpProc *newp)
415 {
416     register int i;
417     register long *lp;
418
419     int lim = newp->size / sizeof(long);
420     if (!newp || !newp->sbtm)
421         return;
422     for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
423         *lp = LWP_CHECKMARK;
424     }
425 }
426
427 /* lwpStackCheck
428  *
429  * Check if the thread has overflowed/underflowed its stack.
430  * Should that happen, abort the process, as we cannot recover.
431  */
432 static void
433 lwpStackCheck(struct lwpProc *newp)
434 {
435     register int end, amt;
436     register unsigned int i;
437     register long *lp;
438     register int growsDown;
439     int marker;
440
441     if (CANT_HAPPEN(!newp || !newp->himark || !newp->lowmark))
442         return;
443     growsDown = growsdown(&marker);
444     for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
445          i++, lp++) {
446         if (*lp == LWP_CHECKMARK)
447             continue;
448         /* Stack overflow. */
449         if (growsDown) {
450             end = i;
451             while (i < LWP_REDZONE / sizeof(long)) {
452                 if (*lp++ != LWP_CHECKMARK)
453                     end = i;
454                 i++;
455             }
456             amt = (end + 1) * sizeof(long);
457         } else {
458             amt = (i + 1) * sizeof(long);
459         }
460         logerror("Thread %s stack overflow %d bytes (of %u)",
461                  newp->name, amt,
462                  newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
463         abort();
464     }
465     for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
466          i++, lp++) {
467         if (*lp == LWP_CHECKMARK)
468             continue;
469         /* Stack underflow. */
470         if (growsDown) {
471             end = i;
472             while (i < LWP_REDZONE / sizeof(long)) {
473                 if (*lp++ != LWP_CHECKMARK)
474                     end = i;
475                 i++;
476             }
477             amt = (end + 1) * sizeof(long);
478         } else {
479             amt = (LWP_REDZONE - i + 1) * sizeof(long);
480         }
481         logerror("Thread %s stack underflow %d bytes (of %u)",
482                   newp->name, amt,
483                  newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
484         abort();
485     }
486 }
487
488 /* lwpStackCheckUsed
489  *
490  * Figure out how much stack was used by this thread.
491  */
492 static void
493 lwpStackCheckUsed(struct lwpProc *newp)
494 {
495     register int i;
496     register long *lp;
497     register int lim;
498     int marker;
499
500     if (!newp || !newp->sbtm)
501         return;
502     lim = newp->size / sizeof(long);
503     if (growsdown(&marker)) {
504         /* Start at the bottom and find first non checkmark. */
505         for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
506             if (*lp != LWP_CHECKMARK) {
507                 break;
508             }
509         }
510     } else {
511         /* Start at the top and find first non checkmark. */
512         lp = newp->sbtm;
513         lp += newp->size / sizeof(long);
514         lp--;
515         for (i = 0; i < lim; i++, lp--) {
516             if (*lp != LWP_CHECKMARK) {
517                 break;
518             }
519         }
520     }
521     lwpStatus(newp, "Thread stack %lu used %lu left %lu total",
522               labs((char *)lp - (char *)newp->lowmark) - LWP_REDZONE,
523               labs((char *)newp->himark - (char *)lp) - LWP_REDZONE,
524               labs((char *)newp->himark - (char *)newp->lowmark) - LWP_REDZONE);
525 }
526
527 #endif