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 */
59 static void empth_status(char *format, ...) ATTRIBUTE((format (printf, 1, 2)));
63 empth_start(void *ctx)
67 /* actually it should inherit all this from main but... */
69 act.sa_flags = SA_SIGINFO;
71 sigemptyset(&act.sa_mask);
72 act.sa_handler = shutdwn;
73 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
74 a Linux box running POSIX threads -- STM */
75 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
76 sigaction(SIGUSR1, &act, NULL);
78 sigaction(SIGTERM, &act, NULL);
79 sigaction(SIGINT, &act, NULL);
80 act.sa_handler = panic;
81 sigaction(SIGBUS, &act, NULL);
82 sigaction(SIGSEGV, &act, NULL);
83 sigaction(SIGILL, &act, NULL);
84 sigaction(SIGFPE, &act, NULL);
85 act.sa_handler = SIG_IGN;
86 sigaction(SIGPIPE, &act, NULL);
88 act.sa_handler = empth_alarm;
89 sigaction(SIGALRM, &act, NULL);
91 ((empth_t *)ctx)->id = pthread_self();
92 pthread_setspecific(ctx_key, ctx);
93 pthread_mutex_lock(&mtx_ctxsw);
94 *udata = ((empth_t *)ctx)->ud;
95 ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
101 empth_status(char *format, ...)
104 static struct timeval startTime;
110 va_start(ap, format);
111 if (empth_flags & EMPTH_PRINT) {
112 if (startTime.tv_sec == 0)
113 gettimeofday(&startTime, 0);
114 gettimeofday(&tv, 0);
115 sec = tv.tv_sec - startTime.tv_sec;
116 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
121 vsprintf(buf, format, ap);
123 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
132 empth_init(char **ctx_ptr, int flags)
135 struct sigaction act;
138 pthread_key_create(&ctx_key, 0);
140 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
142 pthread_mutex_init(&mtx_ctxsw, 0);
146 sigemptyset(&act.sa_mask);
147 act.sa_handler = empth_alarm;
148 sigaction(SIGALRM, &act, NULL);
151 ctx = (empth_t *)malloc(sizeof(empth_t));
153 logerror("pthread init failed: not enough memory");
157 ctx->desc = "empire main";
160 ctx->id = pthread_self();
162 pthread_setspecific(ctx_key, ctx);
163 pthread_mutex_lock(&mtx_ctxsw);
165 logerror("pthreads initialized");
171 * prio can be used for setting scheeduling policy but...
172 * it seems to be optional in POSIX threads and Solaris
173 * for example just ignores it.
174 * More then that priority is not needed even in lwp threads.
177 empth_create(int prio, void (*entry)(void *), int size, int flags,
178 char *name, char *desc, void *ud)
185 empth_status("creating new thread %s:%s", name, desc);
187 ctx = (empth_t *)malloc(sizeof(empth_t));
189 logerror("not enough memoty to create thread: %s (%s)", name,
193 ctx->name = strdup(name);
194 ctx->desc = strdup(desc);
200 eno = pthread_attr_init(&attr) ? errno : 0;
202 eno = pthread_attr_init(&attr);
205 logerror("can not create thread attribute %s (%s): %s", name, desc,
209 #if defined(__linux__)
210 /* Linux doesn't let you adjust the stack */
211 #elif defined(_DECTHREADS_)
212 /* DEC does not have PTHREAD_STACK_MIN constant */
213 /* Do not go below default size */
214 if (size > pthread_attr_getstacksize(attr))
215 pthread_attr_setstacksize(&attr, size);
217 if (size < PTHREAD_STACK_MIN)
218 size = PTHREAD_STACK_MIN + 1;
220 pthread_attr_setstacksize(&attr, size);
223 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
226 eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
228 eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
231 logerror("can not create thread: %s (%s): %s", name, desc,
235 empth_status("new thread id is %ld", (long)t);
237 pthread_attr_destroy(&attr);
239 pthread_attr_destroy(&attr);
247 empth_setctx(void *ct)
252 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
254 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
257 *udata = ((empth_t *)ctx_ptr)->ud;
258 pthread_setspecific(ctx_key, (void *)ctx_ptr);
259 empth_status("context saved");
264 empth_restorectx(void)
269 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
271 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
273 *udata = (char *)ctx_ptr->ud;
274 if (ctx_ptr->state == EMPTH_KILLED) {
275 empth_status("i am dead");
278 empth_status("context restored");
287 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
290 return (empth_t *)pthread_getspecific(ctx_key);
299 pthread_mutex_unlock(&mtx_ctxsw);
300 empth_status("empth_exit");
302 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
304 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
306 /* We want to leave the main thread around forever, until it's time
307 for it to die for real (in a shutdown) */
308 if (!strcmp(ctx_ptr->name, "Main")) {
324 pthread_mutex_unlock(&mtx_ctxsw);
325 sleep(10); /* take a nap pthread_yield(); */
326 pthread_mutex_lock(&mtx_ctxsw);
331 empth_terminate(empth_t *a)
333 /* logerror("calling non supported function empth_terminate: %s:%d",
334 __FILE__, __LINE__); */
335 empth_status("killing thread %s", a->name);
336 a->state = EMPTH_KILLED;
338 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
339 #if (!defined __ppc__)
340 pthread_kill(a->id, SIGALRM);
347 empth_select(int fd, int flags)
355 pthread_mutex_unlock(&mtx_ctxsw);
356 empth_status("%s select on %d",
357 flags == EMPTH_FD_READ ? "read" : "write", fd);
367 FD_SET(fd, &readmask);
370 FD_SET(fd, &writemask);
373 logerror("bad flag %d passed to empth_select", flags);
377 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
380 if (errno == EINTR) {
381 /* go handle the signal */
382 empth_status("select broken by signal");
386 /* strange but we dont get EINTR on select broken by signal */
387 empth_status("select failed (%s)", strerror(errno));
392 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
393 empth_status("input ready");
396 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
397 empth_status("output ready");
403 pthread_mutex_lock(&mtx_ctxsw);
412 struct sigaction act;
413 empth_status("got alarm signal");
415 act.sa_flags &= ~SA_RESTART;
417 sigemptyset(&act.sa_mask);
418 act.sa_handler = empth_alarm;
419 sigaction(SIGALRM, &act, NULL);
423 empth_wakeup(empth_t *a)
425 empth_status("waking up thread %s", a->name);
427 #if (!defined __ppc__)
428 pthread_kill(a->id, SIGALRM);
431 empth_status("waiting for it to run");
436 empth_sleep(long until)
440 empth_status("going to sleep %ld sec", until - time(0));
441 pthread_mutex_unlock(&mtx_ctxsw);
442 tv.tv_sec = until - time(NULL);
445 select(0, NULL, NULL, NULL, &tv);
446 } while ((tv.tv_sec = until - time(NULL)) > 0);
447 empth_status("sleep done. Waiting for lock");
448 pthread_mutex_lock(&mtx_ctxsw);
454 empth_sem_create(char *name, int cnt)
458 sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
460 logerror("out of memory at %s:%d", __FILE__, __LINE__);
463 strncpy(sm->name, name, sizeof(sm->name) - 1);
466 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
467 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
468 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
470 pthread_mutex_init(&sm->mtx_update, 0);
471 pthread_mutex_init(&sm->mtx_sem, 0);
472 pthread_cond_init(&sm->cnd_sem, 0);
478 empth_sem_signal(empth_sem_t *sm)
480 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
481 pthread_mutex_lock(&sm->mtx_update);
482 if (sm->count++ < 0) {
483 pthread_mutex_unlock(&sm->mtx_update);
484 pthread_mutex_lock(&sm->mtx_sem);
485 pthread_cond_signal(&sm->cnd_sem);
486 pthread_mutex_unlock(&sm->mtx_sem);
488 pthread_mutex_unlock(&sm->mtx_update);
492 empth_sem_wait(empth_sem_t *sm)
494 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
495 pthread_mutex_lock(&sm->mtx_update);
496 if (--sm->count < 0) {
497 pthread_mutex_unlock(&sm->mtx_update);
498 empth_status("blocking");
499 pthread_mutex_unlock(&mtx_ctxsw);
500 pthread_mutex_lock(&sm->mtx_sem);
501 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
502 empth_status("waking up");
503 pthread_mutex_unlock(&sm->mtx_sem);
504 pthread_mutex_lock(&mtx_ctxsw);
507 pthread_mutex_unlock(&sm->mtx_update);