2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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-2007
34 * Ron Koenderink, 2007
37 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
38 #define _XOPEN_SOURCE 500
50 #include <sys/types.h>
57 #include "empthread.h"
58 #include "prototypes.h"
60 #define EMPTH_KILLED 1
63 char *name; /* thread name */
64 char *desc; /* description */
65 void *ud; /* user data */
66 int state; /* my state */
67 void (*ep)(void *); /* entry point */
68 pthread_t id; /* thread id */
72 pthread_mutex_t mtx_update; /* use it to update count */
75 pthread_mutex_t mtx_sem;
76 pthread_cond_t cnd_sem;
79 struct empth_rwlock_t {
81 pthread_rwlock_t lock;
84 /* Thread-specific data key */
85 static pthread_key_t ctx_key;
87 /* Flags that were passed to empth_init() */
88 static int empth_flags;
90 /* Pointer to thread context variable */
94 * Non-preemption mutex.
95 * Empire code outside this file is only executed while holding this
96 * mutex. This makes sure Empire code is never preempted by Empire
99 static pthread_mutex_t mtx_ctxsw;
101 static void empth_status(char *format, ...)
102 ATTRIBUTE((format (printf, 1, 2)));
103 static void empth_alarm(int sig);
106 empth_start(void *arg)
110 ctx->id = pthread_self();
111 pthread_setspecific(ctx_key, ctx);
112 pthread_mutex_lock(&mtx_ctxsw);
120 empth_status(char *format, ...)
123 static struct timeval startTime;
129 va_start(ap, format);
130 if (empth_flags & EMPTH_PRINT) {
131 if (startTime.tv_sec == 0)
132 gettimeofday(&startTime, 0);
133 gettimeofday(&tv, 0);
134 sec = tv.tv_sec - startTime.tv_sec;
135 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
140 vsprintf(buf, format, ap);
142 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
151 empth_init(void **ctx_ptr, int flags)
155 struct sigaction act;
160 empth_init_signals();
162 sigaddset(&set, SIGHUP);
163 sigaddset(&set, SIGINT);
164 sigaddset(&set, SIGTERM);
165 pthread_sigmask(SIG_BLOCK, &set, NULL);
167 sigemptyset(&act.sa_mask);
168 act.sa_handler = empth_alarm;
169 sigaction(SIGALRM, &act, NULL);
171 pthread_key_create(&ctx_key, NULL);
172 pthread_mutex_init(&mtx_ctxsw, NULL);
174 ctx = malloc(sizeof(empth_t));
176 logerror("pthread init failed: not enough memory");
180 ctx->desc = "empire main";
183 ctx->id = pthread_self();
185 pthread_setspecific(ctx_key, ctx);
186 pthread_mutex_lock(&mtx_ctxsw);
187 logerror("pthreads initialized");
193 * prio can be used for setting scheeduling policy but...
194 * it seems to be optional in POSIX threads and Solaris
195 * for example just ignores it.
196 * More then that priority is not needed even in lwp threads.
199 empth_create(int prio, void (*entry)(void *), int size, int flags,
200 char *name, char *desc, void *ud)
207 empth_status("creating new thread %s:%s", name, desc);
209 ctx = malloc(sizeof(empth_t));
211 logerror("not enough memory to create thread: %s (%s)",
215 ctx->name = strdup(name);
216 ctx->desc = strdup(desc);
221 eno = pthread_attr_init(&attr);
223 logerror("can not create thread attribute %s (%s): %s",
224 name, desc, strerror(eno));
227 if (size < PTHREAD_STACK_MIN)
228 size = PTHREAD_STACK_MIN;
229 pthread_attr_setstacksize(&attr, size);
230 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
232 eno = pthread_create(&t, &attr, empth_start, ctx);
234 logerror("can not create thread: %s (%s): %s",
235 name, desc, strerror(eno));
238 empth_status("new thread id is %ld", (long)t);
243 pthread_attr_destroy(&attr);
250 empth_restorectx(void)
254 ctx_ptr = pthread_getspecific(ctx_key);
255 *udata = ctx_ptr->ud;
256 if (ctx_ptr->state == EMPTH_KILLED) {
257 empth_status("i am dead");
260 empth_status("context restored");
266 return pthread_getspecific(ctx_key);
272 empth_status("empth_exit");
273 pthread_mutex_unlock(&mtx_ctxsw);
274 free(pthread_getspecific(ctx_key));
281 pthread_mutex_unlock(&mtx_ctxsw);
282 pthread_mutex_lock(&mtx_ctxsw);
287 empth_terminate(empth_t *a)
289 empth_status("killing thread %s", a->name);
290 a->state = EMPTH_KILLED;
291 pthread_kill(a->id, SIGALRM);
295 empth_select(int fd, int flags)
303 pthread_mutex_unlock(&mtx_ctxsw);
304 empth_status("%s select on %d",
305 flags == EMPTH_FD_READ ? "read" : "write", fd);
315 FD_SET(fd, &readmask);
318 FD_SET(fd, &writemask);
321 logerror("bad flag %d passed to empth_select", flags);
325 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
328 if (errno == EINTR) {
329 /* go handle the signal */
330 empth_status("select broken by signal");
334 /* strange but we dont get EINTR on select broken by signal */
335 empth_status("select failed (%s)", strerror(errno));
340 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
341 empth_status("input ready");
344 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
345 empth_status("output ready");
351 pthread_mutex_lock(&mtx_ctxsw);
359 * Nothing to do --- we handle this signal just to let
360 * empth_wakeup() interrupt system calls.
362 empth_status("got alarm signal");
366 empth_wakeup(empth_t *a)
368 empth_status("waking up thread %s", a->name);
369 pthread_kill(a->id, SIGALRM);
373 empth_sleep(time_t until)
377 empth_status("going to sleep %ld sec", until - time(0));
378 pthread_mutex_unlock(&mtx_ctxsw);
379 tv.tv_sec = until - time(NULL);
382 select(0, NULL, NULL, NULL, &tv);
383 } while ((tv.tv_sec = until - time(NULL)) > 0);
384 empth_status("sleep done. Waiting for lock");
385 pthread_mutex_lock(&mtx_ctxsw);
390 empth_wait_for_signal(void)
396 sigaddset(&set, SIGHUP);
397 sigaddset(&set, SIGINT);
398 sigaddset(&set, SIGTERM);
399 pthread_mutex_unlock(&mtx_ctxsw);
401 empth_status("waiting for signals");
402 err = sigwait(&set, &sig);
403 if (CANT_HAPPEN(err)) {
407 empth_status("got awaited signal %d", sig);
408 pthread_mutex_lock(&mtx_ctxsw);
415 empth_sem_create(char *name, int cnt)
419 sm = malloc(sizeof(empth_sem_t));
421 logerror("out of memory at %s:%d", __FILE__, __LINE__);
424 strncpy(sm->name, name, sizeof(sm->name) - 1);
426 pthread_mutex_init(&sm->mtx_update, NULL);
427 pthread_mutex_init(&sm->mtx_sem, NULL);
428 pthread_cond_init(&sm->cnd_sem, NULL);
433 empth_sem_signal(empth_sem_t *sm)
435 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
436 pthread_mutex_lock(&sm->mtx_update);
437 if (sm->count++ < 0) {
438 pthread_mutex_unlock(&sm->mtx_update);
439 pthread_mutex_lock(&sm->mtx_sem);
440 pthread_cond_signal(&sm->cnd_sem);
441 pthread_mutex_unlock(&sm->mtx_sem);
443 pthread_mutex_unlock(&sm->mtx_update);
447 empth_sem_wait(empth_sem_t *sm)
449 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
450 pthread_mutex_lock(&sm->mtx_update);
451 if (--sm->count < 0) {
452 pthread_mutex_unlock(&sm->mtx_update);
453 empth_status("blocking");
454 pthread_mutex_unlock(&mtx_ctxsw);
455 pthread_mutex_lock(&sm->mtx_sem);
456 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
457 empth_status("waking up");
458 pthread_mutex_unlock(&sm->mtx_sem);
459 pthread_mutex_lock(&mtx_ctxsw);
462 pthread_mutex_unlock(&sm->mtx_update);
466 empth_rwlock_create(char *name)
468 empth_rwlock_t *rwlock;
470 rwlock = malloc(sizeof(*rwlock));
474 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
479 rwlock->name = strdup(name);
484 empth_rwlock_destroy(empth_rwlock_t *rwlock)
486 pthread_rwlock_destroy(&rwlock->lock);
491 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
493 pthread_mutex_unlock(&mtx_ctxsw);
494 pthread_rwlock_wrlock(&rwlock->lock);
495 pthread_mutex_lock(&mtx_ctxsw);
500 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
502 pthread_mutex_unlock(&mtx_ctxsw);
503 pthread_rwlock_rdlock(&rwlock->lock);
504 pthread_mutex_lock(&mtx_ctxsw);
509 empth_rwlock_unlock(empth_rwlock_t *rwlock)
511 pthread_rwlock_unlock(&rwlock->lock);