2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * empthread.h: Definitions for Empire threading
30 * Known contributors to this file:
34 * Markus Armbruster, 2005-2008
35 * Ron Koenderink, 2005-2008
39 * This header defines Empire's abstract thread interface. There are
40 * several concrete implementations.
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.
56 /* Abstract data types */
58 /* empth_t * represents a thread. */
59 typedef struct lwpProc empth_t;
61 /* empth_rwlock_t * represents a read-write lock */
62 typedef struct lwp_rwlock empth_rwlock_t;
64 /* Flags for empth_select(): whether to sleep on input or output */
65 #define EMPTH_FD_READ LWP_FD_READ
66 #define EMPTH_FD_WRITE LWP_FD_WRITE
68 /* Flags for empth_init() and empth_create() */
69 /* Request debug prints */
70 #define EMPTH_PRINT LWP_PRINT
71 /* Request stack checking */
72 #define EMPTH_STACKCHECK LWP_STACKCHECK
74 #endif /* EMPTH_LWP */
77 #define EMPTH_FD_READ 0x1
78 #define EMPTH_FD_WRITE 0x2
80 #define EMPTH_PRINT 0x1
81 #define EMPTH_STACKCHECK 0x2
83 typedef struct empth_t empth_t;
84 typedef struct empth_rwlock_t empth_rwlock_t;
86 #endif /* EMPTH_POSIX */
89 /* The Windows NT Threads */
90 #define EMPTH_FD_READ 0x1
91 #define EMPTH_FD_WRITE 0x2
93 #define EMPTH_PRINT 0x1
94 #define EMPTH_STACKCHECK 0x2
96 typedef struct loc_Thread empth_t;
97 typedef struct loc_RWLock empth_rwlock_t;
99 void empth_request_shutdown(void);
100 #endif /* EMPTH_W32 */
103 * Initialize thread package.
104 * CTX points to a thread context variable; see empth_create().
105 * FLAGS request optional features.
106 * Should return 0 on success, -1 on error, but currently always
109 int empth_init(void **ctx, int flags);
112 * Create a new thread.
113 * ENTRY is the entry point. It will be called with argument UD.
114 * Thread stack is at least SIZE bytes.
115 * FLAGS should be the same as were passed to empth_init(), or zero.
116 * NAME is the thread's name, it is used for logging and debugging.
117 * UD is the value to pass to ENTRY. It is also assigned to the
118 * context variable defined with empth_init() whenever the thread gets
120 * Yield the processor.
121 * Return the thread, or NULL on error.
123 empth_t *empth_create(void (*entry)(void *),
124 int size, int flags, char *name, void *ud);
127 * Return the current thread.
129 empth_t *empth_self(void);
132 * Return the name THREAD.
134 char *empth_name(empth_t *thread);
137 * Set the name of THREAD to NAME.
139 void empth_set_name(empth_t *thread, char *name);
142 * Terminate the current thread.
143 * The current thread should not be the thread that executed main().
144 * If it is, implementations may terminate the process rather than the
148 void empth_exit(void);
151 * Yield the processor.
153 void empth_yield(void);
157 * THREAD will not be scheduled again. Instead, it will terminate as
158 * if it executed empth_exit(). It is unspecified when exactly that
160 * THREAD must not be the current thread.
161 * Naive use of this function almost always leads to resource leaks.
162 * Terminating a thread that may hold locks is not a good idea.
164 void empth_terminate(empth_t *thread);
167 * Put current thread to sleep until file descriptor FD is ready for I/O.
168 * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
169 * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
170 * At most one thread may sleep on the same file descriptor.
171 * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
172 * socket. Implementations should not rely on that.
174 void empth_select(int fd, int flags);
177 * Awaken THREAD if it is sleeping in empth_select() or empth_sleep().
178 * Note: This must not awaken threads sleeping in other functions.
179 * Does not yield the processor.
181 void empth_wakeup(empth_t *thread);
184 * Put current thread to sleep until the time is UNTIL.
185 * Return 0 if it slept until that time.
186 * Return -1 if woken up early, by empth_wakeup().
188 int empth_sleep(time_t until);
191 * Wait for signal, return the signal number.
193 int empth_wait_for_signal(void);
196 * Create a read-write lock.
197 * NAME is its name, it is used for debugging.
198 * Return the reade-write lock, or NULL on error.
200 empth_rwlock_t *empth_rwlock_create(char *name);
205 void empth_rwlock_destroy(empth_rwlock_t *rwlock);
208 * Lock RWLOCK for writing.
209 * A read-write lock can be locked for writing only when it is
210 * unlocked. If this is not the case, put the current thread to sleep
213 void empth_rwlock_wrlock(empth_rwlock_t *rwlock);
216 * Lock RWLOCK for reading.
217 * A read-write lock can be locked for reading only when it is not
218 * locked for writing. If this is not the case, put the current
219 * thread to sleep until it is. Must not starve writers, and may
220 * sleep to avoid that.
222 void empth_rwlock_rdlock(empth_rwlock_t *rwlock);
225 * Unlock read-write lock RWLOCK.
226 * The current thread must hold RWLOCK.
227 * Wake up threads that can now lock it.
229 void empth_rwlock_unlock(empth_rwlock_t *rwlock);
233 * Stuff for implementations, not for clients.
236 void empth_init_signals(void);