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/types.h>
54 #include "empthread.h"
55 #include "prototypes.h"
58 char *name; /* thread name */
59 void *ud; /* user data */
61 void (*ep)(void *); /* entry point */
62 pthread_t id; /* thread id */
65 struct empth_rwlock_t {
67 pthread_rwlock_t lock;
70 /* Thread-specific data key */
71 static pthread_key_t ctx_key;
73 /* Flags that were passed to empth_init() */
74 static int empth_flags;
76 /* Pointer to thread context variable */
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
85 static pthread_mutex_t mtx_ctxsw;
87 static void empth_status(char *format, ...)
88 ATTRIBUTE((format (printf, 1, 2)));
89 static void empth_alarm(int sig);
92 empth_start(void *arg)
96 ctx->id = pthread_self();
97 pthread_setspecific(ctx_key, ctx);
98 pthread_mutex_lock(&mtx_ctxsw);
106 empth_status(char *format, ...)
109 static struct timeval startTime;
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;
126 vsprintf(buf, format, ap);
128 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
137 empth_init(void **ctx_ptr, int flags)
141 struct sigaction act;
146 empth_init_signals();
148 sigaddset(&set, SIGHUP);
149 sigaddset(&set, SIGINT);
150 sigaddset(&set, SIGTERM);
151 pthread_sigmask(SIG_BLOCK, &set, NULL);
153 sigemptyset(&act.sa_mask);
154 act.sa_handler = empth_alarm;
155 sigaction(SIGALRM, &act, NULL);
157 pthread_key_create(&ctx_key, NULL);
158 pthread_mutex_init(&mtx_ctxsw, NULL);
160 ctx = malloc(sizeof(empth_t));
162 logerror("pthread init failed: not enough memory");
168 ctx->id = pthread_self();
170 pthread_setspecific(ctx_key, ctx);
171 pthread_mutex_lock(&mtx_ctxsw);
172 logerror("pthreads initialized");
178 empth_create(void (*entry)(void *), int size, int flags,
179 char *name, void *ud)
186 empth_status("creating new thread %s", name);
188 ctx = malloc(sizeof(empth_t));
190 logerror("not enough memory to create thread %s", name);
193 ctx->name = strdup(name);
198 eno = pthread_attr_init(&attr);
200 logerror("can not create thread attribute %s: %s",
201 name, strerror(eno));
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);
209 eno = pthread_create(&t, &attr, empth_start, ctx);
211 logerror("can not create thread: %s: %s", name, strerror(eno));
214 empth_status("new thread id is %ld", (long)t);
219 pthread_attr_destroy(&attr);
226 empth_restorectx(void)
230 ctx_ptr = pthread_getspecific(ctx_key);
231 *udata = ctx_ptr->ud;
233 empth_status("context restored");
239 return pthread_getspecific(ctx_key);
243 empth_name(empth_t *thread)
249 empth_set_name(empth_t *thread, char *name)
253 thread->name = strdup(name);
259 empth_t *ctx = pthread_getspecific(ctx_key);
261 empth_status("empth_exit");
262 pthread_mutex_unlock(&mtx_ctxsw);
271 pthread_mutex_unlock(&mtx_ctxsw);
272 pthread_mutex_lock(&mtx_ctxsw);
277 empth_select(int fd, int flags, struct timeval *timeout)
286 pthread_mutex_unlock(&mtx_ctxsw);
287 empth_status("select on %d for %d", fd, flags);
292 if (flags & EMPTH_FD_READ)
293 FD_SET(fd, &readmask);
294 if (flags & EMPTH_FD_WRITE)
295 FD_SET(fd, &writemask);
299 n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
301 ctx = pthread_getspecific(ctx_key);
303 empth_status("select woken up");
305 } else if (errno == EINTR) {
306 empth_status("select broken by signal");
309 empth_status("select failed (%s)", strerror(errno));
313 empth_status("select timed out");
315 } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
316 empth_status("input ready");
318 } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
319 empth_status("output ready");
323 pthread_mutex_lock(&mtx_ctxsw);
332 * Nothing to do --- we handle this signal just to let
333 * empth_wakeup() interrupt system calls.
338 empth_wakeup(empth_t *a)
340 empth_status("waking up thread %s", a->name);
342 pthread_kill(a->id, SIGALRM);
346 empth_sleep(time_t until)
348 empth_t *ctx = pthread_getspecific(ctx_key);
352 empth_status("going to sleep %ld sec", until - time(0));
353 pthread_mutex_unlock(&mtx_ctxsw);
355 tv.tv_sec = until - time(NULL);
357 res = select(0, NULL, NULL, NULL, &tv);
358 } while (res < 0 && !ctx->wakeup);
359 empth_status("sleep done. Waiting for lock");
360 pthread_mutex_lock(&mtx_ctxsw);
366 empth_wait_for_signal(void)
372 sigaddset(&set, SIGHUP);
373 sigaddset(&set, SIGINT);
374 sigaddset(&set, SIGTERM);
375 pthread_mutex_unlock(&mtx_ctxsw);
377 empth_status("waiting for signals");
378 err = sigwait(&set, &sig);
379 if (CANT_HAPPEN(err)) {
383 empth_status("got awaited signal %d", sig);
384 pthread_mutex_lock(&mtx_ctxsw);
391 empth_rwlock_create(char *name)
393 empth_rwlock_t *rwlock;
395 rwlock = malloc(sizeof(*rwlock));
399 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
404 rwlock->name = strdup(name);
409 empth_rwlock_destroy(empth_rwlock_t *rwlock)
411 pthread_rwlock_destroy(&rwlock->lock);
417 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
419 pthread_mutex_unlock(&mtx_ctxsw);
420 pthread_rwlock_wrlock(&rwlock->lock);
421 pthread_mutex_lock(&mtx_ctxsw);
426 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
428 pthread_mutex_unlock(&mtx_ctxsw);
429 pthread_rwlock_rdlock(&rwlock->lock);
430 pthread_mutex_lock(&mtx_ctxsw);
435 empth_rwlock_unlock(empth_rwlock_t *rwlock)
437 pthread_rwlock_unlock(&rwlock->lock);