]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/lwp.c
(lwpCreate): Initialize members runtime and fd. Initializing fd is
[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-2006
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
80     /* destroy dead threads */
81     lwpStatus(LwpCurrent, "Cleaning dead queue");
82     while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
83         if (CANT_HAPPEN(nextp == LwpCurrent))
84             abort();
85         lwpDestroy(nextp);
86         lwpStatus(LwpCurrent, "Destroying done");
87     }
88
89     for (i = LwpMaxpri + 1; i--;) {
90         while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
91             if (!nextp->dead)
92                 break;
93             /* clean up after dead bodies */
94             lwpStatus(nextp, "got a dead body");
95             if (nextp == LwpCurrent) {
96                 lwpStatus(nextp, "we are in it -- will bury later");
97                 lwpAddTail(&LwpDeadQ, nextp);
98             } else {
99                 lwpDestroy(nextp);
100             }
101             nextp = 0;
102         }
103         if (nextp)
104             break;
105     }
106     if (CANT_HAPPEN(LwpCurrent == 0 && nextp == 0))
107         abort();
108     if (LwpCurrent != nextp) {
109         struct lwpProc *oldp = LwpCurrent;
110         if (oldp)
111             lwpStatus(oldp, "switch out");
112         LwpCurrent = nextp;
113         *LwpContextPtr = nextp->ud;
114         lwpSwitchContext(oldp, nextp);
115         lwpStatus(nextp, "switch in %d", nextp->pri);
116     }
117 }
118
119 /*
120  * lwpEntryPoint -- process entry point.
121  */
122 void
123 lwpEntryPoint(void)
124 {
125     lwpStatus(LwpCurrent, "starting at entry point");
126     (*LwpCurrent->entry)(LwpCurrent->ud);
127     lwpExit();
128 }
129
130 /*
131  * lwpCreate -- create a process.
132  */
133 struct lwpProc *
134 lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
135 {
136     struct lwpProc *newp;
137
138     if (!(newp = malloc(sizeof(struct lwpProc))))
139         return NULL;
140     newp->flags = flags;
141     newp->name = strdup(name);
142     newp->desc = strdup(desc);
143     newp->entry = entry;
144     newp->argc = argc;
145     newp->argv = argv;
146     newp->ud = ud;
147     newp->dead = 0;
148     newp->runtime = -1;
149     newp->fd = -1;
150     if (LWP_MAX_PRIO <= priority)
151         priority = LWP_MAX_PRIO - 1;
152     if (LwpMaxpri < (newp->pri = priority))
153         LwpMaxpri = priority;
154     if (lwpNewContext(newp, stacksz) < 0) {
155         free(newp->name);
156         free(newp->desc);
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 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->desc);
181     free(proc);
182 }
183
184 /*
185  * lwpReady -- put process on ready queue.  if null, assume current.
186  */
187 void
188 lwpReady(struct lwpProc *p)
189 {
190     if (!p)
191         p = LwpCurrent;
192     lwpStatus(p, "added to run queue");
193     lwpAddTail(&LwpSchedQ[p->pri], p);
194 }
195
196 /*
197  * return user's data
198  */
199 void *
200 lwpGetUD(struct lwpProc *p)
201 {
202     if (!p)
203         p = LwpCurrent;
204     return p->ud;
205 }
206
207 /*
208  * set user's data
209  */
210 void
211 lwpSetUD(struct lwpProc *p, char *ud)
212 {
213     if (!p)
214         p = LwpCurrent;
215     p->ud = ud;
216 }
217
218 /*
219  * set name & desc
220  */
221 void
222 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
223 {
224     if (!p)
225         p = LwpCurrent;
226     free(p->name);
227     free(p->desc);
228     p->name = strdup(name);
229     p->desc = strdup(desc);
230 }
231
232 /*
233  * lwpYield -- yield the processor to another thread.
234  */
235 void
236 lwpYield(void)
237 {
238     lwpStatus(LwpCurrent, "yielding control");
239     lwpReady(LwpCurrent);
240     lwpReschedule();
241 }
242
243 /*
244  * cause the current process to be scheduled for deletion.
245  */
246 void
247 lwpExit(void)
248 {
249     lwpStatus(LwpCurrent, "marking self as dead");
250     LwpCurrent->dead = 1;
251     lwpYield();
252 }
253
254 /*
255  * mark another process as dead, so it will never be rescheduled.
256  * remove any lingering FD action
257  */
258 void
259 lwpTerminate(struct lwpProc *p)
260 {
261     lwpStatus(p, "terminating process");
262     p->dead = 1;
263     if (p->fd >= 0)
264         lwpWakeupFd(p);
265 }
266
267 /*
268  * set the thread's priority, returning the old.
269  * if the new priority is lower than the old, we reschedule.
270  */
271 int
272 lwpSetPriority(int new)
273 {
274     int old = LwpCurrent->pri;
275
276     if (LWP_MAX_PRIO <= new)
277         new = LWP_MAX_PRIO - 1;
278     if (LwpMaxpri < new)
279         LwpMaxpri = new;
280     LwpCurrent->pri = new;
281     lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
282     if (new < old)
283         lwpYield();
284     return old;
285 }
286
287 /*
288  * initialise the coroutine structures
289  */
290 struct lwpProc *
291 lwpInitSystem(int pri, void **ctxptr, int flags, sigset_t *waitset)
292 {
293     struct lwpQueue *q;
294     int i, *stack, marker;
295     struct lwpProc *sel;
296
297     LwpContextPtr = ctxptr;
298     if (pri < 1)
299         pri = 1;
300     LwpStackGrowsDown = growsdown(&marker);
301     if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
302         return NULL;
303     if (!(stack = malloc(64)))
304         return NULL;
305     if (LWP_MAX_PRIO <= pri)
306         pri = LWP_MAX_PRIO - 1;
307     if (LwpMaxpri < pri)
308         LwpMaxpri = pri;
309     LwpCurrent->next = 0;
310     LwpCurrent->sbtm = stack;   /* dummy stack for "main" */
311     LwpCurrent->pri = pri;
312     LwpCurrent->dead = 0;
313     LwpCurrent->flags = flags & ~LWP_STACKCHECK;
314     LwpCurrent->name = "Main";
315     for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
316         q->head = q->tail = 0;
317     LwpDeadQ.head = LwpDeadQ.tail = 0;
318     lwpInitSigWait(waitset);
319     /* must be lower in priority than us for this to work right */
320     sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
321                     "Select (main loop) Event Handler", 0, 0, 0);
322     lwpInitSelect(sel);
323     return LwpCurrent;
324 }
325
326 /* lwpStackCheckInit
327  *
328  * Initialize the entire stack (including both redzones) with the stack
329  * check mark.  Thus, we can get some indication of how much stack was
330  * used.
331  */
332 static void
333 lwpStackCheckInit(struct lwpProc *newp)
334 {
335     int *p;
336     int *lim = (int *)((char *)newp->sbtm + newp->size);
337
338     for (p = newp->sbtm; p < lim; p++)
339         *p = LWP_CHECKMARK;
340 }
341
342 /* lwpStackCheck
343  *
344  * Check if the thread has overflowed/underflowed its stack.
345  * Should that happen, abort the process, as we cannot recover.
346  */
347 static void
348 lwpStackCheck(struct lwpProc *newp)
349 {
350     int *btm = (int *)(newp->ustack - LWP_REDZONE);
351     int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
352     int n = LWP_REDZONE / sizeof(int);
353     int i, lo_clean, hi_clean, overflow, underflow;
354
355     for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
356     lo_clean = i;
357
358     for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
359     hi_clean = i - 1;
360
361     if (LwpStackGrowsDown) {
362         overflow = n - lo_clean;
363         underflow = n - hi_clean;
364     } else {
365         overflow = n - hi_clean;
366         underflow = n - lo_clean;
367     }
368     if (overflow)
369         logerror("Thread %s stack overflow %d bytes",
370                  newp->name, overflow * (int)sizeof(int));
371     if (underflow)
372         logerror("Thread %s stack underflow %d bytes",
373                  newp->name, underflow * (int)sizeof(int));
374     if (overflow || underflow)
375         abort();
376 }
377
378 /* lwpStackCheckUsed
379  *
380  * Figure out how much stack was used by this thread.
381  */
382 static void
383 lwpStackCheckUsed(struct lwpProc *newp)
384 {
385     int *base = (int *)newp->ustack;
386     int *lim = (int *)(newp->ustack + newp->usize);
387     int total = (lim + 1 - base) * sizeof(int);
388     int used, *p;
389
390     if (LwpStackGrowsDown) {
391         for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
392         used = (lim - p) * sizeof(int);
393     } else {
394         for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
395         used = (p - base + 1) * sizeof(int);
396     }
397     lwpStatus(newp, "Thread stack %d used, %d left, %d total",
398               used, total - used, total);
399 }