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 memory 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);
238 empth_restorectx(void)
243 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
245 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
247 *udata = ctx_ptr->ud;
248 if (ctx_ptr->state == EMPTH_KILLED) {
249 empth_status("i am dead");
252 empth_status("context restored");
261 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
264 return (empth_t *)pthread_getspecific(ctx_key);
273 pthread_mutex_unlock(&mtx_ctxsw);
274 empth_status("empth_exit");
276 pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
278 ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
280 /* We want to leave the main thread around forever, until it's time
281 for it to die for real (in a shutdown) */
282 if (!strcmp(ctx_ptr->name, "Main")) {
298 pthread_mutex_unlock(&mtx_ctxsw);
299 sleep(10); /* take a nap pthread_yield(); */
300 pthread_mutex_lock(&mtx_ctxsw);
305 empth_terminate(empth_t *a)
307 /* logerror("calling non supported function empth_terminate: %s:%d",
308 __FILE__, __LINE__); */
309 empth_status("killing thread %s", a->name);
310 a->state = EMPTH_KILLED;
312 /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
313 #if (!defined __ppc__)
314 pthread_kill(a->id, SIGALRM);
321 empth_select(int fd, int flags)
329 pthread_mutex_unlock(&mtx_ctxsw);
330 empth_status("%s select on %d",
331 flags == EMPTH_FD_READ ? "read" : "write", fd);
341 FD_SET(fd, &readmask);
344 FD_SET(fd, &writemask);
347 logerror("bad flag %d passed to empth_select", flags);
351 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
354 if (errno == EINTR) {
355 /* go handle the signal */
356 empth_status("select broken by signal");
360 /* strange but we dont get EINTR on select broken by signal */
361 empth_status("select failed (%s)", strerror(errno));
366 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
367 empth_status("input ready");
370 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
371 empth_status("output ready");
377 pthread_mutex_lock(&mtx_ctxsw);
386 struct sigaction act;
387 empth_status("got alarm signal");
389 act.sa_flags &= ~SA_RESTART;
391 sigemptyset(&act.sa_mask);
392 act.sa_handler = empth_alarm;
393 sigaction(SIGALRM, &act, NULL);
397 empth_wakeup(empth_t *a)
399 empth_status("waking up thread %s", a->name);
401 #if (!defined __ppc__)
402 pthread_kill(a->id, SIGALRM);
405 empth_status("waiting for it to run");
410 empth_sleep(time_t until)
414 empth_status("going to sleep %ld sec", until - time(0));
415 pthread_mutex_unlock(&mtx_ctxsw);
416 tv.tv_sec = until - time(NULL);
419 select(0, NULL, NULL, NULL, &tv);
420 } while ((tv.tv_sec = until - time(NULL)) > 0);
421 empth_status("sleep done. Waiting for lock");
422 pthread_mutex_lock(&mtx_ctxsw);
428 empth_sem_create(char *name, int cnt)
432 sm = malloc(sizeof(empth_sem_t));
434 logerror("out of memory at %s:%d", __FILE__, __LINE__);
437 strncpy(sm->name, name, sizeof(sm->name) - 1);
440 pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
441 pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
442 pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
444 pthread_mutex_init(&sm->mtx_update, 0);
445 pthread_mutex_init(&sm->mtx_sem, 0);
446 pthread_cond_init(&sm->cnd_sem, 0);
452 empth_sem_signal(empth_sem_t *sm)
454 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
455 pthread_mutex_lock(&sm->mtx_update);
456 if (sm->count++ < 0) {
457 pthread_mutex_unlock(&sm->mtx_update);
458 pthread_mutex_lock(&sm->mtx_sem);
459 pthread_cond_signal(&sm->cnd_sem);
460 pthread_mutex_unlock(&sm->mtx_sem);
462 pthread_mutex_unlock(&sm->mtx_update);
466 empth_sem_wait(empth_sem_t *sm)
468 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
469 pthread_mutex_lock(&sm->mtx_update);
470 if (--sm->count < 0) {
471 pthread_mutex_unlock(&sm->mtx_update);
472 empth_status("blocking");
473 pthread_mutex_unlock(&mtx_ctxsw);
474 pthread_mutex_lock(&sm->mtx_sem);
475 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
476 empth_status("waking up");
477 pthread_mutex_unlock(&sm->mtx_sem);
478 pthread_mutex_lock(&mtx_ctxsw);
481 pthread_mutex_unlock(&sm->mtx_update);