]> git.pond.sub.org Git - empserver/blob - include/empthread.h
(PP_MAIN, PP_UPDATE, PP_SHUTDOWN, PP_SCHED, PP_TIMESTAMP, PP_PLAYER)
[empserver] / include / empthread.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  empthread.h: Definitions for Empire threading
29  * 
30  *  Known contributors to this file:
31  *     Sasha Mikheev
32  *     Doug Hay, 1998
33  *     Steve McClure, 1998
34  */
35
36 /*
37  * This header defines Empire's abstract thread interface.  There are
38  * several concrete implementations.
39  *
40  * Empire threads are non-preemptive, i.e. they run until they
41  * voluntarily yield the processor.  The thread scheduler then picks
42  * one of the runnable threads with the highest priority.  Priorities
43  * are static.  Empire code relies on these properties heavily.  The
44  * most common form of yielding the processor is sleeping for some
45  * event to happen.
46  */
47
48 #ifndef _EMTHREAD_H_
49 #define _EMTHREAD_H_
50
51 /* thread priorities */
52 enum {
53     PP_MAIN      = 7,
54     PP_UPDATE    = 6,
55     PP_SHUTDOWN  = 5,
56     PP_SCHED     = 4,
57     PP_TIMESTAMP = 2,
58     PP_PLAYER    = 3,
59     PP_ACCEPT    = 3,
60     PP_KILLIDLE  = 2
61 };
62
63 #ifdef EMPTH_LWP
64 #include "lwp.h"
65
66 /* Abstract data types */
67
68 /* empth_t * represents a thread.  */
69 typedef struct lwpProc empth_t;
70
71 /* empth_sem_t * represents a semaphore */
72 typedef struct lwpSem empth_sem_t;
73
74 /* Flags for empth_select(): whether to sleep on input or output */
75 #define EMPTH_FD_READ     LWP_FD_READ
76 #define EMPTH_FD_WRITE    LWP_FD_WRITE
77
78 /* Flags for empth_init() and empth_create() */
79 /* Request debug prints */
80 #define EMPTH_PRINT       LWP_PRINT
81 /* Request stack checking */
82 #define EMPTH_STACKCHECK  LWP_STACKCHECK
83
84 #endif /* EMPTH_LWP */
85
86 #ifdef EMPTH_POSIX
87 #include <pthread.h>
88 #define EMPTH_FD_READ   0x1
89 #define EMPTH_FD_WRITE  0x2
90
91 #define EMPTH_PRINT       0x1
92 #define EMPTH_STACKCHECK  0x2
93
94 typedef struct empth_t empth_t;
95 typedef struct empth_sem_t empth_sem_t;
96
97 #endif /* EMPTH_POSIX */
98
99 #ifdef EMPTH_W32
100 /* The Windows NT Threads */
101 #define EMPTH_FD_READ   0x1
102 #define EMPTH_FD_WRITE  0x2
103
104 #define EMPTH_PRINT       0x1
105 #define EMPTH_STACKCHECK  0x2
106
107 typedef struct loc_Thread_t empth_t;
108 typedef struct loc_Sem_t empth_sem_t;
109
110 void empth_request_shutdown(void);
111 #endif /* EMPTH_W32 */
112
113 /*
114  * Initialize thread package.
115  * CTX points to a thread context variable; see empth_create().
116  * FLAGS request optional features.
117  * Should return 0 on success, -1 on error, but currently always
118  * returns 0.
119  */
120 int empth_init(void **ctx, int flags);
121
122 /*
123  * Create a new thread.
124  * PRIO is the scheduling priority.
125  * ENTRY is the entry point.  It will be called with argument UD.
126  * Thread stack is at least SIZE bytes.
127  * FLAGS should be the same as were passed to empth_init(), or zero.
128  * NAME is the threads name, and DESC its description.  These are used
129  * for logging and debugging.
130  * UD is the value to pass to ENTRY.  It is also assigned to the
131  * context variable defined with empth_init() whenever the thread gets
132  * scheduled.
133  * Return the thread, or NULL on error.
134  */
135 empth_t *empth_create(int prio, void (*entry)(void *),
136                       int size, int flags, char *name, char *desc, void *ud);
137
138 /*
139  * Return the current thread.
140  */
141 empth_t *empth_self(void);
142
143 /*
144  * Terminate the current thread.
145  * Never returns.
146  */
147 void empth_exit(void);
148
149 /*
150  * Yield the processor.
151  */
152 void empth_yield(void);
153
154 /*
155  * Terminate THREAD.
156  * THREAD will not be scheduled again.  Instead, it will terminate as
157  * if it executed empth_exit().  It is unspecified when exactly that
158  * happens.
159  * THREAD must not be the current thread.
160  */
161 void empth_terminate(empth_t *thread);
162
163 /*
164  * Put current thread to sleep until file descriptor FD is ready for I/O.
165  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
166  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
167  * At most one thread may sleep on the same file descriptor.
168  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
169  * socket.  Implementations should not rely on that.
170  */
171 void empth_select(int fd, int flags);
172
173 /*
174  * Awaken THREAD if it is sleeping in empth_select().
175  * Note: This must not awaken threads sleeping in other functions.
176  */
177 void empth_wakeup(empth_t *thread);
178
179 /*
180  * Put current thread to sleep until the time is UNTIL.
181  * May sleep somehwat longer, but never shorter.
182  */
183 void empth_sleep(time_t until);
184
185 /*
186  * Create a semaphore.
187  * NAME is its name, it is used for debugging.
188  * COUNT is the initial count value of the semaphore, it must not be
189  * negative.
190  * Return the semaphore, or NULL on error.
191  */
192 empth_sem_t *empth_sem_create(char *name, int count);
193
194 /*
195  * Signal SEM.
196  * Increase SEM's count.  If threads are sleeping on it, wake up
197  * exactly one of them.  If that thread has a higher priority, yield
198  * the processor.
199  * This semaphore operation is often called `down' or `V' otherwhere.
200  */
201 void empth_sem_signal(empth_sem_t *sem);
202
203 /*
204  * Wait for SEM.
205  * If SEM has a zero count, put current thread to sleep until
206  * empth_sem_signal() awakens it.  SEM will have non-zero value then.
207  * Decrement SEM's count.
208  * This semaphore operation is often called `up' or `P' otherwhere.
209  */
210 void empth_sem_wait(empth_sem_t *sem);
211
212 #endif