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);
131 pthread_mutex_init(&mtx_ctxsw, 0);
134 sigemptyset(&act.sa_mask);
135 act.sa_handler = empth_alarm;
136 sigaction(SIGALRM, &act, NULL);
139 ctx = malloc(sizeof(empth_t));
141 logerror("pthread init failed: not enough memory");
145 ctx->desc = "empire main";
148 ctx->id = pthread_self();
150 pthread_setspecific(ctx_key, ctx);
151 pthread_mutex_lock(&mtx_ctxsw);
153 logerror("pthreads initialized");
159 * prio can be used for setting scheeduling policy but...
160 * it seems to be optional in POSIX threads and Solaris
161 * for example just ignores it.
162 * More then that priority is not needed even in lwp threads.
165 empth_create(int prio, void (*entry)(void *), int size, int flags,
166 char *name, char *desc, void *ud)
173 empth_status("creating new thread %s:%s", name, desc);
175 ctx = malloc(sizeof(empth_t));
177 logerror("not enough memory to create thread: %s (%s)", name,
181 ctx->name = strdup(name);
182 ctx->desc = strdup(desc);
187 eno = pthread_attr_init(&attr);
189 logerror("can not create thread attribute %s (%s): %s", name, desc,
193 if (size < PTHREAD_STACK_MIN)
194 size = PTHREAD_STACK_MIN + 1;
195 pthread_attr_setstacksize(&attr, size);
196 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
198 eno = pthread_create(&t, &attr, empth_start, ctx);
200 logerror("can not create thread: %s (%s): %s", name, desc,
204 empth_status("new thread id is %ld", (long)t);
206 pthread_attr_destroy(&attr);
208 pthread_attr_destroy(&attr);
215 empth_restorectx(void)
219 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
220 *udata = ctx_ptr->ud;
221 if (ctx_ptr->state == EMPTH_KILLED) {
222 empth_status("i am dead");
225 empth_status("context restored");
231 return (empth_t *)pthread_getspecific(ctx_key);
239 pthread_mutex_unlock(&mtx_ctxsw);
240 empth_status("empth_exit");
241 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
242 /* We want to leave the main thread around forever, until it's time
243 for it to die for real (in a shutdown) */
244 if (!strcmp(ctx_ptr->name, "Main")) {
257 pthread_mutex_unlock(&mtx_ctxsw);
258 sleep(10); /* take a nap pthread_yield(); */
259 pthread_mutex_lock(&mtx_ctxsw);
264 empth_terminate(empth_t *a)
266 /* logerror("calling non supported function empth_terminate: %s:%d",
267 __FILE__, __LINE__); */
268 empth_status("killing thread %s", a->name);
269 a->state = EMPTH_KILLED;
270 pthread_kill(a->id, SIGALRM);
275 empth_select(int fd, int flags)
283 pthread_mutex_unlock(&mtx_ctxsw);
284 empth_status("%s select on %d",
285 flags == EMPTH_FD_READ ? "read" : "write", fd);
295 FD_SET(fd, &readmask);
298 FD_SET(fd, &writemask);
301 logerror("bad flag %d passed to empth_select", flags);
305 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
308 if (errno == EINTR) {
309 /* go handle the signal */
310 empth_status("select broken by signal");
314 /* strange but we dont get EINTR on select broken by signal */
315 empth_status("select failed (%s)", strerror(errno));
320 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
321 empth_status("input ready");
324 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
325 empth_status("output ready");
331 pthread_mutex_lock(&mtx_ctxsw);
340 struct sigaction act;
341 empth_status("got alarm signal");
343 act.sa_flags &= ~SA_RESTART;
345 sigemptyset(&act.sa_mask);
346 act.sa_handler = empth_alarm;
347 sigaction(SIGALRM, &act, NULL);
351 empth_wakeup(empth_t *a)
353 empth_status("waking up thread %s", a->name);
354 pthread_kill(a->id, SIGALRM);
355 empth_status("waiting for it to run");
360 empth_sleep(time_t until)
364 empth_status("going to sleep %ld sec", until - time(0));
365 pthread_mutex_unlock(&mtx_ctxsw);
366 tv.tv_sec = until - time(NULL);
369 select(0, NULL, NULL, NULL, &tv);
370 } while ((tv.tv_sec = until - time(NULL)) > 0);
371 empth_status("sleep done. Waiting for lock");
372 pthread_mutex_lock(&mtx_ctxsw);
378 empth_sem_create(char *name, int cnt)
382 sm = malloc(sizeof(empth_sem_t));
384 logerror("out of memory at %s:%d", __FILE__, __LINE__);
387 strncpy(sm->name, name, sizeof(sm->name) - 1);
389 pthread_mutex_init(&sm->mtx_update, 0);
390 pthread_mutex_init(&sm->mtx_sem, 0);
391 pthread_cond_init(&sm->cnd_sem, 0);
396 empth_sem_signal(empth_sem_t *sm)
398 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
399 pthread_mutex_lock(&sm->mtx_update);
400 if (sm->count++ < 0) {
401 pthread_mutex_unlock(&sm->mtx_update);
402 pthread_mutex_lock(&sm->mtx_sem);
403 pthread_cond_signal(&sm->cnd_sem);
404 pthread_mutex_unlock(&sm->mtx_sem);
406 pthread_mutex_unlock(&sm->mtx_update);
410 empth_sem_wait(empth_sem_t *sm)
412 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
413 pthread_mutex_lock(&sm->mtx_update);
414 if (--sm->count < 0) {
415 pthread_mutex_unlock(&sm->mtx_update);
416 empth_status("blocking");
417 pthread_mutex_unlock(&mtx_ctxsw);
418 pthread_mutex_lock(&sm->mtx_sem);
419 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
420 empth_status("waking up");
421 pthread_mutex_unlock(&sm->mtx_sem);
422 pthread_mutex_lock(&mtx_ctxsw);
425 pthread_mutex_unlock(&sm->mtx_update);