2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * pthread.c: Interface from Empire threads to POSIX threads
30 * Known contributors to this file:
40 #include <sys/types.h>
46 #include "empthread.h"
47 #include "prototypes.h"
52 static pthread_key_t ctx_key;
53 static int empth_flags;
54 static char **udata; /* pointer to out global context */
56 static pthread_mutex_t mtx_ctxsw; /* thread in critical section */
60 static void empth_setctx _PROTO((void *));
62 static void empth_restorectx _PROTO(());
65 empth_start(void *ctx)
69 /* actually it should inherit all this from main but... */
71 act.sa_flags = SA_SIGINFO;
73 sigemptyset(&act.sa_mask);
74 act.sa_handler = shutdwn;
75 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
76 a Linux box running POSIX threads -- STM */
77 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
78 sigaction(SIGUSR1, &act, NULL);
80 sigaction(SIGTERM, &act, NULL);
81 sigaction(SIGINT, &act, NULL);
82 act.sa_handler = panic;
83 sigaction(SIGBUS, &act, NULL);
84 sigaction(SIGSEGV, &act, NULL);
85 sigaction(SIGILL, &act, NULL);
86 sigaction(SIGFPE, &act, NULL);
87 act.sa_handler = SIG_IGN;
88 sigaction(SIGPIPE, &act, NULL);
90 act.sa_handler = empth_alarm;
91 sigaction(SIGALRM, &act, NULL);
93 ((empth_t *)ctx)->id = pthread_self();
94 pthread_setspecific(ctx_key, ctx);
95 pthread_mutex_lock(&mtx_ctxsw);
96 *udata = ((empth_t *)ctx)->ud;
97 ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
103 empth_status(char *format, ...)
106 static struct timeval startTime;
112 va_start(ap, format);
113 if (empth_flags & EMPTH_PRINT) {
114 if (startTime.tv_sec == 0)
115 gettimeofday(&startTime, 0);
116 gettimeofday(&tv, 0);
117 sec = tv.tv_sec - startTime.tv_sec;
118 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
123 vsprintf(buf, format, ap);
125 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
134 empth_init(char **ctx_ptr, int flags)
137 struct sigaction act;
140 pthread_key_create(&ctx_key, 0);
142 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
144 pthread_mutex_init(&mtx_ctxsw, 0);
148 sigemptyset(&act.sa_mask);
149 act.sa_handler = empth_alarm;
150 sigaction(SIGALRM, &act, NULL);
153 ctx = (empth_t *)malloc(sizeof(empth_t));
155 logerror("pthread init failed: not enough memory");
159 ctx->desc = "empire main";
162 ctx->id = pthread_self();
164 pthread_setspecific(ctx_key, ctx);
165 pthread_mutex_lock(&mtx_ctxsw);
167 logerror("pthreads initialized");
173 * prio can be used for setting scheeduling policy but...
174 * it seems to be optional in POSIX threads and Solaris
175 * for example just ignores it.
176 * More then that priority is not needed even in lwp threads.
179 empth_create(int prio, void (*entry) (), int size, int flags,
180 char *name, char *desc, void *ud)
187 empth_status("creating new thread %s:%s", name, desc);
189 ctx = (empth_t *)malloc(sizeof(empth_t));
191 logerror("not enough memoty to create thread: %s (%s)", name,
195 ctx->name = strdup(name);
196 ctx->desc = strdup(desc);
202 eno = pthread_attr_init(&attr) ? errno : 0;
204 eno = pthread_attr_init(&attr);
207 logerror("can not create thread attribute %s (%s): %s", name, desc,
211 #if defined(__linux__)
212 /* Linux doesn't let you adjust the stack */
213 #elif defined(_DECTHREADS_)
214 /* DEC does not have PTHREAD_STACK_MIN constant */
215 /* Do not go below default size */
216 if (size > pthread_attr_getstacksize(attr))
217 pthread_attr_setstacksize(&attr, size);
219 if (size < PTHREAD_STACK_MIN)
220 size = PTHREAD_STACK_MIN + 1;
222 pthread_attr_setstacksize(&attr, size);
225 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
228 eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
230 eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
233 logerror("can not create thread: %s (%s): %s", name, desc,
237 empth_status("new thread id is %d", t);
239 pthread_attr_destroy(&attr);
241 pthread_attr_destroy(&attr);
249 empth_setctx(void *ct)
254 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
256 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
259 *udata = ((empth_t *)ctx_ptr)->ud;
260 pthread_setspecific(ctx_key, (void *)ctx_ptr);
261 empth_status("context saved");
266 empth_restorectx(void)
271 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
273 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
275 *udata = (char *)ctx_ptr->ud;
276 if (ctx_ptr->state == EMPTH_KILLED) {
277 empth_status("i am dead");
280 empth_status("context restored");
289 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
292 return (empth_t *)pthread_getspecific(ctx_key);
301 pthread_mutex_unlock(&mtx_ctxsw);
302 empth_status("empth_exit");
304 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
306 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
308 /* We want to leave the main thread around forever, until it's time
309 for it to die for real (in a shutdown) */
310 if (!strcmp(ctx_ptr->name, "Main")) {
326 pthread_mutex_unlock(&mtx_ctxsw);
327 sleep(10); /* take a nap pthread_yield(); */
328 pthread_mutex_lock(&mtx_ctxsw);
333 empth_terminate(empth_t *a)
335 /* logerror("calling non supported function empth_terminate: %s:%d",
336 __FILE__, __LINE__); */
337 empth_status("killing thread %s", a->name);
338 a->state = EMPTH_KILLED;
340 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
341 #if (!defined __ppc__)
342 pthread_kill(a->id, SIGALRM);
349 empth_select(int fd, int flags)
357 pthread_mutex_unlock(&mtx_ctxsw);
358 empth_status("%s select on %d",
359 flags == EMPTH_FD_READ ? "read" : "write", fd);
369 FD_SET(fd, &readmask);
372 FD_SET(fd, &writemask);
375 logerror("bad flag %d passed to empth_select", flags);
379 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
382 if (errno == EINTR) {
383 /* go handle the signal */
384 empth_status("select broken by signal");
388 /* strange but we dont get EINTR on select broken by signal */
389 empth_status("select failed (%s)", strerror(errno));
394 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
395 empth_status("input ready");
398 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
399 empth_status("output ready");
405 pthread_mutex_lock(&mtx_ctxsw);
414 struct sigaction act;
415 empth_status("got alarm signal");
417 act.sa_flags &= ~SA_RESTART;
419 sigemptyset(&act.sa_mask);
420 act.sa_handler = empth_alarm;
421 sigaction(SIGALRM, &act, NULL);
425 empth_wakeup(empth_t *a)
427 empth_status("waking up thread %s", a->name);
429 #if (!defined __ppc__)
430 pthread_kill(a->id, SIGALRM);
433 empth_status("waiting for it to run");
438 empth_sleep(long until)
442 empth_status("going to sleep %ld sec", until - time(0));
443 pthread_mutex_unlock(&mtx_ctxsw);
444 tv.tv_sec = until - time(NULL);
447 select(0, NULL, NULL, NULL, &tv);
448 } while ((tv.tv_sec = until - time(NULL)) > 0);
449 empth_status("sleep done. Waiting for lock");
450 pthread_mutex_lock(&mtx_ctxsw);
456 empth_sem_create(char *name, int cnt)
460 sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
462 logerror("out of memory at %s:%d", __FILE__, __LINE__);
465 strncpy(sm->name, name, sizeof(sm->name) - 1);
468 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
469 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
470 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
472 pthread_mutex_init(&sm->mtx_update, 0);
473 pthread_mutex_init(&sm->mtx_sem, 0);
474 pthread_cond_init(&sm->cnd_sem, 0);
480 empth_sem_signal(empth_sem_t *sm)
482 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
483 pthread_mutex_lock(&sm->mtx_update);
484 if (sm->count++ < 0) {
485 pthread_mutex_unlock(&sm->mtx_update);
486 pthread_mutex_lock(&sm->mtx_sem);
487 pthread_cond_signal(&sm->cnd_sem);
488 pthread_mutex_unlock(&sm->mtx_sem);
490 pthread_mutex_unlock(&sm->mtx_update);
494 empth_sem_wait(empth_sem_t *sm)
496 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
497 pthread_mutex_lock(&sm->mtx_update);
498 if (--sm->count < 0) {
499 pthread_mutex_unlock(&sm->mtx_update);
500 empth_status("blocking");
501 pthread_mutex_unlock(&mtx_ctxsw);
502 pthread_mutex_lock(&sm->mtx_sem);
503 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
504 empth_status("waking up");
505 pthread_mutex_unlock(&sm->mtx_sem);
506 pthread_mutex_lock(&mtx_ctxsw);
509 pthread_mutex_unlock(&sm->mtx_update);