]> git.pond.sub.org Git - empserver/blob - include/empthread.h
e7448a847ab00988dade7836134295a5258ca3f9
[empserver] / include / empthread.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  empthread.h: Definitions for Empire threading
28  *
29  *  Known contributors to this file:
30  *     Sasha Mikheev
31  *     Doug Hay, 1998
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2005-2012
34  *     Ron Koenderink, 2005-2009
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.  The most common form of yielding the
44  * processor is sleeping for some event to happen.
45  */
46
47 #ifndef EMPTHREAD_H
48 #define EMPTHREAD_H
49
50 #include <sys/time.h>
51 #include <time.h>
52
53 #ifdef EMPTH_LWP
54 #include "lwp.h"
55
56 /* Abstract data types */
57
58 /* A thread. */
59 typedef struct lwpProc empth_t;
60
61 /* A read-write lock, perferring writers */
62 typedef struct lwp_rwlock empth_rwlock_t;
63
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
67
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
73
74 #endif /* EMPTH_LWP */
75
76 #ifdef EMPTH_POSIX
77 #define EMPTH_FD_READ   0x1
78 #define EMPTH_FD_WRITE  0x2
79
80 #define EMPTH_PRINT       0x1
81 #define EMPTH_STACKCHECK  0x2
82
83 typedef struct empth_t empth_t;
84 typedef struct empth_rwlock_t empth_rwlock_t;
85
86 #endif /* EMPTH_POSIX */
87
88 #ifdef EMPTH_W32
89 /* The Windows NT Threads */
90 #define EMPTH_FD_READ   0x1
91 #define EMPTH_FD_WRITE  0x2
92
93 #define EMPTH_PRINT       0x1
94 #define EMPTH_STACKCHECK  0x2
95
96 typedef struct loc_Thread empth_t;
97 typedef struct loc_RWLock empth_rwlock_t;
98
99 void empth_request_shutdown(void);
100 #endif /* EMPTH_W32 */
101
102 /*
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
107  * returns 0.
108  */
109 int empth_init(void **ctx, int flags);
110
111 /*
112  * Create a new thread.
113  * @entry is the entry point.  It will be called with argument @ud.
114  * If it returns, the thread terminates as if it called empth_exit().
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, NULL before empth_init().
129  * This is the only function that may be called before empth_init().
130  */
131 empth_t *empth_self(void);
132
133 /*
134  * Return @thread's name.
135  */
136 char *empth_name(empth_t *thread);
137
138 /*
139  * Set @thread's name to @name.
140  */
141 void empth_set_name(empth_t *thread, char *name);
142
143 /*
144  * Terminate the current thread.
145  * The current thread should not be the thread that executed main().
146  * If it is, implementations may terminate the process rather than the
147  * thread.
148  * Never returns.
149  */
150 void empth_exit(void);
151
152 /*
153  * Yield the processor.
154  */
155 void empth_yield(void);
156
157 /*
158  * Put current thread to sleep until file descriptor @fd is ready for I/O.
159  * If @flags & EMPTH_FD_READ, wake up if @fd is ready for input.
160  * If @flags & EMPTH_FD_WRITE, wake up if @fd is ready for output.
161  * At most one thread may sleep on the same file descriptor.
162  * @timeout, if non-null, limits the sleep time.
163  * Return one when the @fd is ready, zero on timeout or early wakeup by
164  * empth_wakeup(), -1 on error with errno set.
165  * Note: Currently, Empire sleeps only on network I/O, i.e. @fd is a
166  * socket.  Implementations should not rely on that.
167  */
168 int empth_select(int fd, int flags, struct timeval *timeout);
169
170 /*
171  * Awaken @thread if it is sleeping in empth_select() or empth_sleep().
172  * This does not awaken threads sleeping in other functions.
173  * Does not yield the processor.
174  */
175 void empth_wakeup(empth_t *thread);
176
177 /*
178  * Put current thread to sleep until the time is @until.
179  * Return 0 if it slept until that time.
180  * Return -1 if woken up early, by empth_wakeup().
181  */
182 int empth_sleep(time_t until);
183
184 /*
185  * Put current thread to sleep until SIGHUP, SIGINT or SIGTERM is received.
186  * Return the signal number.
187  */
188 int empth_wait_for_signal(void);
189
190 /*
191  * Create a read-write lock.
192  * @name is its name, it is used for debugging.
193  * Return the read-write lock, or NULL on error.
194  */
195 empth_rwlock_t *empth_rwlock_create(char *name);
196
197 /*
198  * Destroy @rwlock.
199  */
200 void empth_rwlock_destroy(empth_rwlock_t *rwlock);
201
202 /*
203  * Lock @rwlock for writing.
204  * A read-write lock can be locked for writing only when it is
205  * unlocked.  If this is not the case, put the current thread to sleep
206  * until it is.
207  */
208 void empth_rwlock_wrlock(empth_rwlock_t *rwlock);
209
210 /*
211  * Lock @rwlock for reading.
212  * A read-write lock can be locked for reading only when it is not
213  * locked for writing, and no other thread is attempting to lock it
214  * for writing.  If this is not the case, put the current thread to
215  * sleep until it is.
216  */
217 void empth_rwlock_rdlock(empth_rwlock_t *rwlock);
218
219 /*
220  * Unlock read-write lock @rwlock.
221  * The current thread must hold @rwlock.
222  * Wake up threads that can now lock it.
223  */
224 void empth_rwlock_unlock(empth_rwlock_t *rwlock);
225
226
227 /*
228  * Stuff for implementations, not for clients.
229  */
230
231 void empth_init_signals(void);
232
233 #endif