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)
68 extern emp_sig_t panic();
69 extern emp_sig_t shutdwn();
72 /* actually it should inherit all this from main but... */
74 act.sa_flags = SA_SIGINFO;
76 sigemptyset(&act.sa_mask);
77 act.sa_handler = shutdwn;
78 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
79 a Linux box running POSIX threads -- STM */
80 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
81 sigaction(SIGUSR1, &act, NULL);
83 sigaction(SIGTERM, &act, NULL);
84 sigaction(SIGINT, &act, NULL);
85 act.sa_handler = panic;
86 sigaction(SIGBUS, &act, NULL);
87 sigaction(SIGSEGV, &act, NULL);
88 sigaction(SIGILL, &act, NULL);
89 sigaction(SIGFPE, &act, NULL);
90 act.sa_handler = SIG_IGN;
91 sigaction(SIGPIPE, &act, NULL);
93 act.sa_handler = empth_alarm;
94 sigaction(SIGALRM, &act, NULL);
96 ((empth_t *)ctx)->id = pthread_self();
97 pthread_setspecific(ctx_key, ctx);
98 pthread_mutex_lock(&mtx_ctxsw);
99 *udata = ((empth_t *)ctx)->ud;
100 ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
106 empth_status(char *format, ...)
109 static struct timeval startTime;
115 va_start(ap, format);
116 if (empth_flags & EMPTH_PRINT) {
117 if (startTime.tv_sec == 0)
118 gettimeofday(&startTime, 0);
119 gettimeofday(&tv, 0);
120 sec = tv.tv_sec - startTime.tv_sec;
121 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
126 vsprintf(buf, format, ap);
128 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
137 empth_init(char **ctx_ptr, int flags)
140 struct sigaction act;
143 pthread_key_create(&ctx_key, 0);
145 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
147 pthread_mutex_init(&mtx_ctxsw, 0);
151 sigemptyset(&act.sa_mask);
152 act.sa_handler = empth_alarm;
153 sigaction(SIGALRM, &act, NULL);
156 ctx = (empth_t *)malloc(sizeof(empth_t));
158 logerror("pthread init failed: not enough memory");
162 ctx->desc = "empire main";
165 ctx->id = pthread_self();
167 pthread_setspecific(ctx_key, ctx);
168 pthread_mutex_lock(&mtx_ctxsw);
170 logerror("pthreads initialized");
176 * prio can be used for setting scheeduling policy but...
177 * it seems to be optional in POSIX threads and Solaris
178 * for example just ignores it.
179 * More then that priority is not needed even in lwp threads.
182 empth_create(int prio, void (*entry) (), int size, int flags,
183 char *name, char *desc, void *ud)
190 empth_status("creating new thread %s:%s", name, desc);
192 ctx = (empth_t *)malloc(sizeof(empth_t));
194 logerror("not enough memoty to create thread: %s (%s)", name,
198 ctx->name = strdup(name);
199 ctx->desc = strdup(desc);
205 eno = pthread_attr_init(&attr) ? errno : 0;
207 eno = pthread_attr_init(&attr);
210 logerror("can not create thread attribute %s (%s): %s", name, desc,
214 #if defined(__linux__)
215 /* Linux doesn't let you adjust the stack */
216 #elif defined(_DECTHREADS_)
217 /* DEC does not have PTHREAD_STACK_MIN constant */
218 /* Do not go below default size */
219 if (size > pthread_attr_getstacksize(attr))
220 pthread_attr_setstacksize(&attr, size);
222 if (size < PTHREAD_STACK_MIN)
223 size = PTHREAD_STACK_MIN + 1;
225 pthread_attr_setstacksize(&attr, size);
228 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
231 eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
233 eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
236 logerror("can not create thread: %s (%s): %s", name, desc,
240 empth_status("new thread id is %d", t);
242 pthread_attr_destroy(&attr);
244 pthread_attr_destroy(&attr);
252 empth_setctx(void *ct)
257 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
259 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
262 *udata = ((empth_t *)ctx_ptr)->ud;
263 pthread_setspecific(ctx_key, (void *)ctx_ptr);
264 empth_status("context saved");
269 empth_restorectx(void)
274 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
276 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
278 *udata = (char *)ctx_ptr->ud;
279 if (ctx_ptr->state == EMPTH_KILLED) {
280 empth_status("i am dead");
283 empth_status("context restored");
292 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
295 return (empth_t *)pthread_getspecific(ctx_key);
304 pthread_mutex_unlock(&mtx_ctxsw);
305 empth_status("empth_exit");
307 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
309 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
311 /* We want to leave the main thread around forever, until it's time
312 for it to die for real (in a shutdown) */
313 if (!strcmp(ctx_ptr->name, "Main")) {
329 pthread_mutex_unlock(&mtx_ctxsw);
330 sleep(10); /* take a nap pthread_yield(); */
331 pthread_mutex_lock(&mtx_ctxsw);
336 empth_terminate(empth_t *a)
338 /* logerror("calling non supported function empth_terminate: %s:%d",
339 __FILE__, __LINE__); */
340 empth_status("killing thread %s", a->name);
341 a->state = EMPTH_KILLED;
343 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
344 #if (!defined __ppc__)
345 pthread_kill(a->id, SIGALRM);
352 empth_select(int fd, int flags)
360 pthread_mutex_unlock(&mtx_ctxsw);
361 empth_status("%s select on %d",
362 flags == EMPTH_FD_READ ? "read" : "write", fd);
372 FD_SET(fd, &readmask);
375 FD_SET(fd, &writemask);
378 logerror("bad flag %d passed to empth_select", flags);
382 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
385 if (errno == EINTR) {
386 /* go handle the signal */
387 empth_status("select broken by signal");
391 /* strange but we dont get EINTR on select broken by signal */
392 empth_status("select failed (%s)", strerror(errno));
397 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
398 empth_status("input ready");
401 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
402 empth_status("output ready");
408 pthread_mutex_lock(&mtx_ctxsw);
417 struct sigaction act;
418 empth_status("got alarm signal");
420 act.sa_flags &= ~SA_RESTART;
422 sigemptyset(&act.sa_mask);
423 act.sa_handler = empth_alarm;
424 sigaction(SIGALRM, &act, NULL);
428 empth_wakeup(empth_t *a)
430 empth_status("waking up thread %s", a->name);
432 #if (!defined __ppc__)
433 pthread_kill(a->id, SIGALRM);
436 empth_status("waiting for it to run");
441 empth_sleep(long until)
445 empth_status("going to sleep %ld sec", until - time(0));
446 pthread_mutex_unlock(&mtx_ctxsw);
447 tv.tv_sec = until - time(NULL);
450 select(0, NULL, NULL, NULL, &tv);
451 } while ((tv.tv_sec = until - time(NULL)) > 0);
452 empth_status("sleep done. Waiting for lock");
453 pthread_mutex_lock(&mtx_ctxsw);
459 empth_sem_create(char *name, int cnt)
463 sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
465 logerror("out of memory at %s:%d", __FILE__, __LINE__);
468 strncpy(sm->name, name, sizeof(sm->name) - 1);
471 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
472 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
473 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
475 pthread_mutex_init(&sm->mtx_update, 0);
476 pthread_mutex_init(&sm->mtx_sem, 0);
477 pthread_cond_init(&sm->cnd_sem, 0);
483 empth_sem_signal(empth_sem_t *sm)
485 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
486 pthread_mutex_lock(&sm->mtx_update);
487 if (sm->count++ < 0) {
488 pthread_mutex_unlock(&sm->mtx_update);
489 pthread_mutex_lock(&sm->mtx_sem);
490 pthread_cond_signal(&sm->cnd_sem);
491 pthread_mutex_unlock(&sm->mtx_sem);
493 pthread_mutex_unlock(&sm->mtx_update);
497 empth_sem_wait(empth_sem_t *sm)
499 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
500 pthread_mutex_lock(&sm->mtx_update);
501 if (--sm->count < 0) {
502 pthread_mutex_unlock(&sm->mtx_update);
503 empth_status("blocking");
504 pthread_mutex_unlock(&mtx_ctxsw);
505 pthread_mutex_lock(&sm->mtx_sem);
506 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
507 empth_status("waking up");
508 pthread_mutex_unlock(&sm->mtx_sem);
509 pthread_mutex_lock(&mtx_ctxsw);
512 pthread_mutex_unlock(&sm->mtx_update);