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 empth_start(void *ctx)
64 /* actually it should inherit all this from main but... */
66 act.sa_flags = SA_SIGINFO;
68 sigemptyset(&act.sa_mask);
69 act.sa_handler = shutdwn;
70 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
71 a Linux box running POSIX threads -- STM */
72 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
73 sigaction(SIGUSR1, &act, NULL);
75 sigaction(SIGTERM, &act, NULL);
76 sigaction(SIGINT, &act, NULL);
77 act.sa_handler = panic;
78 sigaction(SIGBUS, &act, NULL);
79 sigaction(SIGSEGV, &act, NULL);
80 sigaction(SIGILL, &act, NULL);
81 sigaction(SIGFPE, &act, NULL);
82 act.sa_handler = SIG_IGN;
83 sigaction(SIGPIPE, &act, NULL);
85 act.sa_handler = empth_alarm;
86 sigaction(SIGALRM, &act, NULL);
88 ((empth_t *)ctx)->id = pthread_self();
89 pthread_setspecific(ctx_key, ctx);
90 pthread_mutex_lock(&mtx_ctxsw);
91 *udata = ((empth_t *)ctx)->ud;
92 ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
98 empth_status(char *format, ...)
101 static struct timeval startTime;
107 va_start(ap, format);
108 if (empth_flags & EMPTH_PRINT) {
109 if (startTime.tv_sec == 0)
110 gettimeofday(&startTime, 0);
111 gettimeofday(&tv, 0);
112 sec = tv.tv_sec - startTime.tv_sec;
113 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
118 vsprintf(buf, format, ap);
120 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
129 empth_init(char **ctx_ptr, int flags)
132 struct sigaction act;
135 pthread_key_create(&ctx_key, 0);
137 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
139 pthread_mutex_init(&mtx_ctxsw, 0);
143 sigemptyset(&act.sa_mask);
144 act.sa_handler = empth_alarm;
145 sigaction(SIGALRM, &act, NULL);
148 ctx = (empth_t *)malloc(sizeof(empth_t));
150 logerror("pthread init failed: not enough memory");
154 ctx->desc = "empire main";
157 ctx->id = pthread_self();
159 pthread_setspecific(ctx_key, ctx);
160 pthread_mutex_lock(&mtx_ctxsw);
162 logerror("pthreads initialized");
168 * prio can be used for setting scheeduling policy but...
169 * it seems to be optional in POSIX threads and Solaris
170 * for example just ignores it.
171 * More then that priority is not needed even in lwp threads.
174 empth_create(int prio, void (*entry)(void *), int size, int flags,
175 char *name, char *desc, void *ud)
182 empth_status("creating new thread %s:%s", name, desc);
184 ctx = (empth_t *)malloc(sizeof(empth_t));
186 logerror("not enough memoty to create thread: %s (%s)", name,
190 ctx->name = strdup(name);
191 ctx->desc = strdup(desc);
197 eno = pthread_attr_init(&attr) ? errno : 0;
199 eno = pthread_attr_init(&attr);
202 logerror("can not create thread attribute %s (%s): %s", name, desc,
206 #if defined(__linux__)
207 /* Linux doesn't let you adjust the stack */
208 #elif defined(_DECTHREADS_)
209 /* DEC does not have PTHREAD_STACK_MIN constant */
210 /* Do not go below default size */
211 if (size > pthread_attr_getstacksize(attr))
212 pthread_attr_setstacksize(&attr, size);
214 if (size < PTHREAD_STACK_MIN)
215 size = PTHREAD_STACK_MIN + 1;
217 pthread_attr_setstacksize(&attr, size);
220 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
223 eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
225 eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
228 logerror("can not create thread: %s (%s): %s", name, desc,
232 empth_status("new thread id is %d", t);
234 pthread_attr_destroy(&attr);
236 pthread_attr_destroy(&attr);
244 empth_setctx(void *ct)
249 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
251 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
254 *udata = ((empth_t *)ctx_ptr)->ud;
255 pthread_setspecific(ctx_key, (void *)ctx_ptr);
256 empth_status("context saved");
261 empth_restorectx(void)
266 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
268 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
270 *udata = (char *)ctx_ptr->ud;
271 if (ctx_ptr->state == EMPTH_KILLED) {
272 empth_status("i am dead");
275 empth_status("context restored");
284 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
287 return (empth_t *)pthread_getspecific(ctx_key);
296 pthread_mutex_unlock(&mtx_ctxsw);
297 empth_status("empth_exit");
299 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
301 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
303 /* We want to leave the main thread around forever, until it's time
304 for it to die for real (in a shutdown) */
305 if (!strcmp(ctx_ptr->name, "Main")) {
321 pthread_mutex_unlock(&mtx_ctxsw);
322 sleep(10); /* take a nap pthread_yield(); */
323 pthread_mutex_lock(&mtx_ctxsw);
328 empth_terminate(empth_t *a)
330 /* logerror("calling non supported function empth_terminate: %s:%d",
331 __FILE__, __LINE__); */
332 empth_status("killing thread %s", a->name);
333 a->state = EMPTH_KILLED;
335 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
336 #if (!defined __ppc__)
337 pthread_kill(a->id, SIGALRM);
344 empth_select(int fd, int flags)
352 pthread_mutex_unlock(&mtx_ctxsw);
353 empth_status("%s select on %d",
354 flags == EMPTH_FD_READ ? "read" : "write", fd);
364 FD_SET(fd, &readmask);
367 FD_SET(fd, &writemask);
370 logerror("bad flag %d passed to empth_select", flags);
374 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
377 if (errno == EINTR) {
378 /* go handle the signal */
379 empth_status("select broken by signal");
383 /* strange but we dont get EINTR on select broken by signal */
384 empth_status("select failed (%s)", strerror(errno));
389 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
390 empth_status("input ready");
393 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
394 empth_status("output ready");
400 pthread_mutex_lock(&mtx_ctxsw);
409 struct sigaction act;
410 empth_status("got alarm signal");
412 act.sa_flags &= ~SA_RESTART;
414 sigemptyset(&act.sa_mask);
415 act.sa_handler = empth_alarm;
416 sigaction(SIGALRM, &act, NULL);
420 empth_wakeup(empth_t *a)
422 empth_status("waking up thread %s", a->name);
424 #if (!defined __ppc__)
425 pthread_kill(a->id, SIGALRM);
428 empth_status("waiting for it to run");
433 empth_sleep(long until)
437 empth_status("going to sleep %ld sec", until - time(0));
438 pthread_mutex_unlock(&mtx_ctxsw);
439 tv.tv_sec = until - time(NULL);
442 select(0, NULL, NULL, NULL, &tv);
443 } while ((tv.tv_sec = until - time(NULL)) > 0);
444 empth_status("sleep done. Waiting for lock");
445 pthread_mutex_lock(&mtx_ctxsw);
451 empth_sem_create(char *name, int cnt)
455 sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
457 logerror("out of memory at %s:%d", __FILE__, __LINE__);
460 strncpy(sm->name, name, sizeof(sm->name) - 1);
463 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
464 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
465 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
467 pthread_mutex_init(&sm->mtx_update, 0);
468 pthread_mutex_init(&sm->mtx_sem, 0);
469 pthread_cond_init(&sm->cnd_sem, 0);
475 empth_sem_signal(empth_sem_t *sm)
477 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
478 pthread_mutex_lock(&sm->mtx_update);
479 if (sm->count++ < 0) {
480 pthread_mutex_unlock(&sm->mtx_update);
481 pthread_mutex_lock(&sm->mtx_sem);
482 pthread_cond_signal(&sm->cnd_sem);
483 pthread_mutex_unlock(&sm->mtx_sem);
485 pthread_mutex_unlock(&sm->mtx_update);
489 empth_sem_wait(empth_sem_t *sm)
491 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
492 pthread_mutex_lock(&sm->mtx_update);
493 if (--sm->count < 0) {
494 pthread_mutex_unlock(&sm->mtx_update);
495 empth_status("blocking");
496 pthread_mutex_unlock(&mtx_ctxsw);
497 pthread_mutex_lock(&sm->mtx_sem);
498 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
499 empth_status("waking up");
500 pthread_mutex_unlock(&sm->mtx_sem);
501 pthread_mutex_lock(&mtx_ctxsw);
504 pthread_mutex_unlock(&sm->mtx_update);