]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/lwp.c
LWP failed to wake up threads sleeping in empth_sleep() while other
[empserver] / src / lib / lwp / lwp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1994-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *  Copyright (C) 1991-3 Stephen Crane
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *  ---
22  *
23  *  See files README, COPYING and CREDITS in the root of the source
24  *  tree for related information and legal notices.  It is expected
25  *  that future projects/authors will amend these files as needed.
26  *
27  *  ---
28  *
29  *  lwp.c: lightweight process creation, destruction and manipulation
30  * 
31  *  Known contributors to this file:
32  *     Markus Armbruster, 2004-2007
33  */
34
35 #include <config.h>
36
37 #include <stdio.h>
38 #include <string.h>
39 #include "lwp.h"
40 #include "lwpint.h"
41 #include "prototypes.h"
42
43 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
44
45 struct lwpProc *LwpCurrent = NULL;
46 void **LwpContextPtr;
47 int LwpMaxpri = 0;              /* maximum priority so far */
48 int LwpStackGrowsDown;
49
50 static void lwpDestroy(struct lwpProc *proc);
51 static void lwpStackCheckInit(struct lwpProc *newp);
52 static void lwpStackCheck(struct lwpProc *newp);
53 static void lwpStackCheckUsed(struct lwpProc *newp);
54
55 /* check stack direction */
56 static int
57 growsdown(void *x)
58 {
59     int y;
60     y = (x > (void *)&y);
61     return y;
62 }
63
64 /*
65  * lwpReschedule -- schedule another process.  we also check for dead
66  * processes here and free them.
67  */
68 void
69 lwpReschedule(void)
70 {
71     static struct lwpProc *nextp;
72     static int i;
73
74     if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
75         lwpStackCheck(LwpCurrent);
76     }
77
78     lwpSigWakeup();
79     lwpWakeupSleep();
80
81     /* destroy dead threads */
82     lwpStatus(LwpCurrent, "Cleaning dead queue");
83     while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
84         if (CANT_HAPPEN(nextp == LwpCurrent))
85             abort();
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 (CANT_HAPPEN(LwpCurrent == 0 && nextp == 0))
108         abort();
109     if (LwpCurrent != nextp) {
110         struct lwpProc *oldp = LwpCurrent;
111         if (oldp)
112             lwpStatus(oldp, "switch out");
113         LwpCurrent = nextp;
114         *LwpContextPtr = nextp->ud;
115         lwpSwitchContext(oldp, nextp);
116         lwpStatus(nextp, "switch in %d", nextp->pri);
117     }
118 }
119
120 /*
121  * lwpEntryPoint -- process entry point.
122  */
123 void
124 lwpEntryPoint(void)
125 {
126     lwpStatus(LwpCurrent, "starting at entry point");
127     (*LwpCurrent->entry)(LwpCurrent->ud);
128     lwpExit();
129 }
130
131 /*
132  * lwpCreate -- create a process.
133  */
134 struct lwpProc *
135 lwpCreate(int priority, void (*entry)(void *), int stacksz,
136           int flags, char *name, int argc, char **argv, void *ud)
137 {
138     struct lwpProc *newp;
139
140     if (!(newp = malloc(sizeof(struct lwpProc))))
141         return NULL;
142     newp->flags = flags;
143     newp->name = strdup(name);
144     newp->entry = entry;
145     newp->argc = argc;
146     newp->argv = argv;
147     newp->ud = ud;
148     newp->dead = 0;
149     newp->runtime = (time_t)-1;
150     newp->fd = -1;
151     if (LWP_MAX_PRIO <= priority)
152         priority = LWP_MAX_PRIO - 1;
153     if (LwpMaxpri < (newp->pri = priority))
154         LwpMaxpri = priority;
155     if (lwpNewContext(newp, stacksz) < 0) {
156         free(newp->name);
157         free(newp);
158         return NULL;
159     }
160     lwpStatus(newp, "creating process structure %p (sbtm %p)",
161               newp, newp->sbtm);
162     if (flags & LWP_STACKCHECK)
163         lwpStackCheckInit(newp);
164     lwpReady(newp);
165     lwpReady(LwpCurrent);
166     lwpReschedule();
167     return newp;
168 }
169
170 static void
171 lwpDestroy(struct lwpProc *proc)
172 {
173     if (proc->flags & LWP_STACKCHECK) {
174         lwpStackCheckUsed(proc);
175         lwpStackCheck(proc);
176     }
177     lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
178     free(proc->sbtm);
179     free(proc->name);
180     free(proc);
181 }
182
183 /*
184  * lwpReady -- put process on ready queue.  if null, assume current.
185  */
186 void
187 lwpReady(struct lwpProc *p)
188 {
189     if (!p)
190         p = LwpCurrent;
191     lwpStatus(p, "added to run queue");
192     lwpAddTail(&LwpSchedQ[p->pri], p);
193 }
194
195 /*
196  * return user's data
197  */
198 void *
199 lwpGetUD(struct lwpProc *p)
200 {
201     if (!p)
202         p = LwpCurrent;
203     return p->ud;
204 }
205
206 /*
207  * set user's data
208  */
209 void
210 lwpSetUD(struct lwpProc *p, char *ud)
211 {
212     if (!p)
213         p = LwpCurrent;
214     p->ud = ud;
215 }
216
217 /*
218  * lwpYield -- yield the processor to another thread.
219  */
220 void
221 lwpYield(void)
222 {
223     lwpStatus(LwpCurrent, "yielding control");
224     lwpReady(LwpCurrent);
225     lwpReschedule();
226 }
227
228 /*
229  * cause the current process to be scheduled for deletion.
230  */
231 void
232 lwpExit(void)
233 {
234     lwpStatus(LwpCurrent, "marking self as dead");
235     LwpCurrent->dead = 1;
236     lwpYield();
237 }
238
239 /*
240  * mark another process as dead, so it will never be rescheduled.
241  * remove any lingering FD action
242  */
243 void
244 lwpTerminate(struct lwpProc *p)
245 {
246     lwpStatus(p, "terminating process");
247     p->dead = 1;
248     lwpWakeup(p);
249 }
250
251 /*
252  * set the thread's priority, returning the old.
253  * if the new priority is lower than the old, we reschedule.
254  */
255 int
256 lwpSetPriority(int new)
257 {
258     int old = LwpCurrent->pri;
259
260     if (LWP_MAX_PRIO <= new)
261         new = LWP_MAX_PRIO - 1;
262     if (LwpMaxpri < new)
263         LwpMaxpri = new;
264     LwpCurrent->pri = new;
265     lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
266     if (new < old)
267         lwpYield();
268     return old;
269 }
270
271 /*
272  * initialise the coroutine structures
273  */
274 struct lwpProc *
275 lwpInitSystem(int pri, void **ctxptr, int flags, sigset_t *waitset)
276 {
277     struct lwpQueue *q;
278     int i, *stack, marker;
279     struct lwpProc *sel;
280
281     LwpContextPtr = ctxptr;
282     if (pri < 1)
283         pri = 1;
284     LwpStackGrowsDown = growsdown(&marker);
285     if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
286         return NULL;
287     if (!(stack = malloc(64)))
288         return NULL;
289     if (LWP_MAX_PRIO <= pri)
290         pri = LWP_MAX_PRIO - 1;
291     if (LwpMaxpri < pri)
292         LwpMaxpri = pri;
293     LwpCurrent->next = 0;
294     LwpCurrent->sbtm = stack;   /* dummy stack for "main" */
295     LwpCurrent->pri = pri;
296     LwpCurrent->dead = 0;
297     LwpCurrent->flags = flags & ~LWP_STACKCHECK;
298     LwpCurrent->name = "Main";
299     for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
300         q->head = q->tail = 0;
301     LwpDeadQ.head = LwpDeadQ.tail = 0;
302     lwpInitSigWait(waitset);
303     /* must be lower in priority than us for this to work right */
304     sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler", 0, 0, 0);
305     lwpInitSelect(sel);
306     return LwpCurrent;
307 }
308
309 /* lwpStackCheckInit
310  *
311  * Initialize the entire stack (including both redzones) with the stack
312  * check mark.  Thus, we can get some indication of how much stack was
313  * used.
314  */
315 static void
316 lwpStackCheckInit(struct lwpProc *newp)
317 {
318     int *p;
319     int *lim = (int *)((char *)newp->sbtm + newp->size);
320
321     for (p = newp->sbtm; p < lim; p++)
322         *p = LWP_CHECKMARK;
323 }
324
325 /* lwpStackCheck
326  *
327  * Check if the thread has overflowed/underflowed its stack.
328  * Should that happen, abort the process, as we cannot recover.
329  */
330 static void
331 lwpStackCheck(struct lwpProc *newp)
332 {
333     int *btm = (int *)(newp->ustack - LWP_REDZONE);
334     int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
335     int n = LWP_REDZONE / sizeof(int);
336     int i, lo_clean, hi_clean, overflow, underflow;
337
338     for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
339     lo_clean = i;
340
341     for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
342     hi_clean = i - 1;
343
344     if (LwpStackGrowsDown) {
345         overflow = n - lo_clean;
346         underflow = n - hi_clean;
347     } else {
348         overflow = n - hi_clean;
349         underflow = n - lo_clean;
350     }
351     if (overflow)
352         logerror("Thread %s stack overflow %d bytes",
353                  newp->name, overflow * (int)sizeof(int));
354     if (underflow)
355         logerror("Thread %s stack underflow %d bytes",
356                  newp->name, underflow * (int)sizeof(int));
357     if (overflow || underflow)
358         abort();
359 }
360
361 /* lwpStackCheckUsed
362  *
363  * Figure out how much stack was used by this thread.
364  */
365 static void
366 lwpStackCheckUsed(struct lwpProc *newp)
367 {
368     int *base = (int *)newp->ustack;
369     int *lim = (int *)(newp->ustack + newp->usize);
370     int total = (lim + 1 - base) * sizeof(int);
371     int used, *p;
372
373     if (LwpStackGrowsDown) {
374         for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
375         used = (lim - p) * sizeof(int);
376     } else {
377         for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
378         used = (p - base + 1) * sizeof(int);
379     }
380     lwpStatus(newp, "Thread stack %d used, %d left, %d total",
381               used, total - used, total);
382 }