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 {
65 /* Can't use pthread_rwlock_t, because it needn't prefer writers */
67 int nread; /* #active readers */
68 int nwrite; /* total #writers (active and waiting) */
69 pthread_cond_t can_read;
70 pthread_cond_t can_write;
73 /* Thread-specific data key */
74 static pthread_key_t ctx_key;
76 /* Flags that were passed to empth_init() */
77 static int empth_flags;
79 /* Pointer to thread context variable */
83 * Non-preemption mutex.
84 * Empire code outside this file is only executed while holding this
85 * mutex. This makes sure Empire code is never preempted by Empire
88 static pthread_mutex_t mtx_ctxsw;
90 static void empth_status(char *format, ...)
91 ATTRIBUTE((format (printf, 1, 2)));
92 static void empth_alarm(int sig);
95 empth_start(void *arg)
99 ctx->id = pthread_self();
100 pthread_setspecific(ctx_key, ctx);
101 pthread_mutex_lock(&mtx_ctxsw);
109 empth_status(char *format, ...)
112 static struct timeval startTime;
118 va_start(ap, format);
119 if (empth_flags & EMPTH_PRINT) {
120 if (startTime.tv_sec == 0)
121 gettimeofday(&startTime, 0);
122 gettimeofday(&tv, 0);
123 sec = tv.tv_sec - startTime.tv_sec;
124 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
129 vsprintf(buf, format, ap);
131 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
140 empth_init(void **ctx_ptr, int flags)
144 struct sigaction act;
149 empth_init_signals();
151 sigaddset(&set, SIGHUP);
152 sigaddset(&set, SIGINT);
153 sigaddset(&set, SIGTERM);
154 pthread_sigmask(SIG_BLOCK, &set, NULL);
156 sigemptyset(&act.sa_mask);
157 act.sa_handler = empth_alarm;
158 sigaction(SIGALRM, &act, NULL);
160 pthread_key_create(&ctx_key, NULL);
161 pthread_mutex_init(&mtx_ctxsw, NULL);
163 ctx = malloc(sizeof(empth_t));
165 logerror("pthread init failed: not enough memory");
171 ctx->id = pthread_self();
173 pthread_setspecific(ctx_key, ctx);
174 pthread_mutex_lock(&mtx_ctxsw);
175 logerror("pthreads initialized");
181 empth_create(void (*entry)(void *), int size, int flags,
182 char *name, void *ud)
189 empth_status("creating new thread %s", name);
191 ctx = malloc(sizeof(empth_t));
193 logerror("not enough memory to create thread %s", name);
196 ctx->name = strdup(name);
201 eno = pthread_attr_init(&attr);
203 logerror("can not create thread attribute %s: %s",
204 name, strerror(eno));
207 if (size < PTHREAD_STACK_MIN)
208 size = PTHREAD_STACK_MIN;
209 pthread_attr_setstacksize(&attr, size);
210 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
212 eno = pthread_create(&t, &attr, empth_start, ctx);
214 logerror("can not create thread: %s: %s", name, strerror(eno));
217 empth_status("new thread id is %ld", (long)t);
222 pthread_attr_destroy(&attr);
229 empth_restorectx(void)
233 ctx_ptr = pthread_getspecific(ctx_key);
234 *udata = ctx_ptr->ud;
236 empth_status("context restored");
242 return pthread_getspecific(ctx_key);
246 empth_name(empth_t *thread)
252 empth_set_name(empth_t *thread, char *name)
256 thread->name = strdup(name);
262 empth_t *ctx = pthread_getspecific(ctx_key);
264 empth_status("empth_exit");
265 pthread_mutex_unlock(&mtx_ctxsw);
274 pthread_mutex_unlock(&mtx_ctxsw);
275 pthread_mutex_lock(&mtx_ctxsw);
280 empth_select(int fd, int flags, struct timeval *timeout)
289 pthread_mutex_unlock(&mtx_ctxsw);
290 empth_status("select on %d for %d", fd, flags);
295 if (flags & EMPTH_FD_READ)
296 FD_SET(fd, &readmask);
297 if (flags & EMPTH_FD_WRITE)
298 FD_SET(fd, &writemask);
302 n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
304 ctx = pthread_getspecific(ctx_key);
306 empth_status("select woken up");
308 } else if (errno == EINTR) {
309 empth_status("select broken by signal");
312 empth_status("select failed (%s)", strerror(errno));
316 empth_status("select timed out");
318 } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
319 empth_status("input ready");
321 } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
322 empth_status("output ready");
326 pthread_mutex_lock(&mtx_ctxsw);
335 * Nothing to do --- we handle this signal just to let
336 * empth_wakeup() interrupt system calls.
341 empth_wakeup(empth_t *a)
343 empth_status("waking up thread %s", a->name);
345 pthread_kill(a->id, SIGALRM);
349 empth_sleep(time_t until)
351 empth_t *ctx = pthread_getspecific(ctx_key);
356 pthread_mutex_unlock(&mtx_ctxsw);
359 tv.tv_sec = until >= now ? until - now : 0;
361 empth_status("going to sleep %ld sec", (long)tv.tv_sec);
362 res = select(0, NULL, NULL, NULL, &tv);
363 } while (res < 0 && !ctx->wakeup);
364 empth_status("sleep done. Waiting for lock");
365 pthread_mutex_lock(&mtx_ctxsw);
371 empth_wait_for_signal(void)
377 sigaddset(&set, SIGHUP);
378 sigaddset(&set, SIGINT);
379 sigaddset(&set, SIGTERM);
380 pthread_mutex_unlock(&mtx_ctxsw);
382 empth_status("waiting for signals");
383 err = sigwait(&set, &sig);
384 if (CANT_HAPPEN(err)) {
388 empth_status("got awaited signal %d", sig);
389 pthread_mutex_lock(&mtx_ctxsw);
396 empth_rwlock_create(char *name)
398 empth_rwlock_t *rwlock;
400 rwlock = malloc(sizeof(*rwlock));
404 if (pthread_cond_init(&rwlock->can_read, NULL) != 0
405 || pthread_cond_init(&rwlock->can_write, NULL) != 0) {
410 rwlock->name = strdup(name);
411 rwlock->nread = rwlock->nwrite = 0;
416 empth_rwlock_destroy(empth_rwlock_t *rwlock)
418 pthread_cond_destroy(&rwlock->can_read);
419 pthread_cond_destroy(&rwlock->can_write);
425 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
427 empth_status("wrlock %s %d %d",
428 rwlock->name, rwlock->nread, rwlock->nwrite);
430 while (rwlock->nread != 0 || rwlock->nwrite != 1) {
431 empth_status("waiting for wrlock %s", rwlock->name);
432 pthread_cond_wait(&rwlock->can_write, &mtx_ctxsw);
433 empth_status("got wrlock %s %d %d",
434 rwlock->name, rwlock->nread, rwlock->nwrite);
440 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
442 empth_status("rdlock %s %d %d",
443 rwlock->name, rwlock->nread, rwlock->nwrite);
444 while (rwlock->nwrite) {
445 empth_status("waiting for rdlock %s", rwlock->name);
446 pthread_cond_wait(&rwlock->can_read, &mtx_ctxsw);
447 empth_status("got rdlock %s %d %d",
448 rwlock->name, rwlock->nread, rwlock->nwrite);
455 empth_rwlock_unlock(empth_rwlock_t *rwlock)
457 if (CANT_HAPPEN(!rwlock->nread && !rwlock->nwrite))
459 if (rwlock->nread) { /* holding read lock */
460 if (!--rwlock->nread)
461 pthread_cond_signal(&rwlock->can_write);
464 pthread_cond_signal(&rwlock->can_write);
466 if (rwlock->nwrite == 0)
467 pthread_cond_broadcast(&rwlock->can_read);