]> git.pond.sub.org Git - empserver/blob - include/empthread.h
Update known contributors comments
[empserver] / include / empthread.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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-2008
35  *     Ron Koenderink, 2005-2009
36  */
37
38 /*
39  * This header defines Empire's abstract thread interface.  There are
40  * several concrete implementations.
41  *
42  * Empire threads are non-preemptive, i.e. they run until they
43  * voluntarily yield the processor.  The thread scheduler then picks
44  * one of the runnable threads.  The most common form of yielding the
45  * processor is sleeping for some event to happen.
46  */
47
48 #ifndef EMPTHREAD_H
49 #define EMPTHREAD_H
50
51 #include <sys/time.h>
52 #include <time.h>
53
54 #ifdef EMPTH_LWP
55 #include "lwp.h"
56
57 /* Abstract data types */
58
59 /* empth_t * represents a thread.  */
60 typedef struct lwpProc empth_t;
61
62 /* empth_rwlock_t * represents a read-write lock */
63 typedef struct lwp_rwlock empth_rwlock_t;
64
65 /* Flags for empth_select(): whether to sleep on input or output */
66 #define EMPTH_FD_READ     LWP_FD_READ
67 #define EMPTH_FD_WRITE    LWP_FD_WRITE
68
69 /* Flags for empth_init() and empth_create() */
70 /* Request debug prints */
71 #define EMPTH_PRINT       LWP_PRINT
72 /* Request stack checking */
73 #define EMPTH_STACKCHECK  LWP_STACKCHECK
74
75 #endif /* EMPTH_LWP */
76
77 #ifdef EMPTH_POSIX
78 #define EMPTH_FD_READ   0x1
79 #define EMPTH_FD_WRITE  0x2
80
81 #define EMPTH_PRINT       0x1
82 #define EMPTH_STACKCHECK  0x2
83
84 typedef struct empth_t empth_t;
85 typedef struct empth_rwlock_t empth_rwlock_t;
86
87 #endif /* EMPTH_POSIX */
88
89 #ifdef EMPTH_W32
90 /* The Windows NT Threads */
91 #define EMPTH_FD_READ   0x1
92 #define EMPTH_FD_WRITE  0x2
93
94 #define EMPTH_PRINT       0x1
95 #define EMPTH_STACKCHECK  0x2
96
97 typedef struct loc_Thread empth_t;
98 typedef struct loc_RWLock empth_rwlock_t;
99
100 void empth_request_shutdown(void);
101 #endif /* EMPTH_W32 */
102
103 /*
104  * Initialize thread package.
105  * CTX points to a thread context variable; see empth_create().
106  * FLAGS request optional features.
107  * Should return 0 on success, -1 on error, but currently always
108  * returns 0.
109  */
110 int empth_init(void **ctx, int flags);
111
112 /*
113  * Create a new thread.
114  * ENTRY is the entry point.  It will be called with argument UD.
115  * Thread stack is at least SIZE bytes.
116  * FLAGS should be the same as were passed to empth_init(), or zero.
117  * NAME is the thread's name, it is used for logging and debugging.
118  * UD is the value to pass to ENTRY.  It is also assigned to the
119  * context variable defined with empth_init() whenever the thread gets
120  * scheduled.
121  * Yield the processor.
122  * Return the thread, or NULL on error.
123  */
124 empth_t *empth_create(void (*entry)(void *),
125                       int size, int flags, char *name, void *ud);
126
127 /*
128  * Return the current thread.
129  */
130 empth_t *empth_self(void);
131
132 /*
133  * Return the name THREAD.
134  */
135 char *empth_name(empth_t *thread);
136
137 /*
138  * Set the name of THREAD to NAME.
139  */
140 void empth_set_name(empth_t *thread, char *name);
141
142 /*
143  * Terminate the current thread.
144  * The current thread should not be the thread that executed main().
145  * If it is, implementations may terminate the process rather than the
146  * thread.
147  * Never returns.
148  */
149 void empth_exit(void);
150
151 /*
152  * Yield the processor.
153  */
154 void empth_yield(void);
155
156 /*
157  * Terminate THREAD.
158  * THREAD will not be scheduled again.  Instead, it will terminate as
159  * if it executed empth_exit().  It is unspecified when exactly that
160  * happens.
161  * THREAD must not be the current thread.
162  * Naive use of this function almost always leads to resource leaks.
163  * Terminating a thread that may hold locks is not a good idea.
164  */
165 void empth_terminate(empth_t *thread);
166
167 /*
168  * Put current thread to sleep until file descriptor FD is ready for I/O.
169  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
170  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
171  * At most one thread may sleep on the same file descriptor.
172  * TIMEOUT, if non-null, limits the sleep time.
173  * Return one when the FD is ready, zero on timeout, -1 on error with
174  * errno set.
175  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
176  * socket.  Implementations should not rely on that.
177  */
178 int empth_select(int fd, int flags, struct timeval *timeout);
179
180 /*
181  * Awaken THREAD if it is sleeping in empth_select() or empth_sleep().
182  * Note: This must not awaken threads sleeping in other functions.
183  * Does not yield the processor.
184  */
185 void empth_wakeup(empth_t *thread);
186
187 /*
188  * Put current thread to sleep until the time is UNTIL.
189  * Return 0 if it slept until that time.
190  * Return -1 if woken up early, by empth_wakeup().
191  */
192 int empth_sleep(time_t until);
193
194 /*
195  * Wait for signal, return the signal number.
196  */
197 int empth_wait_for_signal(void);
198
199 /*
200  * Create a read-write lock.
201  * NAME is its name, it is used for debugging.
202  * Return the reade-write lock, or NULL on error.
203  */
204 empth_rwlock_t *empth_rwlock_create(char *name);
205
206 /*
207  * Destroy RWLOCK.
208  */
209 void empth_rwlock_destroy(empth_rwlock_t *rwlock);
210
211 /*
212  * Lock RWLOCK for writing.
213  * A read-write lock can be locked for writing only when it is
214  * unlocked.  If this is not the case, put the current thread to sleep
215  * until it is.
216  */
217 void empth_rwlock_wrlock(empth_rwlock_t *rwlock);
218
219 /*
220  * Lock RWLOCK for reading.
221  * A read-write lock can be locked for reading only when it is not
222  * locked for writing.  If this is not the case, put the current
223  * thread to sleep until it is.  Must not starve writers, and may
224  * sleep to avoid that.
225  */
226 void empth_rwlock_rdlock(empth_rwlock_t *rwlock);
227
228 /*
229  * Unlock read-write lock RWLOCK.
230  * The current thread must hold RWLOCK.
231  * Wake up threads that can now lock it.
232  */
233 void empth_rwlock_unlock(empth_rwlock_t *rwlock);
234
235
236 /*
237  * Stuff for implementations, not for clients.
238  */
239
240 void empth_init_signals(void);
241
242 #endif