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