]> git.pond.sub.org Git - empserver/blob - include/empthread.h
(struct loc_RWLock_t): Rename struct loc_RWLock_t to struct loc_RWLock
[empserver] / include / empthread.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  *     Markus Armbruster, 2005-2007
35  */
36
37 /*
38  * This header defines Empire's abstract thread interface.  There are
39  * several concrete implementations.
40  *
41  * Empire threads are non-preemptive, i.e. they run until they
42  * voluntarily yield the processor.  The thread scheduler then picks
43  * one of the runnable threads with the highest priority.  Priorities
44  * are static.  Empire code relies on these properties heavily.  The
45  * most common form of yielding the processor is sleeping for some
46  * event to happen.
47  */
48
49 #ifndef EMPTHREAD_H
50 #define EMPTHREAD_H
51
52 #include <time.h>
53
54 /* thread priorities */
55 enum {
56     PP_MAIN      = 7,
57     PP_UPDATE    = 6,
58     PP_SHUTDOWN  = 5,
59     PP_SCHED     = 4,
60     PP_TIMESTAMP = 2,
61     PP_PLAYER    = 3,
62     PP_ACCEPT    = 3,
63     PP_KILLIDLE  = 2
64 };
65
66 #ifdef EMPTH_LWP
67 #include "lwp.h"
68
69 /* Abstract data types */
70
71 /* empth_t * represents a thread.  */
72 typedef struct lwpProc empth_t;
73
74 /* empth_sem_t * represents a semaphore */
75 typedef struct lwpSem empth_sem_t;
76
77 /* empth_rwlock_t * represents a read-write lock */
78 typedef struct lwp_rwlock empth_rwlock_t;
79
80 /* Flags for empth_select(): whether to sleep on input or output */
81 #define EMPTH_FD_READ     LWP_FD_READ
82 #define EMPTH_FD_WRITE    LWP_FD_WRITE
83
84 /* Flags for empth_init() and empth_create() */
85 /* Request debug prints */
86 #define EMPTH_PRINT       LWP_PRINT
87 /* Request stack checking */
88 #define EMPTH_STACKCHECK  LWP_STACKCHECK
89
90 #endif /* EMPTH_LWP */
91
92 #ifdef EMPTH_POSIX
93 #define EMPTH_FD_READ   0x1
94 #define EMPTH_FD_WRITE  0x2
95
96 #define EMPTH_PRINT       0x1
97 #define EMPTH_STACKCHECK  0x2
98
99 typedef struct empth_t empth_t;
100 typedef struct empth_sem_t empth_sem_t;
101 typedef struct empth_rwlock_t empth_rwlock_t;
102
103 #endif /* EMPTH_POSIX */
104
105 #ifdef EMPTH_W32
106 /* The Windows NT Threads */
107 #define EMPTH_FD_READ   0x1
108 #define EMPTH_FD_WRITE  0x2
109
110 #define EMPTH_PRINT       0x1
111 #define EMPTH_STACKCHECK  0x2
112
113 typedef struct loc_Thread empth_t;
114 typedef struct loc_Sem empth_sem_t;
115 typedef struct loc_RWLock empth_rwlock_t;
116
117 void empth_request_shutdown(void);
118 #endif /* EMPTH_W32 */
119
120 /*
121  * Initialize thread package.
122  * CTX points to a thread context variable; see empth_create().
123  * FLAGS request optional features.
124  * Should return 0 on success, -1 on error, but currently always
125  * returns 0.
126  */
127 int empth_init(void **ctx, int flags);
128
129 /*
130  * Create a new thread.
131  * PRIO is the scheduling priority.
132  * ENTRY is the entry point.  It will be called with argument UD.
133  * Thread stack is at least SIZE bytes.
134  * FLAGS should be the same as were passed to empth_init(), or zero.
135  * NAME is the threads name, and DESC its description.  These are used
136  * for logging and debugging.
137  * UD is the value to pass to ENTRY.  It is also assigned to the
138  * context variable defined with empth_init() whenever the thread gets
139  * scheduled.
140  * Yield the processor.
141  * Return the thread, or NULL on error.
142  */
143 empth_t *empth_create(int prio, void (*entry)(void *),
144                       int size, int flags, char *name, char *desc, void *ud);
145
146 /*
147  * Return the current thread.
148  */
149 empth_t *empth_self(void);
150
151 /*
152  * Terminate the current thread.
153  * The current thread should not be the thread that executed main().
154  * If it is, implementations may terminate the process rather than the
155  * thread.
156  * Never returns.
157  */
158 void empth_exit(void);
159
160 /*
161  * Yield the processor.
162  */
163 void empth_yield(void);
164
165 /*
166  * Terminate THREAD.
167  * THREAD will not be scheduled again.  Instead, it will terminate as
168  * if it executed empth_exit().  It is unspecified when exactly that
169  * happens.
170  * THREAD must not be the current thread.
171  */
172 void empth_terminate(empth_t *thread);
173
174 /*
175  * Put current thread to sleep until file descriptor FD is ready for I/O.
176  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
177  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
178  * At most one thread may sleep on the same file descriptor.
179  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
180  * socket.  Implementations should not rely on that.
181  */
182 void empth_select(int fd, int flags);
183
184 /*
185  * Awaken THREAD if it is sleeping in empth_select().
186  * Note: This must not awaken threads sleeping in other functions.
187  */
188 void empth_wakeup(empth_t *thread);
189
190 /*
191  * Put current thread to sleep until the time is UNTIL.
192  * May sleep somehwat longer, but never shorter.
193  */
194 void empth_sleep(time_t until);
195
196 /*
197  * Wait for signal, return the signal number
198  */
199 int empth_wait_for_signal(void);
200
201 /*
202  * Create a semaphore.
203  * NAME is its name, it is used for debugging.
204  * COUNT is the initial count value of the semaphore, it must not be
205  * negative.
206  * Return the semaphore, or NULL on error.
207  */
208 empth_sem_t *empth_sem_create(char *name, int count);
209
210 /*
211  * Signal SEM.
212  * Increase SEM's count.  If threads are sleeping on it, wake up
213  * exactly one of them.  If that thread has a higher priority, yield
214  * the processor.
215  * This semaphore operation is often called `down' or `V' otherwhere.
216  */
217 void empth_sem_signal(empth_sem_t *sem);
218
219 /*
220  * Wait for SEM.
221  * If SEM has a zero count, put current thread to sleep until
222  * empth_sem_signal() awakens it.  SEM will have non-zero value then.
223  * Decrement SEM's count.
224  * This semaphore operation is often called `up' or `P' otherwhere.
225  */
226 void empth_sem_wait(empth_sem_t *sem);
227
228 /*
229  * Create a read-write lock.
230  * NAME is its name, it is used for debugging.
231  * Return the reade-write lock, or NULL on error.
232  */
233 empth_rwlock_t *empth_rwlock_create(char *name);
234
235 /*
236  * Destroy RWLOCK.
237  */
238 void empth_rwlock_destroy(empth_rwlock_t *rwlock);
239
240 /*
241  * Lock RWLOCK for writing.
242  * A read-write lock can be locked for writing only when it is
243  * unlocked.  If this is not the case, put the current thread to sleep
244  * until it is.
245  */
246 void empth_rwlock_wrlock(empth_rwlock_t *rwlock);
247
248 /*
249  * Lock RWLOCK for reading.
250  * A read-write lock can be locked for reading only when it is not
251  * locked for writing.  If this is not the case, put the current
252  * thread to sleep until it is.  Must not starve writers, and may
253  * sleep to avoid that.
254  */
255 void empth_rwlock_rdlock(empth_rwlock_t *rwlock);
256
257 /*
258  * Unlock read-write lock RWLOCK.
259  * The current thread must hold RWLOCK.
260  * Wake up threads that can now lock it.
261  */
262 void empth_rwlock_unlock(empth_rwlock_t *rwlock);
263
264
265 /*
266  * Stuff for implementations, not for clients.
267  */
268
269 void empth_init_signals(void);
270
271 #endif