]> git.pond.sub.org Git - empserver/blob - include/empthread.h
New empth_name() and empth_set_name()
[empserver] / include / empthread.h
1 /*
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
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-2007
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 <time.h>
51
52 #ifdef EMPTH_LWP
53 #include "lwp.h"
54
55 /* Abstract data types */
56
57 /* empth_t * represents a thread.  */
58 typedef struct lwpProc empth_t;
59
60 /* empth_rwlock_t * represents a read-write lock */
61 typedef struct lwp_rwlock empth_rwlock_t;
62
63 /* Flags for empth_select(): whether to sleep on input or output */
64 #define EMPTH_FD_READ     LWP_FD_READ
65 #define EMPTH_FD_WRITE    LWP_FD_WRITE
66
67 /* Flags for empth_init() and empth_create() */
68 /* Request debug prints */
69 #define EMPTH_PRINT       LWP_PRINT
70 /* Request stack checking */
71 #define EMPTH_STACKCHECK  LWP_STACKCHECK
72
73 #endif /* EMPTH_LWP */
74
75 #ifdef EMPTH_POSIX
76 #define EMPTH_FD_READ   0x1
77 #define EMPTH_FD_WRITE  0x2
78
79 #define EMPTH_PRINT       0x1
80 #define EMPTH_STACKCHECK  0x2
81
82 typedef struct empth_t empth_t;
83 typedef struct empth_rwlock_t empth_rwlock_t;
84
85 #endif /* EMPTH_POSIX */
86
87 #ifdef EMPTH_W32
88 /* The Windows NT Threads */
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 loc_Thread empth_t;
96 typedef struct loc_RWLock empth_rwlock_t;
97
98 void empth_request_shutdown(void);
99 #endif /* EMPTH_W32 */
100
101 /*
102  * Initialize thread package.
103  * CTX points to a thread context variable; see empth_create().
104  * FLAGS request optional features.
105  * Should return 0 on success, -1 on error, but currently always
106  * returns 0.
107  */
108 int empth_init(void **ctx, int flags);
109
110 /*
111  * Create a new thread.
112  * ENTRY is the entry point.  It will be called with argument UD.
113  * Thread stack is at least SIZE bytes.
114  * FLAGS should be the same as were passed to empth_init(), or zero.
115  * NAME is the thread's name, it is used for logging and debugging.
116  * UD is the value to pass to ENTRY.  It is also assigned to the
117  * context variable defined with empth_init() whenever the thread gets
118  * scheduled.
119  * Yield the processor.
120  * Return the thread, or NULL on error.
121  */
122 empth_t *empth_create(void (*entry)(void *),
123                       int size, int flags, char *name, void *ud);
124
125 /*
126  * Return the current thread.
127  */
128 empth_t *empth_self(void);
129
130 /*
131  * Return the name of the current thread.
132  */
133 char *empth_name(void);
134
135 /*
136  * Sets the name of the current thread.
137  */
138 void empth_set_name(char *);
139
140 /*
141  * Terminate the current thread.
142  * The current thread should not be the thread that executed main().
143  * If it is, implementations may terminate the process rather than the
144  * thread.
145  * Never returns.
146  */
147 void empth_exit(void);
148
149 /*
150  * Yield the processor.
151  */
152 void empth_yield(void);
153
154 /*
155  * Terminate THREAD.
156  * THREAD will not be scheduled again.  Instead, it will terminate as
157  * if it executed empth_exit().  It is unspecified when exactly that
158  * happens.
159  * THREAD must not be the current thread.
160  * Naive use of this function almost always leads to resource leaks.
161  * Terminating a thread that may hold locks is not a good idea.
162  */
163 void empth_terminate(empth_t *thread);
164
165 /*
166  * Put current thread to sleep until file descriptor FD is ready for I/O.
167  * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
168  * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
169  * At most one thread may sleep on the same file descriptor.
170  * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
171  * socket.  Implementations should not rely on that.
172  */
173 void empth_select(int fd, int flags);
174
175 /*
176  * Awaken THREAD if it is sleeping in empth_select() or empth_sleep().
177  * Note: This must not awaken threads sleeping in other functions.
178  * Does not yield the processor.
179  */
180 void empth_wakeup(empth_t *thread);
181
182 /*
183  * Put current thread to sleep until the time is UNTIL.
184  * Return 0 if it slept until that time.
185  * Return -1 if woken up early, by empth_wakeup().
186  */
187 int empth_sleep(time_t until);
188
189 /*
190  * Wait for signal, return the signal number.
191  */
192 int empth_wait_for_signal(void);
193
194 /*
195  * Create a read-write lock.
196  * NAME is its name, it is used for debugging.
197  * Return the reade-write lock, or NULL on error.
198  */
199 empth_rwlock_t *empth_rwlock_create(char *name);
200
201 /*
202  * Destroy RWLOCK.
203  */
204 void empth_rwlock_destroy(empth_rwlock_t *rwlock);
205
206 /*
207  * Lock RWLOCK for writing.
208  * A read-write lock can be locked for writing only when it is
209  * unlocked.  If this is not the case, put the current thread to sleep
210  * until it is.
211  */
212 void empth_rwlock_wrlock(empth_rwlock_t *rwlock);
213
214 /*
215  * Lock RWLOCK for reading.
216  * A read-write lock can be locked for reading only when it is not
217  * locked for writing.  If this is not the case, put the current
218  * thread to sleep until it is.  Must not starve writers, and may
219  * sleep to avoid that.
220  */
221 void empth_rwlock_rdlock(empth_rwlock_t *rwlock);
222
223 /*
224  * Unlock read-write lock RWLOCK.
225  * The current thread must hold RWLOCK.
226  * Wake up threads that can now lock it.
227  */
228 void empth_rwlock_unlock(empth_rwlock_t *rwlock);
229
230
231 /*
232  * Stuff for implementations, not for clients.
233  */
234
235 void empth_init_signals(void);
236
237 #endif