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)
301 pthread_mutex_unlock(&mtx_ctxsw);
302 empth_status("%s select on %d",
303 flags == EMPTH_FD_READ ? "read" : "write", fd);
313 FD_SET(fd, &readmask);
316 FD_SET(fd, &writemask);
319 logerror("bad flag %d passed to empth_select", flags);
323 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
326 if (errno == EINTR) {
327 /* go handle the signal */
328 empth_status("select broken by signal");
332 /* strange but we dont get EINTR on select broken by signal */
333 empth_status("select failed (%s)", strerror(errno));
338 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
339 empth_status("input ready");
342 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
343 empth_status("output ready");
349 pthread_mutex_lock(&mtx_ctxsw);
357 * Nothing to do --- we handle this signal just to let
358 * empth_wakeup() and empth_terminate() interrupt system calls.
363 empth_wakeup(empth_t *a)
365 empth_status("waking up thread %s", a->name);
367 a->state = EMPTH_INTR;
368 pthread_kill(a->id, SIGALRM);
372 empth_sleep(time_t until)
374 empth_t *ctx = pthread_getspecific(ctx_key);
378 empth_status("going to sleep %ld sec", until - time(0));
379 pthread_mutex_unlock(&mtx_ctxsw);
381 tv.tv_sec = until - time(NULL);
383 res = select(0, NULL, NULL, NULL, &tv);
384 } while (res < 0 && ctx->state == 0);
385 empth_status("sleep done. Waiting for lock");
386 pthread_mutex_lock(&mtx_ctxsw);
392 empth_wait_for_signal(void)
398 sigaddset(&set, SIGHUP);
399 sigaddset(&set, SIGINT);
400 sigaddset(&set, SIGTERM);
401 pthread_mutex_unlock(&mtx_ctxsw);
403 empth_status("waiting for signals");
404 err = sigwait(&set, &sig);
405 if (CANT_HAPPEN(err)) {
409 empth_status("got awaited signal %d", sig);
410 pthread_mutex_lock(&mtx_ctxsw);
417 empth_rwlock_create(char *name)
419 empth_rwlock_t *rwlock;
421 rwlock = malloc(sizeof(*rwlock));
425 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
430 rwlock->name = strdup(name);
435 empth_rwlock_destroy(empth_rwlock_t *rwlock)
437 pthread_rwlock_destroy(&rwlock->lock);
443 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
445 pthread_mutex_unlock(&mtx_ctxsw);
446 pthread_rwlock_wrlock(&rwlock->lock);
447 pthread_mutex_lock(&mtx_ctxsw);
452 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
454 pthread_mutex_unlock(&mtx_ctxsw);
455 pthread_rwlock_rdlock(&rwlock->lock);
456 pthread_mutex_lock(&mtx_ctxsw);
461 empth_rwlock_unlock(empth_rwlock_t *rwlock)
463 pthread_rwlock_unlock(&rwlock->lock);