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
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>
55 #include "empthread.h"
56 #include "prototypes.h"
58 #define EMPTH_KILLED 1
62 char *name; /* thread name */
63 void *ud; /* user data */
64 int state; /* my state */
65 void (*ep)(void *); /* entry point */
66 pthread_t id; /* thread id */
69 struct empth_rwlock_t {
71 pthread_rwlock_t lock;
74 /* Thread-specific data key */
75 static pthread_key_t ctx_key;
77 /* Flags that were passed to empth_init() */
78 static int empth_flags;
80 /* Pointer to thread context variable */
84 * Non-preemption mutex.
85 * Empire code outside this file is only executed while holding this
86 * mutex. This makes sure Empire code is never preempted by Empire
89 static pthread_mutex_t mtx_ctxsw;
91 static void empth_status(char *format, ...)
92 ATTRIBUTE((format (printf, 1, 2)));
93 static void empth_alarm(int sig);
96 empth_start(void *arg)
100 ctx->id = pthread_self();
101 pthread_setspecific(ctx_key, ctx);
102 pthread_mutex_lock(&mtx_ctxsw);
110 empth_status(char *format, ...)
113 static struct timeval startTime;
119 va_start(ap, format);
120 if (empth_flags & EMPTH_PRINT) {
121 if (startTime.tv_sec == 0)
122 gettimeofday(&startTime, 0);
123 gettimeofday(&tv, 0);
124 sec = tv.tv_sec - startTime.tv_sec;
125 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
130 vsprintf(buf, format, ap);
132 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
141 empth_init(void **ctx_ptr, int flags)
145 struct sigaction act;
150 empth_init_signals();
152 sigaddset(&set, SIGHUP);
153 sigaddset(&set, SIGINT);
154 sigaddset(&set, SIGTERM);
155 pthread_sigmask(SIG_BLOCK, &set, NULL);
157 sigemptyset(&act.sa_mask);
158 act.sa_handler = empth_alarm;
159 sigaction(SIGALRM, &act, NULL);
161 pthread_key_create(&ctx_key, NULL);
162 pthread_mutex_init(&mtx_ctxsw, NULL);
164 ctx = malloc(sizeof(empth_t));
166 logerror("pthread init failed: not enough memory");
172 ctx->id = pthread_self();
174 pthread_setspecific(ctx_key, ctx);
175 pthread_mutex_lock(&mtx_ctxsw);
176 logerror("pthreads initialized");
182 empth_create(void (*entry)(void *), int size, int flags,
183 char *name, void *ud)
190 empth_status("creating new thread %s", name);
192 ctx = malloc(sizeof(empth_t));
194 logerror("not enough memory to create thread %s", name);
197 ctx->name = strdup(name);
202 eno = pthread_attr_init(&attr);
204 logerror("can not create thread attribute %s: %s",
205 name, strerror(eno));
208 if (size < PTHREAD_STACK_MIN)
209 size = PTHREAD_STACK_MIN;
210 pthread_attr_setstacksize(&attr, size);
211 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
213 eno = pthread_create(&t, &attr, empth_start, ctx);
215 logerror("can not create thread: %s: %s", name, strerror(eno));
218 empth_status("new thread id is %ld", (long)t);
223 pthread_attr_destroy(&attr);
230 empth_restorectx(void)
234 ctx_ptr = pthread_getspecific(ctx_key);
235 *udata = ctx_ptr->ud;
236 if (ctx_ptr->state == EMPTH_KILLED) {
237 empth_status("i am dead");
241 empth_status("context restored");
247 return pthread_getspecific(ctx_key);
251 empth_name(empth_t *thread)
257 empth_set_name(empth_t *thread, char *name)
261 thread->name = strdup(name);
267 empth_t *ctx = pthread_getspecific(ctx_key);
269 empth_status("empth_exit");
270 pthread_mutex_unlock(&mtx_ctxsw);
279 pthread_mutex_unlock(&mtx_ctxsw);
280 pthread_mutex_lock(&mtx_ctxsw);
285 empth_terminate(empth_t *a)
287 empth_status("killing thread %s", a->name);
288 a->state = EMPTH_KILLED;
289 pthread_kill(a->id, SIGALRM);
293 empth_select(int fd, int flags, struct timeval *timeout)
301 pthread_mutex_unlock(&mtx_ctxsw);
302 empth_status("select on %d for %d", fd, flags);
306 if (flags & EMPTH_FD_READ)
307 FD_SET(fd, &readmask);
308 if (flags & EMPTH_FD_WRITE)
309 FD_SET(fd, &writemask);
315 n = select(fd + 1, &readmask, &writemask, NULL, timeout);
318 if (errno == EINTR) /* go handle the signal */
319 empth_status("select broken by signal");
321 empth_status("select failed (%s)", strerror(errno));
324 empth_status("select timed out");
326 } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
327 empth_status("input ready");
329 } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
330 empth_status("output ready");
334 pthread_mutex_lock(&mtx_ctxsw);
343 * Nothing to do --- we handle this signal just to let
344 * empth_wakeup() and empth_terminate() interrupt system calls.
349 empth_wakeup(empth_t *a)
351 empth_status("waking up thread %s", a->name);
353 a->state = EMPTH_INTR;
354 pthread_kill(a->id, SIGALRM);
358 empth_sleep(time_t until)
360 empth_t *ctx = pthread_getspecific(ctx_key);
364 empth_status("going to sleep %ld sec", until - time(0));
365 pthread_mutex_unlock(&mtx_ctxsw);
367 tv.tv_sec = until - time(NULL);
369 res = select(0, NULL, NULL, NULL, &tv);
370 } while (res < 0 && ctx->state == 0);
371 empth_status("sleep done. Waiting for lock");
372 pthread_mutex_lock(&mtx_ctxsw);
378 empth_wait_for_signal(void)
384 sigaddset(&set, SIGHUP);
385 sigaddset(&set, SIGINT);
386 sigaddset(&set, SIGTERM);
387 pthread_mutex_unlock(&mtx_ctxsw);
389 empth_status("waiting for signals");
390 err = sigwait(&set, &sig);
391 if (CANT_HAPPEN(err)) {
395 empth_status("got awaited signal %d", sig);
396 pthread_mutex_lock(&mtx_ctxsw);
403 empth_rwlock_create(char *name)
405 empth_rwlock_t *rwlock;
407 rwlock = malloc(sizeof(*rwlock));
411 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
416 rwlock->name = strdup(name);
421 empth_rwlock_destroy(empth_rwlock_t *rwlock)
423 pthread_rwlock_destroy(&rwlock->lock);
429 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
431 pthread_mutex_unlock(&mtx_ctxsw);
432 pthread_rwlock_wrlock(&rwlock->lock);
433 pthread_mutex_lock(&mtx_ctxsw);
438 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
440 pthread_mutex_unlock(&mtx_ctxsw);
441 pthread_rwlock_rdlock(&rwlock->lock);
442 pthread_mutex_lock(&mtx_ctxsw);
447 empth_rwlock_unlock(empth_rwlock_t *rwlock)
449 pthread_rwlock_unlock(&rwlock->lock);