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>
47 #include "empthread.h"
48 #include "prototypes.h"
53 static pthread_key_t ctx_key;
54 static int empth_flags;
55 static char **udata; /* pointer to out global context */
57 static pthread_mutex_t mtx_ctxsw; /* thread in critical section */
61 empth_start(void *ctx)
65 /* actually it should inherit all this from main but... */
67 act.sa_flags = SA_SIGINFO;
69 sigemptyset(&act.sa_mask);
70 act.sa_handler = shutdwn;
71 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
72 a Linux box running POSIX threads -- STM */
73 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
74 sigaction(SIGUSR1, &act, NULL);
76 sigaction(SIGTERM, &act, NULL);
77 sigaction(SIGINT, &act, NULL);
78 act.sa_handler = panic;
79 sigaction(SIGBUS, &act, NULL);
80 sigaction(SIGSEGV, &act, NULL);
81 sigaction(SIGILL, &act, NULL);
82 sigaction(SIGFPE, &act, NULL);
83 act.sa_handler = SIG_IGN;
84 sigaction(SIGPIPE, &act, NULL);
86 act.sa_handler = empth_alarm;
87 sigaction(SIGALRM, &act, NULL);
89 ((empth_t *)ctx)->id = pthread_self();
90 pthread_setspecific(ctx_key, ctx);
91 pthread_mutex_lock(&mtx_ctxsw);
92 *udata = ((empth_t *)ctx)->ud;
93 ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
99 empth_status(char *format, ...)
102 static struct timeval startTime;
108 va_start(ap, format);
109 if (empth_flags & EMPTH_PRINT) {
110 if (startTime.tv_sec == 0)
111 gettimeofday(&startTime, 0);
112 gettimeofday(&tv, 0);
113 sec = tv.tv_sec - startTime.tv_sec;
114 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
119 vsprintf(buf, format, ap);
121 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
130 empth_init(char **ctx_ptr, int flags)
133 struct sigaction act;
136 pthread_key_create(&ctx_key, 0);
138 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
140 pthread_mutex_init(&mtx_ctxsw, 0);
144 sigemptyset(&act.sa_mask);
145 act.sa_handler = empth_alarm;
146 sigaction(SIGALRM, &act, NULL);
149 ctx = (empth_t *)malloc(sizeof(empth_t));
151 logerror("pthread init failed: not enough memory");
155 ctx->desc = "empire main";
158 ctx->id = pthread_self();
160 pthread_setspecific(ctx_key, ctx);
161 pthread_mutex_lock(&mtx_ctxsw);
163 logerror("pthreads initialized");
169 * prio can be used for setting scheeduling policy but...
170 * it seems to be optional in POSIX threads and Solaris
171 * for example just ignores it.
172 * More then that priority is not needed even in lwp threads.
175 empth_create(int prio, void (*entry)(void *), int size, int flags,
176 char *name, char *desc, void *ud)
183 empth_status("creating new thread %s:%s", name, desc);
185 ctx = (empth_t *)malloc(sizeof(empth_t));
187 logerror("not enough memoty to create thread: %s (%s)", name,
191 ctx->name = strdup(name);
192 ctx->desc = strdup(desc);
198 eno = pthread_attr_init(&attr) ? errno : 0;
200 eno = pthread_attr_init(&attr);
203 logerror("can not create thread attribute %s (%s): %s", name, desc,
207 #if defined(__linux__)
208 /* Linux doesn't let you adjust the stack */
209 #elif defined(_DECTHREADS_)
210 /* DEC does not have PTHREAD_STACK_MIN constant */
211 /* Do not go below default size */
212 if (size > pthread_attr_getstacksize(attr))
213 pthread_attr_setstacksize(&attr, size);
215 if (size < PTHREAD_STACK_MIN)
216 size = PTHREAD_STACK_MIN + 1;
218 pthread_attr_setstacksize(&attr, size);
221 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
224 eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
226 eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
229 logerror("can not create thread: %s (%s): %s", name, desc,
233 empth_status("new thread id is %d", t);
235 pthread_attr_destroy(&attr);
237 pthread_attr_destroy(&attr);
245 empth_setctx(void *ct)
250 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
252 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
255 *udata = ((empth_t *)ctx_ptr)->ud;
256 pthread_setspecific(ctx_key, (void *)ctx_ptr);
257 empth_status("context saved");
262 empth_restorectx(void)
267 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
269 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
271 *udata = (char *)ctx_ptr->ud;
272 if (ctx_ptr->state == EMPTH_KILLED) {
273 empth_status("i am dead");
276 empth_status("context restored");
285 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
288 return (empth_t *)pthread_getspecific(ctx_key);
297 pthread_mutex_unlock(&mtx_ctxsw);
298 empth_status("empth_exit");
300 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
302 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
304 /* We want to leave the main thread around forever, until it's time
305 for it to die for real (in a shutdown) */
306 if (!strcmp(ctx_ptr->name, "Main")) {
322 pthread_mutex_unlock(&mtx_ctxsw);
323 sleep(10); /* take a nap pthread_yield(); */
324 pthread_mutex_lock(&mtx_ctxsw);
329 empth_terminate(empth_t *a)
331 /* logerror("calling non supported function empth_terminate: %s:%d",
332 __FILE__, __LINE__); */
333 empth_status("killing thread %s", a->name);
334 a->state = EMPTH_KILLED;
336 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
337 #if (!defined __ppc__)
338 pthread_kill(a->id, SIGALRM);
345 empth_select(int fd, int flags)
353 pthread_mutex_unlock(&mtx_ctxsw);
354 empth_status("%s select on %d",
355 flags == EMPTH_FD_READ ? "read" : "write", fd);
365 FD_SET(fd, &readmask);
368 FD_SET(fd, &writemask);
371 logerror("bad flag %d passed to empth_select", flags);
375 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
378 if (errno == EINTR) {
379 /* go handle the signal */
380 empth_status("select broken by signal");
384 /* strange but we dont get EINTR on select broken by signal */
385 empth_status("select failed (%s)", strerror(errno));
390 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
391 empth_status("input ready");
394 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
395 empth_status("output ready");
401 pthread_mutex_lock(&mtx_ctxsw);
410 struct sigaction act;
411 empth_status("got alarm signal");
413 act.sa_flags &= ~SA_RESTART;
415 sigemptyset(&act.sa_mask);
416 act.sa_handler = empth_alarm;
417 sigaction(SIGALRM, &act, NULL);
421 empth_wakeup(empth_t *a)
423 empth_status("waking up thread %s", a->name);
425 #if (!defined __ppc__)
426 pthread_kill(a->id, SIGALRM);
429 empth_status("waiting for it to run");
434 empth_sleep(long until)
438 empth_status("going to sleep %ld sec", until - time(0));
439 pthread_mutex_unlock(&mtx_ctxsw);
440 tv.tv_sec = until - time(NULL);
443 select(0, NULL, NULL, NULL, &tv);
444 } while ((tv.tv_sec = until - time(NULL)) > 0);
445 empth_status("sleep done. Waiting for lock");
446 pthread_mutex_lock(&mtx_ctxsw);
452 empth_sem_create(char *name, int cnt)
456 sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
458 logerror("out of memory at %s:%d", __FILE__, __LINE__);
461 strncpy(sm->name, name, sizeof(sm->name) - 1);
464 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
465 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
466 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
468 pthread_mutex_init(&sm->mtx_update, 0);
469 pthread_mutex_init(&sm->mtx_sem, 0);
470 pthread_cond_init(&sm->cnd_sem, 0);
476 empth_sem_signal(empth_sem_t *sm)
478 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
479 pthread_mutex_lock(&sm->mtx_update);
480 if (sm->count++ < 0) {
481 pthread_mutex_unlock(&sm->mtx_update);
482 pthread_mutex_lock(&sm->mtx_sem);
483 pthread_cond_signal(&sm->cnd_sem);
484 pthread_mutex_unlock(&sm->mtx_sem);
486 pthread_mutex_unlock(&sm->mtx_update);
490 empth_sem_wait(empth_sem_t *sm)
492 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
493 pthread_mutex_lock(&sm->mtx_update);
494 if (--sm->count < 0) {
495 pthread_mutex_unlock(&sm->mtx_update);
496 empth_status("blocking");
497 pthread_mutex_unlock(&mtx_ctxsw);
498 pthread_mutex_lock(&sm->mtx_sem);
499 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
500 empth_status("waking up");
501 pthread_mutex_unlock(&sm->mtx_sem);
502 pthread_mutex_lock(&mtx_ctxsw);
505 pthread_mutex_unlock(&sm->mtx_update);