2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 void **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... */
68 sigemptyset(&act.sa_mask);
69 act.sa_handler = shutdwn;
70 sigaction(SIGTERM, &act, NULL);
71 sigaction(SIGINT, &act, NULL);
72 act.sa_handler = panic;
73 sigaction(SIGBUS, &act, NULL);
74 sigaction(SIGSEGV, &act, NULL);
75 sigaction(SIGILL, &act, NULL);
76 sigaction(SIGFPE, &act, NULL);
77 act.sa_handler = SIG_IGN;
78 sigaction(SIGPIPE, &act, NULL);
80 act.sa_handler = empth_alarm;
81 sigaction(SIGALRM, &act, NULL);
83 ((empth_t *)ctx)->id = pthread_self();
84 pthread_setspecific(ctx_key, ctx);
85 pthread_mutex_lock(&mtx_ctxsw);
86 *udata = ((empth_t *)ctx)->ud;
87 ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
93 empth_status(char *format, ...)
96 static struct timeval startTime;
102 va_start(ap, format);
103 if (empth_flags & EMPTH_PRINT) {
104 if (startTime.tv_sec == 0)
105 gettimeofday(&startTime, 0);
106 gettimeofday(&tv, 0);
107 sec = tv.tv_sec - startTime.tv_sec;
108 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
113 vsprintf(buf, format, ap);
115 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
124 empth_init(void **ctx_ptr, int flags)
127 struct sigaction act;
130 pthread_key_create(&ctx_key, 0);
132 pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
134 pthread_mutex_init(&mtx_ctxsw, 0);
138 sigemptyset(&act.sa_mask);
139 act.sa_handler = empth_alarm;
140 sigaction(SIGALRM, &act, NULL);
143 ctx = malloc(sizeof(empth_t));
145 logerror("pthread init failed: not enough memory");
149 ctx->desc = "empire main";
152 ctx->id = pthread_self();
154 pthread_setspecific(ctx_key, ctx);
155 pthread_mutex_lock(&mtx_ctxsw);
157 logerror("pthreads initialized");
163 * prio can be used for setting scheeduling policy but...
164 * it seems to be optional in POSIX threads and Solaris
165 * for example just ignores it.
166 * More then that priority is not needed even in lwp threads.
169 empth_create(int prio, void (*entry)(void *), int size, int flags,
170 char *name, char *desc, void *ud)
177 empth_status("creating new thread %s:%s", name, desc);
179 ctx = malloc(sizeof(empth_t));
181 logerror("not enough memoty to create thread: %s (%s)", name,
185 ctx->name = strdup(name);
186 ctx->desc = strdup(desc);
192 eno = pthread_attr_init(&attr) ? errno : 0;
194 eno = pthread_attr_init(&attr);
197 logerror("can not create thread attribute %s (%s): %s", name, desc,
201 #if defined(__linux__)
202 /* Linux doesn't let you adjust the stack */
203 #elif defined(_DECTHREADS_)
204 /* DEC does not have PTHREAD_STACK_MIN constant */
205 /* Do not go below default size */
206 if (size > pthread_attr_getstacksize(attr))
207 pthread_attr_setstacksize(&attr, size);
209 if (size < PTHREAD_STACK_MIN)
210 size = PTHREAD_STACK_MIN + 1;
212 pthread_attr_setstacksize(&attr, size);
215 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
218 eno = pthread_create(&t, attr, empth_start, ctx) ? errno : 0;
220 eno = pthread_create(&t, &attr, empth_start, ctx);
223 logerror("can not create thread: %s (%s): %s", name, desc,
227 empth_status("new thread id is %ld", (long)t);
229 pthread_attr_destroy(&attr);
231 pthread_attr_destroy(&attr);
239 empth_setctx(void *ct)
244 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
246 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
249 *udata = ctx_ptr->ud;
250 pthread_setspecific(ctx_key, ctx_ptr);
251 empth_status("context saved");
256 empth_restorectx(void)
261 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
263 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
265 *udata = ctx_ptr->ud;
266 if (ctx_ptr->state == EMPTH_KILLED) {
267 empth_status("i am dead");
270 empth_status("context restored");
279 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
282 return (empth_t *)pthread_getspecific(ctx_key);
291 pthread_mutex_unlock(&mtx_ctxsw);
292 empth_status("empth_exit");
294 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
296 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
298 /* We want to leave the main thread around forever, until it's time
299 for it to die for real (in a shutdown) */
300 if (!strcmp(ctx_ptr->name, "Main")) {
316 pthread_mutex_unlock(&mtx_ctxsw);
317 sleep(10); /* take a nap pthread_yield(); */
318 pthread_mutex_lock(&mtx_ctxsw);
323 empth_terminate(empth_t *a)
325 /* logerror("calling non supported function empth_terminate: %s:%d",
326 __FILE__, __LINE__); */
327 empth_status("killing thread %s", a->name);
328 a->state = EMPTH_KILLED;
330 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
331 #if (!defined __ppc__)
332 pthread_kill(a->id, SIGALRM);
339 empth_select(int fd, int flags)
347 pthread_mutex_unlock(&mtx_ctxsw);
348 empth_status("%s select on %d",
349 flags == EMPTH_FD_READ ? "read" : "write", fd);
359 FD_SET(fd, &readmask);
362 FD_SET(fd, &writemask);
365 logerror("bad flag %d passed to empth_select", flags);
369 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
372 if (errno == EINTR) {
373 /* go handle the signal */
374 empth_status("select broken by signal");
378 /* strange but we dont get EINTR on select broken by signal */
379 empth_status("select failed (%s)", strerror(errno));
384 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
385 empth_status("input ready");
388 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
389 empth_status("output ready");
395 pthread_mutex_lock(&mtx_ctxsw);
404 struct sigaction act;
405 empth_status("got alarm signal");
407 act.sa_flags &= ~SA_RESTART;
409 sigemptyset(&act.sa_mask);
410 act.sa_handler = empth_alarm;
411 sigaction(SIGALRM, &act, NULL);
415 empth_wakeup(empth_t *a)
417 empth_status("waking up thread %s", a->name);
419 #if (!defined __ppc__)
420 pthread_kill(a->id, SIGALRM);
423 empth_status("waiting for it to run");
428 empth_sleep(time_t until)
432 empth_status("going to sleep %ld sec", until - time(0));
433 pthread_mutex_unlock(&mtx_ctxsw);
434 tv.tv_sec = until - time(NULL);
437 select(0, NULL, NULL, NULL, &tv);
438 } while ((tv.tv_sec = until - time(NULL)) > 0);
439 empth_status("sleep done. Waiting for lock");
440 pthread_mutex_lock(&mtx_ctxsw);
446 empth_sem_create(char *name, int cnt)
450 sm = malloc(sizeof(empth_sem_t));
452 logerror("out of memory at %s:%d", __FILE__, __LINE__);
455 strncpy(sm->name, name, sizeof(sm->name) - 1);
458 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
459 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
460 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
462 pthread_mutex_init(&sm->mtx_update, 0);
463 pthread_mutex_init(&sm->mtx_sem, 0);
464 pthread_cond_init(&sm->cnd_sem, 0);
470 empth_sem_signal(empth_sem_t *sm)
472 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
473 pthread_mutex_lock(&sm->mtx_update);
474 if (sm->count++ < 0) {
475 pthread_mutex_unlock(&sm->mtx_update);
476 pthread_mutex_lock(&sm->mtx_sem);
477 pthread_cond_signal(&sm->cnd_sem);
478 pthread_mutex_unlock(&sm->mtx_sem);
480 pthread_mutex_unlock(&sm->mtx_update);
484 empth_sem_wait(empth_sem_t *sm)
486 empth_status("wait 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 empth_status("blocking");
491 pthread_mutex_unlock(&mtx_ctxsw);
492 pthread_mutex_lock(&sm->mtx_sem);
493 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
494 empth_status("waking up");
495 pthread_mutex_unlock(&sm->mtx_sem);
496 pthread_mutex_lock(&mtx_ctxsw);
499 pthread_mutex_unlock(&sm->mtx_update);