]> git.pond.sub.org Git - empserver/blob - include/empthread.h
Document Empire's thread abstraction. Minor cleanups:
[empserver] / include / empthread.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
35
36 /*
37  * This header defines Empire's abstract thread interface.  There are
38  * several concrete implementations.
39  *
40  * Empire threads are non-preemptive, i.e. they run until they
41  * voluntarily yield the processor.  The thread scheduler then picks
42  * one of the runnable threads with the highest priority.  Priorities
43  * are static.  Empire code relies on these properties heavily.  The
44  * most common form of yielding the processor is sleeping for some
45  * event to happen.
46  */
47
48 #ifndef _EMTHREAD_H_
49 #define _EMTHREAD_H_
50
51 #include "misc.h"
52
53 #if defined(_WIN32)
54 #undef _EMPTH_LWP
55 #undef _EMPTH_POSIX
56 #define _EMPTH_WIN32
57 #endif
58
59 #ifdef _EMPTH_LWP
60 #include "lwp.h"
61
62 /* Abstract data types */
63
64 /* empth_t * represents a thread.  */
65 typedef struct lwpProc empth_t;
66
67 /* empth_sem_t * represents a semaphore */
68 typedef struct lwpSem empth_sem_t;
69
70 /* Flags for empth_select(): whether to sleep on input or output */
71 #define EMPTH_FD_READ     LWP_FD_READ
72 #define EMPTH_FD_WRITE    LWP_FD_WRITE
73
74 /* Flags for empth_init() and empth_create() */
75 /* Request debug prints */
76 #define EMPTH_PRINT       LWP_PRINT
77 /* Request stack checking */
78 #define EMPTH_STACKCHECK  LWP_STACKCHECK
79
80 #endif /* _EMPTH_LWP */
81
82 #ifdef _EMPTH_POSIX
83 #ifdef __linux__
84 #define _MIT_POSIX_THREADS 1
85 #endif
86 #include <pthread.h>
87 #define EMPTH_FD_READ   0x1
88 #define EMPTH_FD_WRITE  0x2
89
90 #define EMPTH_PRINT       0x1
91 #define EMPTH_STACKCHECK  0x2
92
93 #define EMPTH_KILLED  1
94 typedef struct {
95     char *name;                 /* thread name */
96     char *desc;                 /* description */
97     void *ud;                   /* user data */
98     int state;                  /* my state */
99     void (*ep)(void *);         /* entry point */
100     pthread_t id;               /* thread id */
101 } empth_t;
102
103 typedef struct {
104     pthread_mutex_t mtx_update; /* use it to update count */
105     int count;
106     char name[80];
107     pthread_mutex_t mtx_sem;
108     pthread_cond_t cnd_sem;
109 } empth_sem_t;
110
111 #endif /* _EMPTH_POSIX */
112
113 /* DEC has slightly different names for whatever reason... */
114 #ifdef _DECTHREADS_
115 #define pthread_key_create  pthread_keycreate
116 #define pthread_attr_init   pthread_attr_create
117 #define pthread_attr_destroy pthread_attr_delete
118
119 #endif
120
121
122 #if defined(_EMPTH_WIN32)
123 /* The Windows NT Threads */
124 #define EMPTH_FD_READ   0x1
125 #define EMPTH_FD_WRITE  0x2
126
127 #define EMPTH_PRINT       0x1
128 #define EMPTH_STACKCHECK  0x2
129
130 typedef struct loc_Thread_t empth_t;
131 typedef struct loc_Sem_t empth_sem_t;
132
133 void empth_request_shutdown(void);
134 #endif /* _EMPTH_WIN32 */
135
136 /*
137  * Initialize thread package.
138  * CTX points to a thread context variable; see empth_create().
139  * FLAGS request optional features.
140  * Should return 0 on success, -1 on error, but currently always
141  * returns 0.
142  */
143 int empth_init(void **ctx, int flags);
144
145 /*
146  * Create a new thread.
147  * PRIO is the scheduling priority.
148  * ENTRY is the entry point.  It will be called with argument UD.
149  * Thread stack is at least SIZE bytes.
150  * FLAGS should be the same as were passed to empth_init(), or zero.
151  * NAME is the threads name, and DESC its description.  These are used
152  * for logging and debugging.
153  * UD is the value to pass to ENTRY.  It is also assigned to the
154  * context variable defined with empth_init() whenever the thread gets
155  * scheduled.
156  * Return the thread, or NULL on error.
157  */
158 empth_t *empth_create(int prio, void (*entry)(void *),
159                       int size, int flags, char *name, char *desc, void *ud);
160
161 /*
162  * Return the current thread.
163  */
164 empth_t *empth_self(void);
165
166 /*
167  * Terminate the current thread.
168  * Never returns.
169  */
170 void empth_exit(void);
171
172 /*
173  * Yield the processor.
174  */
175 void empth_yield(void);
176
177 /*
178  * Terminate THREAD.
179  * THREAD will not be scheduled again.  Instead, it will terminate as
180  * if it executed empth_exit().  It is unspecified when exactly that
181  * happens.
182  * THREAD must not be the current thread.
183  */
184 void empth_terminate(empth_t *thread);
185
186 /*
187  * Put current thread to sleep until file descriptor FD is ready for I/O.
188  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
189  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
190  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
191  * socket.  Implementations should not rely on that.
192  */
193 void empth_select(int fd, int flags);
194
195 /*
196  * Awaken THREAD if it is sleeping in empth_select().
197  * Note: This must not awaken threads sleeping in other functions.
198  */
199 void empth_wakeup(empth_t *thread);
200
201 /*
202  * Put current thread to sleep until the time is UNTIL.
203  * May sleep somehwat longer, but never shorter.
204  */
205 void empth_sleep(time_t until);
206
207 /*
208  * Create a semaphore.
209  * NAME is its name, it is used for debugging.
210  * COUNT is the initial count value of the semaphore, it must not be
211  * negative.
212  * Return the semaphore, or NULL on error.
213  */
214 empth_sem_t *empth_sem_create(char *name, int count);
215
216 /*
217  * Signal SEM.
218  * Increase SEM's count.  If threads are sleeping on it, wake up
219  * exactly one of them.  If that thread has a higher priority, yield
220  * the processor.
221  * This semaphore operation is often called `down' or `V' otherwhere.
222  */
223 void empth_sem_signal(empth_sem_t *sem);
224
225 /*
226  * Wait for SEM.
227  * If SEM has a zero count, put current thread to sleep until
228  * empth_sem_signal() awakens it.  SEM will have non-zero value then.
229  * Decrement SEM's count.
230  * This semaphore operation is often called `up' or `P' otherwhere.
231  */
232 void empth_sem_wait(empth_sem_t *sem);
233
234 /* Internal function, not part of the thread abstraction */
235 void empth_alarm(int);
236
237 #endif