]> git.pond.sub.org Git - empserver/blob - src/lib/empthread/pthread.c
Remove unused empth_terminate()
[empserver] / src / lib / empthread / pthread.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *  pthread.c: Interface from Empire threads to POSIX threads
29  *
30  *  Known contributors to this file:
31  *     Sasha Mikheev
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2005-2009
34  *     Ron Koenderink, 2007-2009
35  */
36
37 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
38 #define _XOPEN_SOURCE 500
39
40 #include <config.h>
41
42 #include <errno.h>
43 #include <limits.h>
44 #include <pthread.h>
45 #include <signal.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <unistd.h>
53 #include "misc.h"
54 #include "empthread.h"
55 #include "prototypes.h"
56
57 struct empth_t {
58     char *name;                 /* thread name */
59     void *ud;                   /* user data */
60     int wakeup;
61     void (*ep)(void *);         /* entry point */
62     pthread_t id;               /* thread id */
63 };
64
65 struct empth_rwlock_t {
66     char *name;
67     pthread_rwlock_t lock;
68 };
69
70 /* Thread-specific data key */
71 static pthread_key_t ctx_key;
72
73 /* Flags that were passed to empth_init() */
74 static int empth_flags;
75
76 /* Pointer to thread context variable */
77 static void **udata;
78
79 /*
80  * Non-preemption mutex.
81  * Empire code outside this file is only executed while holding this
82  * mutex.  This makes sure Empire code is never preempted by Empire
83  * code.
84  */
85 static pthread_mutex_t mtx_ctxsw;
86
87 static void empth_status(char *format, ...)
88     ATTRIBUTE((format (printf, 1, 2)));
89 static void empth_alarm(int sig);
90
91 static void *
92 empth_start(void *arg)
93 {
94     empth_t *ctx = arg;
95
96     ctx->id = pthread_self();
97     pthread_setspecific(ctx_key, ctx);
98     pthread_mutex_lock(&mtx_ctxsw);
99     *udata = ctx->ud;
100     ctx->ep(ctx->ud);
101     empth_exit();
102     return NULL;
103 }
104
105 static void
106 empth_status(char *format, ...)
107 {
108     va_list ap;
109     static struct timeval startTime;
110     struct timeval tv;
111     char buf[1024];
112     int sec, msec;
113     empth_t *a;
114
115     va_start(ap, format);
116     if (empth_flags & EMPTH_PRINT) {
117         if (startTime.tv_sec == 0)
118             gettimeofday(&startTime, 0);
119         gettimeofday(&tv, 0);
120         sec = tv.tv_sec - startTime.tv_sec;
121         msec = (tv.tv_usec - startTime.tv_usec) / 1000;
122         if (msec < 0) {
123             sec++;
124             msec += 1000;
125         }
126         vsprintf(buf, format, ap);
127         a = empth_self();
128         printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
129                a->name, buf);
130
131     }
132     va_end(ap);
133 }
134
135
136 int
137 empth_init(void **ctx_ptr, int flags)
138 {
139     empth_t *ctx;
140     sigset_t set;
141     struct sigaction act;
142
143     empth_flags = flags;
144     udata = ctx_ptr;
145
146     empth_init_signals();
147     sigemptyset(&set);
148     sigaddset(&set, SIGHUP);
149     sigaddset(&set, SIGINT);
150     sigaddset(&set, SIGTERM);
151     pthread_sigmask(SIG_BLOCK, &set, NULL);
152     act.sa_flags = 0;
153     sigemptyset(&act.sa_mask);
154     act.sa_handler = empth_alarm;
155     sigaction(SIGALRM, &act, NULL);
156
157     pthread_key_create(&ctx_key, NULL);
158     pthread_mutex_init(&mtx_ctxsw, NULL);
159
160     ctx = malloc(sizeof(empth_t));
161     if (!ctx) {
162         logerror("pthread init failed: not enough memory");
163         exit(1);
164     }
165     ctx->name = "Main";
166     ctx->ep = 0;
167     ctx->ud = 0;
168     ctx->id = pthread_self();
169     ctx->wakeup = 0;
170     pthread_setspecific(ctx_key, ctx);
171     pthread_mutex_lock(&mtx_ctxsw);
172     logerror("pthreads initialized");
173     return 0;
174 }
175
176
177 empth_t *
178 empth_create(void (*entry)(void *), int size, int flags,
179              char *name, void *ud)
180 {
181     pthread_t t;
182     pthread_attr_t attr;
183     empth_t *ctx;
184     int eno;
185
186     empth_status("creating new thread %s", name);
187
188     ctx = malloc(sizeof(empth_t));
189     if (!ctx) {
190         logerror("not enough memory to create thread %s", name);
191         return NULL;
192     }
193     ctx->name = strdup(name);
194     ctx->ud = ud;
195     ctx->wakeup = 0;
196     ctx->ep = entry;
197
198     eno = pthread_attr_init(&attr);
199     if (eno) {
200         logerror("can not create thread attribute %s: %s",
201                  name, strerror(eno));
202         goto bad;
203     }
204     if (size < PTHREAD_STACK_MIN)
205         size = PTHREAD_STACK_MIN;
206     pthread_attr_setstacksize(&attr, size);
207     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
208
209     eno = pthread_create(&t, &attr, empth_start, ctx);
210     if (eno) {
211         logerror("can not create thread: %s: %s", name, strerror(eno));
212         goto bad;
213     }
214     empth_status("new thread id is %ld", (long)t);
215     empth_yield();
216     return ctx;
217
218   bad:
219     pthread_attr_destroy(&attr);
220     free(ctx);
221     return NULL;
222 }
223
224
225 static void
226 empth_restorectx(void)
227 {
228     empth_t *ctx_ptr;
229
230     ctx_ptr = pthread_getspecific(ctx_key);
231     *udata = ctx_ptr->ud;
232     ctx_ptr->wakeup = 0;
233     empth_status("context restored");
234 }
235
236 empth_t *
237 empth_self(void)
238 {
239     return pthread_getspecific(ctx_key);
240 }
241
242 char *
243 empth_name(empth_t *thread)
244 {
245     return thread->name;
246 }
247
248 void
249 empth_set_name(empth_t *thread, char *name)
250 {
251     if (thread->name)
252         free(thread->name);
253     thread->name = strdup(name);
254 }
255
256 void
257 empth_exit(void)
258 {
259     empth_t *ctx = pthread_getspecific(ctx_key);
260
261     empth_status("empth_exit");
262     pthread_mutex_unlock(&mtx_ctxsw);
263     free(ctx->name);
264     free(ctx);
265     pthread_exit(0);
266 }
267
268 void
269 empth_yield(void)
270 {
271     pthread_mutex_unlock(&mtx_ctxsw);
272     pthread_mutex_lock(&mtx_ctxsw);
273     empth_restorectx();
274 }
275
276 int
277 empth_select(int fd, int flags, struct timeval *timeout)
278 {
279     fd_set readmask;
280     fd_set writemask;
281     struct timeval tv;
282     int n;
283     int res = 0;
284
285     pthread_mutex_unlock(&mtx_ctxsw);
286     empth_status("select on %d for %d", fd, flags);
287
288     FD_ZERO(&readmask);
289     FD_ZERO(&writemask);
290     if (flags & EMPTH_FD_READ)
291         FD_SET(fd, &readmask);
292     if (flags & EMPTH_FD_WRITE)
293         FD_SET(fd, &writemask);
294
295     if (timeout) {
296         tv = *timeout;
297         timeout = &tv;
298     }
299     n = select(fd + 1, &readmask, &writemask, NULL, timeout);
300
301     if (n < 0) {
302         if (errno == EINTR) /* go handle the signal */
303             empth_status("select broken by signal");
304          else
305             empth_status("select failed (%s)", strerror(errno));
306         res = -1;
307     } else if (n == 0) {
308         empth_status("select timed out");
309         res = 0;
310     } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
311         empth_status("input ready");
312         res = 1;
313     } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
314         empth_status("output ready");
315         res = 1;
316     }
317
318     pthread_mutex_lock(&mtx_ctxsw);
319     empth_restorectx();
320     return res;
321 }
322
323 static void
324 empth_alarm(int sig)
325 {
326     /*
327      * Nothing to do --- we handle this signal just to let
328      * empth_wakeup() interrupt system calls.
329      */
330 }
331
332 void
333 empth_wakeup(empth_t *a)
334 {
335     empth_status("waking up thread %s", a->name);
336     a->wakeup = 1;
337     pthread_kill(a->id, SIGALRM);
338 }
339
340 int
341 empth_sleep(time_t until)
342 {
343     empth_t *ctx = pthread_getspecific(ctx_key);
344     struct timeval tv;
345     int res;
346
347     empth_status("going to sleep %ld sec", until - time(0));
348     pthread_mutex_unlock(&mtx_ctxsw);
349     do {
350         tv.tv_sec = until - time(NULL);
351         tv.tv_usec = 0;
352         res = select(0, NULL, NULL, NULL, &tv);
353     } while (res < 0 && !ctx->wakeup);
354     empth_status("sleep done. Waiting for lock");
355     pthread_mutex_lock(&mtx_ctxsw);
356     empth_restorectx();
357     return res;
358 }
359
360 int
361 empth_wait_for_signal(void)
362 {
363     sigset_t set;
364     int sig, err;
365
366     sigemptyset(&set);
367     sigaddset(&set, SIGHUP);
368     sigaddset(&set, SIGINT);
369     sigaddset(&set, SIGTERM);
370     pthread_mutex_unlock(&mtx_ctxsw);
371     for (;;) {
372         empth_status("waiting for signals");
373         err = sigwait(&set, &sig);
374         if (CANT_HAPPEN(err)) {
375             sleep(60);
376             continue;
377         }
378         empth_status("got awaited signal %d", sig);
379         pthread_mutex_lock(&mtx_ctxsw);
380         empth_restorectx();
381         return sig;
382     }
383 }
384
385 empth_rwlock_t *
386 empth_rwlock_create(char *name)
387 {
388     empth_rwlock_t *rwlock;
389
390     rwlock = malloc(sizeof(*rwlock));
391     if (!rwlock)
392         return NULL;
393
394     if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
395         free(rwlock);
396         return NULL;
397     }
398
399     rwlock->name = strdup(name);
400     return rwlock;
401 }
402
403 void
404 empth_rwlock_destroy(empth_rwlock_t *rwlock)
405 {
406     pthread_rwlock_destroy(&rwlock->lock);
407     free(rwlock->name);
408     free(rwlock);
409 }
410
411 void
412 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
413 {
414     pthread_mutex_unlock(&mtx_ctxsw);
415     pthread_rwlock_wrlock(&rwlock->lock);
416     pthread_mutex_lock(&mtx_ctxsw);
417     empth_restorectx();
418 }
419
420 void
421 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
422 {
423     pthread_mutex_unlock(&mtx_ctxsw);
424     pthread_rwlock_rdlock(&rwlock->lock);
425     pthread_mutex_lock(&mtx_ctxsw);
426     empth_restorectx();
427 }
428
429 void
430 empth_rwlock_unlock(empth_rwlock_t *rwlock)
431 {
432     pthread_rwlock_unlock(&rwlock->lock);
433 }