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"
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 {
66 /* Can't use pthread_rwlock_t, because it needn't prefer writers */
68 int nread; /* #active readers */
69 int nwrite; /* total #writers (active and waiting) */
70 pthread_cond_t can_read;
71 pthread_cond_t can_write;
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);
193 ctx = malloc(sizeof(empth_t));
195 logerror("not enough memory to create thread %s", name);
198 ctx->name = strdup(name);
203 eno = pthread_attr_init(&attr);
205 logerror("can not create thread attribute %s: %s",
206 name, strerror(eno));
209 if (size < PTHREAD_STACK_MIN)
210 size = PTHREAD_STACK_MIN;
211 pthread_attr_setstacksize(&attr, size);
212 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
214 eno = pthread_create(&t, &attr, empth_start, ctx);
216 logerror("can not create thread: %s: %s", name, strerror(eno));
219 empth_status("new thread id is %ld", (long)t);
224 pthread_attr_destroy(&attr);
231 empth_restorectx(void)
235 ctx_ptr = pthread_getspecific(ctx_key);
236 *udata = ctx_ptr->ud;
238 empth_status("context restored");
244 return pthread_getspecific(ctx_key);
248 empth_name(empth_t *thread)
254 empth_set_name(empth_t *thread, char *name)
258 thread->name = strdup(name);
264 empth_t *ctx = pthread_getspecific(ctx_key);
266 empth_status("empth_exit");
268 pthread_mutex_unlock(&mtx_ctxsw);
278 pthread_mutex_unlock(&mtx_ctxsw);
279 pthread_mutex_lock(&mtx_ctxsw);
284 empth_select(int fd, int flags, struct timeval *timeout)
294 pthread_mutex_unlock(&mtx_ctxsw);
295 empth_status("select on %d for %d", fd, flags);
300 if (flags & EMPTH_FD_READ)
301 FD_SET(fd, &readmask);
302 if (flags & EMPTH_FD_WRITE)
303 FD_SET(fd, &writemask);
307 n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
309 ctx = pthread_getspecific(ctx_key);
311 empth_status("select woken up");
313 } else if (errno == EINTR) {
314 empth_status("select broken by signal");
317 empth_status("select failed (%s)", strerror(errno));
321 empth_status("select timed out");
323 } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
324 empth_status("input ready");
326 } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
327 empth_status("output ready");
331 pthread_mutex_lock(&mtx_ctxsw);
340 * Nothing to do --- we handle this signal just to let
341 * empth_wakeup() interrupt system calls.
346 empth_wakeup(empth_t *a)
348 empth_status("waking up thread %s", a->name);
350 pthread_kill(a->id, SIGALRM);
354 empth_sleep(time_t until)
356 empth_t *ctx = pthread_getspecific(ctx_key);
362 pthread_mutex_unlock(&mtx_ctxsw);
365 tv.tv_sec = until >= now ? until - now : 0;
367 empth_status("going to sleep %ld sec", (long)tv.tv_sec);
368 res = select(0, NULL, NULL, NULL, &tv);
369 } while (res < 0 && !ctx->wakeup);
370 empth_status("sleep done. Waiting for lock");
371 pthread_mutex_lock(&mtx_ctxsw);
377 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_cond_init(&rwlock->can_read, NULL) != 0
412 || pthread_cond_init(&rwlock->can_write, NULL) != 0) {
417 rwlock->name = strdup(name);
418 rwlock->nread = rwlock->nwrite = 0;
423 empth_rwlock_destroy(empth_rwlock_t *rwlock)
425 pthread_cond_destroy(&rwlock->can_read);
426 pthread_cond_destroy(&rwlock->can_write);
432 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
434 empth_status("wrlock %s %d %d",
435 rwlock->name, rwlock->nread, rwlock->nwrite);
438 while (rwlock->nread != 0 || rwlock->nwrite != 1) {
439 empth_status("waiting for wrlock %s", rwlock->name);
440 pthread_cond_wait(&rwlock->can_write, &mtx_ctxsw);
441 empth_status("got wrlock %s %d %d",
442 rwlock->name, rwlock->nread, rwlock->nwrite);
448 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
450 empth_status("rdlock %s %d %d",
451 rwlock->name, rwlock->nread, rwlock->nwrite);
453 while (rwlock->nwrite) {
454 empth_status("waiting for rdlock %s", rwlock->name);
455 pthread_cond_wait(&rwlock->can_read, &mtx_ctxsw);
456 empth_status("got rdlock %s %d %d",
457 rwlock->name, rwlock->nread, rwlock->nwrite);
464 empth_rwlock_unlock(empth_rwlock_t *rwlock)
466 if (CANT_HAPPEN(!rwlock->nread && !rwlock->nwrite))
468 if (rwlock->nread) { /* holding read lock */
469 if (!--rwlock->nread)
470 pthread_cond_signal(&rwlock->can_write);
473 pthread_cond_signal(&rwlock->can_write);
475 if (rwlock->nwrite == 0)
476 pthread_cond_broadcast(&rwlock->can_read);