]> git.pond.sub.org Git - empserver/blob - include/empthread.h
Semaphores are no longer used, remove:
[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_rwlock_t * represents a read-write lock */
75 typedef struct lwp_rwlock empth_rwlock_t;
76
77 /* Flags for empth_select(): whether to sleep on input or output */
78 #define EMPTH_FD_READ     LWP_FD_READ
79 #define EMPTH_FD_WRITE    LWP_FD_WRITE
80
81 /* Flags for empth_init() and empth_create() */
82 /* Request debug prints */
83 #define EMPTH_PRINT       LWP_PRINT
84 /* Request stack checking */
85 #define EMPTH_STACKCHECK  LWP_STACKCHECK
86
87 #endif /* EMPTH_LWP */
88
89 #ifdef EMPTH_POSIX
90 #define EMPTH_FD_READ   0x1
91 #define EMPTH_FD_WRITE  0x2
92
93 #define EMPTH_PRINT       0x1
94 #define EMPTH_STACKCHECK  0x2
95
96 typedef struct empth_t empth_t;
97 typedef struct empth_rwlock_t empth_rwlock_t;
98
99 #endif /* EMPTH_POSIX */
100
101 #ifdef EMPTH_W32
102 /* The Windows NT Threads */
103 #define EMPTH_FD_READ   0x1
104 #define EMPTH_FD_WRITE  0x2
105
106 #define EMPTH_PRINT       0x1
107 #define EMPTH_STACKCHECK  0x2
108
109 typedef struct loc_Thread empth_t;
110 typedef struct loc_RWLock empth_rwlock_t;
111
112 void empth_request_shutdown(void);
113 #endif /* EMPTH_W32 */
114
115 /*
116  * Initialize thread package.
117  * CTX points to a thread context variable; see empth_create().
118  * FLAGS request optional features.
119  * Should return 0 on success, -1 on error, but currently always
120  * returns 0.
121  */
122 int empth_init(void **ctx, int flags);
123
124 /*
125  * Create a new thread.
126  * PRIO is the scheduling priority.
127  * ENTRY is the entry point.  It will be called with argument UD.
128  * Thread stack is at least SIZE bytes.
129  * FLAGS should be the same as were passed to empth_init(), or zero.
130  * NAME is the thread's name, it is used for logging and debugging.
131  * UD is the value to pass to ENTRY.  It is also assigned to the
132  * context variable defined with empth_init() whenever the thread gets
133  * scheduled.
134  * Yield the processor.
135  * Return the thread, or NULL on error.
136  */
137 empth_t *empth_create(int prio, void (*entry)(void *),
138                       int size, int flags, char *name, void *ud);
139
140 /*
141  * Return the current thread.
142  */
143 empth_t *empth_self(void);
144
145 /*
146  * Terminate the current thread.
147  * The current thread should not be the thread that executed main().
148  * If it is, implementations may terminate the process rather than the
149  * thread.
150  * Never returns.
151  */
152 void empth_exit(void);
153
154 /*
155  * Yield the processor.
156  */
157 void empth_yield(void);
158
159 /*
160  * Terminate THREAD.
161  * THREAD will not be scheduled again.  Instead, it will terminate as
162  * if it executed empth_exit().  It is unspecified when exactly that
163  * happens.
164  * THREAD must not be the current thread.
165  */
166 void empth_terminate(empth_t *thread);
167
168 /*
169  * Put current thread to sleep until file descriptor FD is ready for I/O.
170  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
171  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
172  * At most one thread may sleep on the same file descriptor.
173  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
174  * socket.  Implementations should not rely on that.
175  */
176 void empth_select(int fd, int flags);
177
178 /*
179  * Awaken THREAD if it is sleeping in empth_select() or empth_sleep().
180  * Note: This must not awaken threads sleeping in other functions.
181  * Does not yield the processor.
182  */
183 void empth_wakeup(empth_t *thread);
184
185 /*
186  * Put current thread to sleep until the time is UNTIL.
187  * Return 0 if it slept until that time.
188  * Return -1 if woken up early, by empth_wakeup().
189  */
190 int empth_sleep(time_t until);
191
192 /*
193  * Wait for signal, return the signal number.
194  */
195 int empth_wait_for_signal(void);
196
197 /*
198  * Create a read-write lock.
199  * NAME is its name, it is used for debugging.
200  * Return the reade-write lock, or NULL on error.
201  */
202 empth_rwlock_t *empth_rwlock_create(char *name);
203
204 /*
205  * Destroy RWLOCK.
206  */
207 void empth_rwlock_destroy(empth_rwlock_t *rwlock);
208
209 /*
210  * Lock RWLOCK for writing.
211  * A read-write lock can be locked for writing only when it is
212  * unlocked.  If this is not the case, put the current thread to sleep
213  * until it is.
214  */
215 void empth_rwlock_wrlock(empth_rwlock_t *rwlock);
216
217 /*
218  * Lock RWLOCK for reading.
219  * A read-write lock can be locked for reading only when it is not
220  * locked for writing.  If this is not the case, put the current
221  * thread to sleep until it is.  Must not starve writers, and may
222  * sleep to avoid that.
223  */
224 void empth_rwlock_rdlock(empth_rwlock_t *rwlock);
225
226 /*
227  * Unlock read-write lock RWLOCK.
228  * The current thread must hold RWLOCK.
229  * Wake up threads that can now lock it.
230  */
231 void empth_rwlock_unlock(empth_rwlock_t *rwlock);
232
233
234 /*
235  * Stuff for implementations, not for clients.
236  */
237
238 void empth_init_signals(void);
239
240 #endif