2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * pthread.c: Interface from Empire threads to POSIX threads
30 * Known contributors to this file:
33 * Markus Armbruster, 2005-2007
34 * Ron Koenderink, 2007
37 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
38 #define _XOPEN_SOURCE 500
50 #include <sys/types.h>
57 #include "empthread.h"
58 #include "prototypes.h"
60 #define EMPTH_KILLED 1
64 char *name; /* thread name */
65 void *ud; /* user data */
66 int state; /* my state */
67 void (*ep)(void *); /* entry point */
68 pthread_t id; /* thread id */
71 struct empth_rwlock_t {
73 pthread_rwlock_t lock;
76 /* Thread-specific data key */
77 static pthread_key_t ctx_key;
79 /* Flags that were passed to empth_init() */
80 static int empth_flags;
82 /* Pointer to thread context variable */
86 * Non-preemption mutex.
87 * Empire code outside this file is only executed while holding this
88 * mutex. This makes sure Empire code is never preempted by Empire
91 static pthread_mutex_t mtx_ctxsw;
93 static void empth_status(char *format, ...)
94 ATTRIBUTE((format (printf, 1, 2)));
95 static void empth_alarm(int sig);
98 empth_start(void *arg)
102 ctx->id = pthread_self();
103 pthread_setspecific(ctx_key, ctx);
104 pthread_mutex_lock(&mtx_ctxsw);
112 empth_status(char *format, ...)
115 static struct timeval startTime;
121 va_start(ap, format);
122 if (empth_flags & EMPTH_PRINT) {
123 if (startTime.tv_sec == 0)
124 gettimeofday(&startTime, 0);
125 gettimeofday(&tv, 0);
126 sec = tv.tv_sec - startTime.tv_sec;
127 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
132 vsprintf(buf, format, ap);
134 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
143 empth_init(void **ctx_ptr, int flags)
147 struct sigaction act;
152 empth_init_signals();
154 sigaddset(&set, SIGHUP);
155 sigaddset(&set, SIGINT);
156 sigaddset(&set, SIGTERM);
157 pthread_sigmask(SIG_BLOCK, &set, NULL);
159 sigemptyset(&act.sa_mask);
160 act.sa_handler = empth_alarm;
161 sigaction(SIGALRM, &act, NULL);
163 pthread_key_create(&ctx_key, NULL);
164 pthread_mutex_init(&mtx_ctxsw, NULL);
166 ctx = malloc(sizeof(empth_t));
168 logerror("pthread init failed: not enough memory");
174 ctx->id = pthread_self();
176 pthread_setspecific(ctx_key, ctx);
177 pthread_mutex_lock(&mtx_ctxsw);
178 logerror("pthreads initialized");
184 empth_create(void (*entry)(void *), int size, int flags,
185 char *name, void *ud)
192 empth_status("creating new thread %s", name);
194 ctx = malloc(sizeof(empth_t));
196 logerror("not enough memory to create thread %s", name);
199 ctx->name = strdup(name);
204 eno = pthread_attr_init(&attr);
206 logerror("can not create thread attribute %s: %s",
207 name, strerror(eno));
210 if (size < PTHREAD_STACK_MIN)
211 size = PTHREAD_STACK_MIN;
212 pthread_attr_setstacksize(&attr, size);
213 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
215 eno = pthread_create(&t, &attr, empth_start, ctx);
217 logerror("can not create thread: %s: %s", name, strerror(eno));
220 empth_status("new thread id is %ld", (long)t);
225 pthread_attr_destroy(&attr);
232 empth_restorectx(void)
236 ctx_ptr = pthread_getspecific(ctx_key);
237 *udata = ctx_ptr->ud;
238 if (ctx_ptr->state == EMPTH_KILLED) {
239 empth_status("i am dead");
243 empth_status("context restored");
249 return pthread_getspecific(ctx_key);
255 empth_t *ctx = pthread_getspecific(ctx_key);
257 empth_status("empth_exit");
258 pthread_mutex_unlock(&mtx_ctxsw);
267 pthread_mutex_unlock(&mtx_ctxsw);
268 pthread_mutex_lock(&mtx_ctxsw);
273 empth_terminate(empth_t *a)
275 empth_status("killing thread %s", a->name);
276 a->state = EMPTH_KILLED;
277 pthread_kill(a->id, SIGALRM);
281 empth_select(int fd, int flags)
289 pthread_mutex_unlock(&mtx_ctxsw);
290 empth_status("%s select on %d",
291 flags == EMPTH_FD_READ ? "read" : "write", fd);
301 FD_SET(fd, &readmask);
304 FD_SET(fd, &writemask);
307 logerror("bad flag %d passed to empth_select", flags);
311 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
314 if (errno == EINTR) {
315 /* go handle the signal */
316 empth_status("select broken by signal");
320 /* strange but we dont get EINTR on select broken by signal */
321 empth_status("select failed (%s)", strerror(errno));
326 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
327 empth_status("input ready");
330 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
331 empth_status("output ready");
337 pthread_mutex_lock(&mtx_ctxsw);
345 * Nothing to do --- we handle this signal just to let
346 * empth_wakeup() and empth_terminate() interrupt system calls.
351 empth_wakeup(empth_t *a)
353 empth_status("waking up thread %s", a->name);
355 a->state = EMPTH_INTR;
356 pthread_kill(a->id, SIGALRM);
360 empth_sleep(time_t until)
362 empth_t *ctx = pthread_getspecific(ctx_key);
366 empth_status("going to sleep %ld sec", until - time(0));
367 pthread_mutex_unlock(&mtx_ctxsw);
369 tv.tv_sec = until - time(NULL);
371 res = select(0, NULL, NULL, NULL, &tv);
372 } while (res < 0 && ctx->state == 0);
373 empth_status("sleep done. Waiting for lock");
374 pthread_mutex_lock(&mtx_ctxsw);
380 empth_wait_for_signal(void)
386 sigaddset(&set, SIGHUP);
387 sigaddset(&set, SIGINT);
388 sigaddset(&set, SIGTERM);
389 pthread_mutex_unlock(&mtx_ctxsw);
391 empth_status("waiting for signals");
392 err = sigwait(&set, &sig);
393 if (CANT_HAPPEN(err)) {
397 empth_status("got awaited signal %d", sig);
398 pthread_mutex_lock(&mtx_ctxsw);
405 empth_rwlock_create(char *name)
407 empth_rwlock_t *rwlock;
409 rwlock = malloc(sizeof(*rwlock));
413 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
418 rwlock->name = strdup(name);
423 empth_rwlock_destroy(empth_rwlock_t *rwlock)
425 pthread_rwlock_destroy(&rwlock->lock);
431 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
433 pthread_mutex_unlock(&mtx_ctxsw);
434 pthread_rwlock_wrlock(&rwlock->lock);
435 pthread_mutex_lock(&mtx_ctxsw);
440 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
442 pthread_mutex_unlock(&mtx_ctxsw);
443 pthread_rwlock_rdlock(&rwlock->lock);
444 pthread_mutex_lock(&mtx_ctxsw);
449 empth_rwlock_unlock(empth_rwlock_t *rwlock)
451 pthread_rwlock_unlock(&rwlock->lock);