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
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-2009
34 * Ron Koenderink, 2007-2009
37 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
38 #define _XOPEN_SOURCE 500
50 #include <sys/select.h>
53 #include "empthread.h"
54 #include "prototypes.h"
57 char *name; /* thread name */
58 void *ud; /* user data */
60 void (*ep)(void *); /* entry point */
61 pthread_t id; /* thread id */
64 struct empth_rwlock_t {
66 pthread_rwlock_t lock;
69 /* Thread-specific data key */
70 static pthread_key_t ctx_key;
72 /* Flags that were passed to empth_init() */
73 static int empth_flags;
75 /* Pointer to thread context variable */
79 * Non-preemption mutex.
80 * Empire code outside this file is only executed while holding this
81 * mutex. This makes sure Empire code is never preempted by Empire
84 static pthread_mutex_t mtx_ctxsw;
86 static void empth_status(char *format, ...)
87 ATTRIBUTE((format (printf, 1, 2)));
88 static void empth_alarm(int sig);
91 empth_start(void *arg)
95 ctx->id = pthread_self();
96 pthread_setspecific(ctx_key, ctx);
97 pthread_mutex_lock(&mtx_ctxsw);
105 empth_status(char *format, ...)
108 static struct timeval startTime;
114 va_start(ap, format);
115 if (empth_flags & EMPTH_PRINT) {
116 if (startTime.tv_sec == 0)
117 gettimeofday(&startTime, 0);
118 gettimeofday(&tv, 0);
119 sec = tv.tv_sec - startTime.tv_sec;
120 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
125 vsprintf(buf, format, ap);
127 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
136 empth_init(void **ctx_ptr, int flags)
140 struct sigaction act;
145 empth_init_signals();
147 sigaddset(&set, SIGHUP);
148 sigaddset(&set, SIGINT);
149 sigaddset(&set, SIGTERM);
150 pthread_sigmask(SIG_BLOCK, &set, NULL);
152 sigemptyset(&act.sa_mask);
153 act.sa_handler = empth_alarm;
154 sigaction(SIGALRM, &act, NULL);
156 pthread_key_create(&ctx_key, NULL);
157 pthread_mutex_init(&mtx_ctxsw, NULL);
159 ctx = malloc(sizeof(empth_t));
161 logerror("pthread init failed: not enough memory");
167 ctx->id = pthread_self();
169 pthread_setspecific(ctx_key, ctx);
170 pthread_mutex_lock(&mtx_ctxsw);
171 logerror("pthreads initialized");
177 empth_create(void (*entry)(void *), int size, int flags,
178 char *name, void *ud)
185 empth_status("creating new thread %s", name);
187 ctx = malloc(sizeof(empth_t));
189 logerror("not enough memory to create thread %s", name);
192 ctx->name = strdup(name);
197 eno = pthread_attr_init(&attr);
199 logerror("can not create thread attribute %s: %s",
200 name, strerror(eno));
203 if (size < PTHREAD_STACK_MIN)
204 size = PTHREAD_STACK_MIN;
205 pthread_attr_setstacksize(&attr, size);
206 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
208 eno = pthread_create(&t, &attr, empth_start, ctx);
210 logerror("can not create thread: %s: %s", name, strerror(eno));
213 empth_status("new thread id is %ld", (long)t);
218 pthread_attr_destroy(&attr);
225 empth_restorectx(void)
229 ctx_ptr = pthread_getspecific(ctx_key);
230 *udata = ctx_ptr->ud;
232 empth_status("context restored");
238 return pthread_getspecific(ctx_key);
242 empth_name(empth_t *thread)
248 empth_set_name(empth_t *thread, char *name)
252 thread->name = strdup(name);
258 empth_t *ctx = pthread_getspecific(ctx_key);
260 empth_status("empth_exit");
261 pthread_mutex_unlock(&mtx_ctxsw);
270 pthread_mutex_unlock(&mtx_ctxsw);
271 pthread_mutex_lock(&mtx_ctxsw);
276 empth_select(int fd, int flags, struct timeval *timeout)
285 pthread_mutex_unlock(&mtx_ctxsw);
286 empth_status("select on %d for %d", fd, flags);
291 if (flags & EMPTH_FD_READ)
292 FD_SET(fd, &readmask);
293 if (flags & EMPTH_FD_WRITE)
294 FD_SET(fd, &writemask);
298 n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
300 ctx = pthread_getspecific(ctx_key);
302 empth_status("select woken up");
304 } else if (errno == EINTR) {
305 empth_status("select broken by signal");
308 empth_status("select failed (%s)", strerror(errno));
312 empth_status("select timed out");
314 } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
315 empth_status("input ready");
317 } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
318 empth_status("output ready");
322 pthread_mutex_lock(&mtx_ctxsw);
331 * Nothing to do --- we handle this signal just to let
332 * empth_wakeup() interrupt system calls.
337 empth_wakeup(empth_t *a)
339 empth_status("waking up thread %s", a->name);
341 pthread_kill(a->id, SIGALRM);
345 empth_sleep(time_t until)
347 empth_t *ctx = pthread_getspecific(ctx_key);
352 pthread_mutex_unlock(&mtx_ctxsw);
355 tv.tv_sec = until >= now ? until - now : 0;
357 empth_status("going to sleep %ld sec", (long)tv.tv_sec);
358 res = select(0, NULL, NULL, NULL, &tv);
359 } while (res < 0 && !ctx->wakeup);
360 empth_status("sleep done. Waiting for lock");
361 pthread_mutex_lock(&mtx_ctxsw);
367 empth_wait_for_signal(void)
373 sigaddset(&set, SIGHUP);
374 sigaddset(&set, SIGINT);
375 sigaddset(&set, SIGTERM);
376 pthread_mutex_unlock(&mtx_ctxsw);
378 empth_status("waiting for signals");
379 err = sigwait(&set, &sig);
380 if (CANT_HAPPEN(err)) {
384 empth_status("got awaited signal %d", sig);
385 pthread_mutex_lock(&mtx_ctxsw);
392 empth_rwlock_create(char *name)
394 empth_rwlock_t *rwlock;
396 rwlock = malloc(sizeof(*rwlock));
400 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
405 rwlock->name = strdup(name);
410 empth_rwlock_destroy(empth_rwlock_t *rwlock)
412 pthread_rwlock_destroy(&rwlock->lock);
418 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
420 pthread_mutex_unlock(&mtx_ctxsw);
421 pthread_rwlock_wrlock(&rwlock->lock);
422 pthread_mutex_lock(&mtx_ctxsw);
427 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
429 pthread_mutex_unlock(&mtx_ctxsw);
430 pthread_rwlock_rdlock(&rwlock->lock);
431 pthread_mutex_lock(&mtx_ctxsw);
436 empth_rwlock_unlock(empth_rwlock_t *rwlock)
438 pthread_rwlock_unlock(&rwlock->lock);