]> git.pond.sub.org Git - empserver/blob - include/empthread.h
Update known contributors comment.
[empserver] / include / empthread.h
1 /*
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
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
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 /* thread priorities */
53 enum {
54     PP_MAIN      = 7,
55     PP_UPDATE    = 6,
56     PP_SHUTDOWN  = 5,
57     PP_SCHED     = 4,
58     PP_TIMESTAMP = 2,
59     PP_PLAYER    = 3,
60     PP_ACCEPT    = 3,
61     PP_KILLIDLE  = 2
62 };
63
64 #ifdef EMPTH_LWP
65 #include "lwp.h"
66
67 /* Abstract data types */
68
69 /* empth_t * represents a thread.  */
70 typedef struct lwpProc empth_t;
71
72 /* empth_sem_t * represents a semaphore */
73 typedef struct lwpSem empth_sem_t;
74
75 /* Flags for empth_select(): whether to sleep on input or output */
76 #define EMPTH_FD_READ     LWP_FD_READ
77 #define EMPTH_FD_WRITE    LWP_FD_WRITE
78
79 /* Flags for empth_init() and empth_create() */
80 /* Request debug prints */
81 #define EMPTH_PRINT       LWP_PRINT
82 /* Request stack checking */
83 #define EMPTH_STACKCHECK  LWP_STACKCHECK
84
85 #endif /* EMPTH_LWP */
86
87 #ifdef EMPTH_POSIX
88 #include <pthread.h>
89 #define EMPTH_FD_READ   0x1
90 #define EMPTH_FD_WRITE  0x2
91
92 #define EMPTH_PRINT       0x1
93 #define EMPTH_STACKCHECK  0x2
94
95 typedef struct empth_t empth_t;
96 typedef struct empth_sem_t empth_sem_t;
97
98 #endif /* EMPTH_POSIX */
99
100 #ifdef EMPTH_W32
101 /* The Windows NT Threads */
102 #define EMPTH_FD_READ   0x1
103 #define EMPTH_FD_WRITE  0x2
104
105 #define EMPTH_PRINT       0x1
106 #define EMPTH_STACKCHECK  0x2
107
108 typedef struct loc_Thread_t empth_t;
109 typedef struct loc_Sem_t empth_sem_t;
110
111 void empth_request_shutdown(void);
112 #endif /* EMPTH_W32 */
113
114 /*
115  * Initialize thread package.
116  * CTX points to a thread context variable; see empth_create().
117  * FLAGS request optional features.
118  * Should return 0 on success, -1 on error, but currently always
119  * returns 0.
120  */
121 int empth_init(void **ctx, int flags);
122
123 /*
124  * Create a new thread.
125  * PRIO is the scheduling priority.
126  * ENTRY is the entry point.  It will be called with argument UD.
127  * Thread stack is at least SIZE bytes.
128  * FLAGS should be the same as were passed to empth_init(), or zero.
129  * NAME is the threads name, and DESC its description.  These are used
130  * 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  * Return the thread, or NULL on error.
135  */
136 empth_t *empth_create(int prio, void (*entry)(void *),
137                       int size, int flags, char *name, char *desc, void *ud);
138
139 /*
140  * Return the current thread.
141  */
142 empth_t *empth_self(void);
143
144 /*
145  * Terminate the current thread.
146  * Never returns.
147  */
148 void empth_exit(void);
149
150 /*
151  * Yield the processor.
152  */
153 void empth_yield(void);
154
155 /*
156  * Terminate THREAD.
157  * THREAD will not be scheduled again.  Instead, it will terminate as
158  * if it executed empth_exit().  It is unspecified when exactly that
159  * happens.
160  * THREAD must not be the current thread.
161  */
162 void empth_terminate(empth_t *thread);
163
164 /*
165  * Put current thread to sleep until file descriptor FD is ready for I/O.
166  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
167  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
168  * At most one thread may sleep on the same file descriptor.
169  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
170  * socket.  Implementations should not rely on that.
171  */
172 void empth_select(int fd, int flags);
173
174 /*
175  * Awaken THREAD if it is sleeping in empth_select().
176  * Note: This must not awaken threads sleeping in other functions.
177  */
178 void empth_wakeup(empth_t *thread);
179
180 /*
181  * Put current thread to sleep until the time is UNTIL.
182  * May sleep somehwat longer, but never shorter.
183  */
184 void empth_sleep(time_t until);
185
186 /*
187  * Create a semaphore.
188  * NAME is its name, it is used for debugging.
189  * COUNT is the initial count value of the semaphore, it must not be
190  * negative.
191  * Return the semaphore, or NULL on error.
192  */
193 empth_sem_t *empth_sem_create(char *name, int count);
194
195 /*
196  * Signal SEM.
197  * Increase SEM's count.  If threads are sleeping on it, wake up
198  * exactly one of them.  If that thread has a higher priority, yield
199  * the processor.
200  * This semaphore operation is often called `down' or `V' otherwhere.
201  */
202 void empth_sem_signal(empth_sem_t *sem);
203
204 /*
205  * Wait for SEM.
206  * If SEM has a zero count, put current thread to sleep until
207  * empth_sem_signal() awakens it.  SEM will have non-zero value then.
208  * Decrement SEM's count.
209  * This semaphore operation is often called `up' or `P' otherwhere.
210  */
211 void empth_sem_wait(empth_sem_t *sem);
212
213 #endif