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,
138 empth_init (char **ctx_ptr, int flags)
141 struct sigaction act;
144 pthread_key_create(&ctx_key, 0);
146 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
148 pthread_mutex_init(&mtx_ctxsw, 0);
152 sigemptyset (&act.sa_mask);
153 act.sa_handler = empth_alarm;
154 sigaction(SIGALRM, &act, NULL);
157 ctx = (empth_t *)malloc(sizeof(empth_t));
159 logerror("pthread init failed: not enough memory");
163 ctx->desc = "empire main";
166 ctx->id = pthread_self();
168 pthread_setspecific(ctx_key, ctx);
169 pthread_mutex_lock(&mtx_ctxsw);
171 logerror("pthreads initialized");
177 * prio can be used for setting scheeduling policy but...
178 * it seems to be optional in POSIX threads and Solaris
179 * for example just ignores it.
180 * More then that priority is not needed even in lwp threads.
183 empth_create (int prio, void (*entry)(), int size, int flags,
184 char *name, char *desc, void *ud)
191 empth_status("creating new thread %s:%s", name, desc);
193 ctx = (empth_t *)malloc(sizeof(empth_t));
195 logerror("not enough memoty to create thread: %s (%s)", name, desc);
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,
215 #if defined(__linux__)
216 /* Linux doesn't let you adjust the stack */
217 #elif defined(_DECTHREADS_)
218 /* DEC does not have PTHREAD_STACK_MIN constant */
219 /* Do not go below default size */
220 if(size > pthread_attr_getstacksize(attr))
221 pthread_attr_setstacksize(&attr, size);
223 if(size < PTHREAD_STACK_MIN)
224 size = PTHREAD_STACK_MIN + 1;
226 pthread_attr_setstacksize(&attr, size);
229 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
232 eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
234 eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
237 logerror("can not create thread: %s (%s): %s", name, desc,
241 empth_status("new thread id is %d", t);
243 pthread_attr_destroy(&attr);
245 pthread_attr_destroy(&attr);
253 empth_setctx(void *ct)
258 pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
260 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
263 *udata = ((empth_t *)ctx_ptr)->ud;
264 pthread_setspecific(ctx_key, (void *)ctx_ptr);
265 empth_status("context saved");
270 empth_restorectx(void)
275 pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
277 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
279 *udata = (char *)ctx_ptr->ud;
280 if (ctx_ptr->state == EMPTH_KILLED) {
281 empth_status("i am dead");
284 empth_status("context restored");
293 pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
296 return (empth_t *)pthread_getspecific(ctx_key);
305 pthread_mutex_unlock(&mtx_ctxsw);
306 empth_status("empth_exit");
308 pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
310 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
312 /* We want to leave the main thread around forever, until it's time
313 for it to die for real (in a shutdown) */
314 if (!strcmp(ctx_ptr->name, "Main")) {
330 pthread_mutex_unlock(&mtx_ctxsw);
331 sleep(10); /* take a nap pthread_yield(); */
332 pthread_mutex_lock(&mtx_ctxsw);
337 empth_terminate(empth_t *a)
339 /* logerror("calling non supported function empth_terminate: %s:%d",
340 __FILE__, __LINE__); */
341 empth_status("killing thread %s", a->name);
342 a->state = EMPTH_KILLED;
344 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
345 #if (!defined __ppc__)
346 pthread_kill(a->id, SIGALRM);
353 empth_select(int fd, int flags)
361 pthread_mutex_unlock(&mtx_ctxsw);
362 empth_status("%s select on %d",
363 flags == EMPTH_FD_READ ? "read" : "write", fd );
373 FD_SET(fd, &readmask);
376 FD_SET(fd, &writemask);
379 logerror("bad flag %d passed to empth_select", flags);
383 n = select(fd + 1, &readmask, &writemask, (fd_set *)0, &tv);
386 if (errno == EINTR) {
387 /* go handle the signal */
388 empth_status("select broken by signal");
392 /* strange but we dont get EINTR on select broken by signal */
393 empth_status("select failed (%s)", strerror(errno));
398 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
399 empth_status("input ready");
402 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
403 empth_status("output ready");
409 pthread_mutex_lock(&mtx_ctxsw);
418 struct sigaction act;
419 empth_status("got alarm signal");
421 act.sa_flags &= ~SA_RESTART;
423 sigemptyset (&act.sa_mask);
424 act.sa_handler = empth_alarm;
425 sigaction(SIGALRM, &act, NULL);
429 empth_wakeup(empth_t *a)
431 empth_status("waking up thread %s", a->name);
433 #if (!defined __ppc__)
434 pthread_kill(a->id, SIGALRM);
437 empth_status("waiting for it to run");
442 empth_sleep(long until)
446 empth_status("going to sleep %ld sec", until - time(0));
447 pthread_mutex_unlock(&mtx_ctxsw);
448 tv.tv_sec = until - time(NULL);
451 select (0, NULL, NULL, NULL, &tv);
452 } while ((tv.tv_sec = until - time(NULL)) > 0);
453 empth_status("sleep done. Waiting for lock");
454 pthread_mutex_lock(&mtx_ctxsw);
460 empth_sem_create(char *name, int cnt)
464 sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
466 logerror("out of memory at %s:%d", __FILE__, __LINE__);
469 strncpy(sm->name, name, sizeof(sm->name)-1);
472 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
473 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
474 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
476 pthread_mutex_init(&sm->mtx_update, 0);
477 pthread_mutex_init(&sm->mtx_sem, 0);
478 pthread_cond_init(&sm->cnd_sem, 0);
484 empth_sem_signal(empth_sem_t *sm)
486 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
487 pthread_mutex_lock(&sm->mtx_update);
488 if(sm->count++ < 0) {
489 pthread_mutex_unlock(&sm->mtx_update);
490 pthread_mutex_lock(&sm->mtx_sem);
491 pthread_cond_signal(&sm->cnd_sem);
492 pthread_mutex_unlock(&sm->mtx_sem);
495 pthread_mutex_unlock(&sm->mtx_update);
499 empth_sem_wait (empth_sem_t *sm)
501 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
502 pthread_mutex_lock(&sm->mtx_update);
503 if(--sm->count < 0) {
504 pthread_mutex_unlock(&sm->mtx_update);
505 empth_status("blocking");
506 pthread_mutex_unlock(&mtx_ctxsw);
507 pthread_mutex_lock(&sm->mtx_sem);
508 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
509 empth_status("waking up");
510 pthread_mutex_unlock(&sm->mtx_sem);
511 pthread_mutex_lock(&mtx_ctxsw);
515 pthread_mutex_unlock(&sm->mtx_update);