2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
43 #include <sys/types.h>
50 #include "empthread.h"
51 #include "prototypes.h"
55 #define EMPTH_KILLED 1
58 char *name; /* thread name */
59 char *desc; /* description */
60 void *ud; /* user data */
61 int state; /* my state */
62 void (*ep)(void *); /* entry point */
63 pthread_t id; /* thread id */
67 pthread_mutex_t mtx_update; /* use it to update count */
70 pthread_mutex_t mtx_sem;
71 pthread_cond_t cnd_sem;
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, ...) ATTRIBUTE((format (printf, 1, 2)));
92 static void empth_alarm(int sig);
95 empth_start(void *arg)
100 /* actually it should inherit all this from main but... */
102 sigemptyset(&act.sa_mask);
103 act.sa_handler = shutdwn;
104 sigaction(SIGTERM, &act, NULL);
105 sigaction(SIGINT, &act, NULL);
106 act.sa_handler = panic;
107 sigaction(SIGBUS, &act, NULL);
108 sigaction(SIGSEGV, &act, NULL);
109 sigaction(SIGILL, &act, NULL);
110 sigaction(SIGFPE, &act, NULL);
111 act.sa_handler = SIG_IGN;
112 sigaction(SIGPIPE, &act, NULL);
114 act.sa_handler = empth_alarm;
115 sigaction(SIGALRM, &act, NULL);
117 ctx->id = pthread_self();
118 pthread_setspecific(ctx_key, ctx);
119 pthread_mutex_lock(&mtx_ctxsw);
127 empth_status(char *format, ...)
130 static struct timeval startTime;
136 va_start(ap, format);
137 if (empth_flags & EMPTH_PRINT) {
138 if (startTime.tv_sec == 0)
139 gettimeofday(&startTime, 0);
140 gettimeofday(&tv, 0);
141 sec = tv.tv_sec - startTime.tv_sec;
142 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
147 vsprintf(buf, format, ap);
149 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
158 empth_init(void **ctx_ptr, int flags)
161 struct sigaction act;
164 pthread_key_create(&ctx_key, NULL);
165 pthread_mutex_init(&mtx_ctxsw, NULL);
168 sigemptyset(&act.sa_mask);
169 act.sa_handler = empth_alarm;
170 sigaction(SIGALRM, &act, NULL);
173 ctx = malloc(sizeof(empth_t));
175 logerror("pthread init failed: not enough memory");
179 ctx->desc = "empire main";
182 ctx->id = pthread_self();
184 pthread_setspecific(ctx_key, ctx);
185 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)", name,
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", name, desc,
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", name, desc,
238 empth_status("new thread id is %ld", (long)t);
242 pthread_attr_destroy(&attr);
249 empth_restorectx(void)
253 ctx_ptr = pthread_getspecific(ctx_key);
254 *udata = ctx_ptr->ud;
255 if (ctx_ptr->state == EMPTH_KILLED) {
256 empth_status("i am dead");
259 empth_status("context restored");
265 return pthread_getspecific(ctx_key);
273 pthread_mutex_unlock(&mtx_ctxsw);
274 empth_status("empth_exit");
275 ctx_ptr = pthread_getspecific(ctx_key);
276 /* We want to leave the main thread around forever, until it's time
277 for it to die for real (in a shutdown) */
278 if (!strcmp(ctx_ptr->name, "Main")) {
291 pthread_mutex_unlock(&mtx_ctxsw);
292 pthread_mutex_lock(&mtx_ctxsw);
297 empth_terminate(empth_t *a)
299 empth_status("killing thread %s", a->name);
300 a->state = EMPTH_KILLED;
301 pthread_kill(a->id, SIGALRM);
305 empth_select(int fd, int flags)
313 pthread_mutex_unlock(&mtx_ctxsw);
314 empth_status("%s select on %d",
315 flags == EMPTH_FD_READ ? "read" : "write", fd);
325 FD_SET(fd, &readmask);
328 FD_SET(fd, &writemask);
331 logerror("bad flag %d passed to empth_select", flags);
335 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
338 if (errno == EINTR) {
339 /* go handle the signal */
340 empth_status("select broken by signal");
344 /* strange but we dont get EINTR on select broken by signal */
345 empth_status("select failed (%s)", strerror(errno));
350 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
351 empth_status("input ready");
354 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
355 empth_status("output ready");
361 pthread_mutex_lock(&mtx_ctxsw);
370 struct sigaction act;
371 empth_status("got alarm signal");
373 act.sa_flags &= ~SA_RESTART;
375 sigemptyset(&act.sa_mask);
376 act.sa_handler = empth_alarm;
377 sigaction(SIGALRM, &act, NULL);
381 empth_wakeup(empth_t *a)
383 empth_status("waking up thread %s", a->name);
384 pthread_kill(a->id, SIGALRM);
385 empth_status("waiting for it to run");
389 empth_sleep(time_t until)
393 empth_status("going to sleep %ld sec", until - time(0));
394 pthread_mutex_unlock(&mtx_ctxsw);
395 tv.tv_sec = until - time(NULL);
398 select(0, NULL, NULL, NULL, &tv);
399 } while ((tv.tv_sec = until - time(NULL)) > 0);
400 empth_status("sleep done. Waiting for lock");
401 pthread_mutex_lock(&mtx_ctxsw);
407 empth_sem_create(char *name, int cnt)
411 sm = malloc(sizeof(empth_sem_t));
413 logerror("out of memory at %s:%d", __FILE__, __LINE__);
416 strncpy(sm->name, name, sizeof(sm->name) - 1);
418 pthread_mutex_init(&sm->mtx_update, NULL);
419 pthread_mutex_init(&sm->mtx_sem, NULL);
420 pthread_cond_init(&sm->cnd_sem, NULL);
425 empth_sem_signal(empth_sem_t *sm)
427 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
428 pthread_mutex_lock(&sm->mtx_update);
429 if (sm->count++ < 0) {
430 pthread_mutex_unlock(&sm->mtx_update);
431 pthread_mutex_lock(&sm->mtx_sem);
432 pthread_cond_signal(&sm->cnd_sem);
433 pthread_mutex_unlock(&sm->mtx_sem);
435 pthread_mutex_unlock(&sm->mtx_update);
439 empth_sem_wait(empth_sem_t *sm)
441 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
442 pthread_mutex_lock(&sm->mtx_update);
443 if (--sm->count < 0) {
444 pthread_mutex_unlock(&sm->mtx_update);
445 empth_status("blocking");
446 pthread_mutex_unlock(&mtx_ctxsw);
447 pthread_mutex_lock(&sm->mtx_sem);
448 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
449 empth_status("waking up");
450 pthread_mutex_unlock(&sm->mtx_sem);
451 pthread_mutex_lock(&mtx_ctxsw);
454 pthread_mutex_unlock(&sm->mtx_update);