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