2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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-2006
38 * This header defines Empire's abstract thread interface. There are
39 * several concrete implementations.
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
54 /* thread priorities */
69 /* Abstract data types */
71 /* empth_t * represents a thread. */
72 typedef struct lwpProc empth_t;
74 /* empth_sem_t * represents a semaphore */
75 typedef struct lwpSem empth_sem_t;
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
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
87 #endif /* EMPTH_LWP */
91 #define EMPTH_FD_READ 0x1
92 #define EMPTH_FD_WRITE 0x2
94 #define EMPTH_PRINT 0x1
95 #define EMPTH_STACKCHECK 0x2
97 typedef struct empth_t empth_t;
98 typedef struct empth_sem_t empth_sem_t;
100 #endif /* EMPTH_POSIX */
103 /* The Windows NT Threads */
104 #define EMPTH_FD_READ 0x1
105 #define EMPTH_FD_WRITE 0x2
107 #define EMPTH_PRINT 0x1
108 #define EMPTH_STACKCHECK 0x2
110 typedef struct loc_Thread_t empth_t;
111 typedef struct loc_Sem_t empth_sem_t;
113 void empth_request_shutdown(void);
114 #endif /* EMPTH_W32 */
117 * Initialize thread package.
118 * CTX points to a thread context variable; see empth_create().
119 * FLAGS request optional features.
120 * Should return 0 on success, -1 on error, but currently always
123 int empth_init(void **ctx, int flags);
126 * Create a new thread.
127 * PRIO is the scheduling priority.
128 * ENTRY is the entry point. It will be called with argument UD.
129 * Thread stack is at least SIZE bytes.
130 * FLAGS should be the same as were passed to empth_init(), or zero.
131 * NAME is the threads name, and DESC its description. These are used
132 * for logging and debugging.
133 * UD is the value to pass to ENTRY. It is also assigned to the
134 * context variable defined with empth_init() whenever the thread gets
136 * Yield the processor.
137 * Return the thread, or NULL on error.
139 empth_t *empth_create(int prio, void (*entry)(void *),
140 int size, int flags, char *name, char *desc, void *ud);
143 * Return the current thread.
145 empth_t *empth_self(void);
148 * Terminate the current thread.
149 * The current thread should not be the thread that executed main().
150 * If it is, implementations may terminate the process rather than the
154 void empth_exit(void);
157 * Yield the processor.
159 void empth_yield(void);
163 * THREAD will not be scheduled again. Instead, it will terminate as
164 * if it executed empth_exit(). It is unspecified when exactly that
166 * THREAD must not be the current thread.
168 void empth_terminate(empth_t *thread);
171 * Put current thread to sleep until file descriptor FD is ready for I/O.
172 * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
173 * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
174 * At most one thread may sleep on the same file descriptor.
175 * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
176 * socket. Implementations should not rely on that.
178 void empth_select(int fd, int flags);
181 * Awaken THREAD if it is sleeping in empth_select().
182 * Note: This must not awaken threads sleeping in other functions.
184 void empth_wakeup(empth_t *thread);
187 * Put current thread to sleep until the time is UNTIL.
188 * May sleep somehwat longer, but never shorter.
190 void empth_sleep(time_t until);
193 * Wait for signal, return the signal number
195 int empth_wait_for_signal(void);
198 * Create a semaphore.
199 * NAME is its name, it is used for debugging.
200 * COUNT is the initial count value of the semaphore, it must not be
202 * Return the semaphore, or NULL on error.
204 empth_sem_t *empth_sem_create(char *name, int count);
208 * Increase SEM's count. If threads are sleeping on it, wake up
209 * exactly one of them. If that thread has a higher priority, yield
211 * This semaphore operation is often called `down' or `V' otherwhere.
213 void empth_sem_signal(empth_sem_t *sem);
217 * If SEM has a zero count, put current thread to sleep until
218 * empth_sem_signal() awakens it. SEM will have non-zero value then.
219 * Decrement SEM's count.
220 * This semaphore operation is often called `up' or `P' otherwhere.
222 void empth_sem_wait(empth_sem_t *sem);
226 * Stuff for implementations, not for clients.
229 void empth_init_signals(void);