2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * pthread.c: Interface from Empire threads to POSIX threads
29 * Known contributors to this file:
32 * Markus Armbruster, 2005-2011
33 * Ron Koenderink, 2007-2009
36 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
37 #define _XOPEN_SOURCE 500
49 #include <sys/select.h>
52 #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);
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;
237 empth_status("context restored");
243 return pthread_getspecific(ctx_key);
247 empth_name(empth_t *thread)
253 empth_set_name(empth_t *thread, char *name)
257 thread->name = strdup(name);
263 empth_t *ctx = pthread_getspecific(ctx_key);
265 empth_status("empth_exit");
267 pthread_mutex_unlock(&mtx_ctxsw);
277 pthread_mutex_unlock(&mtx_ctxsw);
278 pthread_mutex_lock(&mtx_ctxsw);
283 empth_select(int fd, int flags, struct timeval *timeout)
292 if (CANT_HAPPEN(fd < 0 || fd >= FD_SETSIZE)) {
298 pthread_mutex_unlock(&mtx_ctxsw);
299 empth_status("select on %d for %d", fd, flags);
304 if (flags & EMPTH_FD_READ)
305 FD_SET(fd, &readmask);
306 if (flags & EMPTH_FD_WRITE)
307 FD_SET(fd, &writemask);
311 n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
313 ctx = pthread_getspecific(ctx_key);
315 empth_status("select woken up");
317 } else if (errno == EINTR) {
318 empth_status("select broken by signal");
321 empth_status("select failed (%s)", strerror(errno));
325 empth_status("select timed out");
327 } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
328 empth_status("input ready");
330 } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
331 empth_status("output ready");
335 pthread_mutex_lock(&mtx_ctxsw);
344 * Nothing to do --- we handle this signal just to let
345 * empth_wakeup() interrupt system calls.
350 empth_wakeup(empth_t *a)
352 empth_status("waking up thread %s", a->name);
354 pthread_kill(a->id, SIGALRM);
358 empth_sleep(time_t until)
360 empth_t *ctx = pthread_getspecific(ctx_key);
366 pthread_mutex_unlock(&mtx_ctxsw);
369 tv.tv_sec = until >= now ? until - now : 0;
371 empth_status("going to sleep %ld sec", (long)tv.tv_sec);
372 res = select(0, NULL, NULL, NULL, &tv);
373 } while (res < 0 && !ctx->wakeup);
374 empth_status("sleep done. Waiting for lock");
375 pthread_mutex_lock(&mtx_ctxsw);
381 empth_wait_for_signal(void)
388 sigaddset(&set, SIGHUP);
389 sigaddset(&set, SIGINT);
390 sigaddset(&set, SIGTERM);
391 pthread_mutex_unlock(&mtx_ctxsw);
393 empth_status("waiting for signals");
394 err = sigwait(&set, &sig);
395 if (CANT_HAPPEN(err)) {
399 empth_status("got awaited signal %d", sig);
400 pthread_mutex_lock(&mtx_ctxsw);
407 empth_rwlock_create(char *name)
409 empth_rwlock_t *rwlock;
411 rwlock = malloc(sizeof(*rwlock));
415 if (pthread_cond_init(&rwlock->can_read, NULL) != 0
416 || pthread_cond_init(&rwlock->can_write, NULL) != 0) {
421 rwlock->name = strdup(name);
422 rwlock->nread = rwlock->nwrite = 0;
427 empth_rwlock_destroy(empth_rwlock_t *rwlock)
429 pthread_cond_destroy(&rwlock->can_read);
430 pthread_cond_destroy(&rwlock->can_write);
436 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
438 empth_status("wrlock %s %d %d",
439 rwlock->name, rwlock->nread, rwlock->nwrite);
442 while (rwlock->nread != 0 || rwlock->nwrite != 1) {
443 empth_status("waiting for wrlock %s", rwlock->name);
444 pthread_cond_wait(&rwlock->can_write, &mtx_ctxsw);
445 empth_status("got wrlock %s %d %d",
446 rwlock->name, rwlock->nread, rwlock->nwrite);
452 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
454 empth_status("rdlock %s %d %d",
455 rwlock->name, rwlock->nread, rwlock->nwrite);
457 while (rwlock->nwrite) {
458 empth_status("waiting for rdlock %s", rwlock->name);
459 pthread_cond_wait(&rwlock->can_read, &mtx_ctxsw);
460 empth_status("got rdlock %s %d %d",
461 rwlock->name, rwlock->nread, rwlock->nwrite);
468 empth_rwlock_unlock(empth_rwlock_t *rwlock)
470 if (CANT_HAPPEN(!rwlock->nread && !rwlock->nwrite))
472 if (rwlock->nread) { /* holding read lock */
473 if (!--rwlock->nread)
474 pthread_cond_signal(&rwlock->can_write);
477 pthread_cond_signal(&rwlock->can_write);
479 if (rwlock->nwrite == 0)
480 pthread_cond_broadcast(&rwlock->can_read);