2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * pthread.c: Interface from Empire threads to POSIX threads
30 * Known contributors to this file:
33 * Markus Armbruster, 2005-2006
36 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
37 #define _XOPEN_SOURCE 500
48 #include <sys/types.h>
55 #include "empthread.h"
57 #define EMPTH_KILLED 1
60 char *name; /* thread name */
61 char *desc; /* description */
62 void *ud; /* user data */
63 int state; /* my state */
64 void (*ep)(void *); /* entry point */
65 pthread_t id; /* thread id */
69 pthread_mutex_t mtx_update; /* use it to update count */
72 pthread_mutex_t mtx_sem;
73 pthread_cond_t cnd_sem;
76 /* Thread-specific data key */
77 static pthread_key_t ctx_key;
79 /* Flags that were passed to empth_init() */
80 static int empth_flags;
82 /* Pointer to thread context variable */
86 * Non-preemption mutex.
87 * Empire code outside this file is only executed while holding this
88 * mutex. This makes sure Empire code is never preempted by Empire
91 static pthread_mutex_t mtx_ctxsw;
93 static void empth_status(char *format, ...)
94 ATTRIBUTE((format (printf, 1, 2)));
95 static void empth_alarm(int sig);
98 empth_start(void *arg)
102 ctx->id = pthread_self();
103 pthread_setspecific(ctx_key, ctx);
104 pthread_mutex_lock(&mtx_ctxsw);
112 empth_status(char *format, ...)
115 static struct timeval startTime;
121 va_start(ap, format);
122 if (empth_flags & EMPTH_PRINT) {
123 if (startTime.tv_sec == 0)
124 gettimeofday(&startTime, 0);
125 gettimeofday(&tv, 0);
126 sec = tv.tv_sec - startTime.tv_sec;
127 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
132 vsprintf(buf, format, ap);
134 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
143 empth_init(void **ctx_ptr, int flags)
147 struct sigaction act;
152 empth_init_signals();
154 sigaddset(&set, SIGHUP);
155 sigaddset(&set, SIGINT);
156 sigaddset(&set, SIGTERM);
157 pthread_sigmask(SIG_BLOCK, &set, NULL);
159 sigemptyset(&act.sa_mask);
160 act.sa_handler = empth_alarm;
161 sigaction(SIGALRM, &act, NULL);
163 pthread_key_create(&ctx_key, NULL);
164 pthread_mutex_init(&mtx_ctxsw, NULL);
166 ctx = malloc(sizeof(empth_t));
168 logerror("pthread init failed: not enough memory");
172 ctx->desc = "empire main";
175 ctx->id = pthread_self();
177 pthread_setspecific(ctx_key, ctx);
178 pthread_mutex_lock(&mtx_ctxsw);
179 logerror("pthreads initialized");
185 * prio can be used for setting scheeduling policy but...
186 * it seems to be optional in POSIX threads and Solaris
187 * for example just ignores it.
188 * More then that priority is not needed even in lwp threads.
191 empth_create(int prio, void (*entry)(void *), int size, int flags,
192 char *name, char *desc, void *ud)
199 empth_status("creating new thread %s:%s", name, desc);
201 ctx = malloc(sizeof(empth_t));
203 logerror("not enough memory to create thread: %s (%s)",
207 ctx->name = strdup(name);
208 ctx->desc = strdup(desc);
213 eno = pthread_attr_init(&attr);
215 logerror("can not create thread attribute %s (%s): %s",
216 name, desc, strerror(eno));
219 if (size < PTHREAD_STACK_MIN)
220 size = PTHREAD_STACK_MIN;
221 pthread_attr_setstacksize(&attr, size);
222 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
224 eno = pthread_create(&t, &attr, empth_start, ctx);
226 logerror("can not create thread: %s (%s): %s",
227 name, desc, strerror(eno));
230 empth_status("new thread id is %ld", (long)t);
235 pthread_attr_destroy(&attr);
242 empth_restorectx(void)
246 ctx_ptr = 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");
258 return pthread_getspecific(ctx_key);
264 empth_status("empth_exit");
265 pthread_mutex_unlock(&mtx_ctxsw);
266 free(pthread_getspecific(ctx_key));
273 pthread_mutex_unlock(&mtx_ctxsw);
274 pthread_mutex_lock(&mtx_ctxsw);
279 empth_terminate(empth_t *a)
281 empth_status("killing thread %s", a->name);
282 a->state = EMPTH_KILLED;
283 pthread_kill(a->id, SIGALRM);
287 empth_select(int fd, int flags)
295 pthread_mutex_unlock(&mtx_ctxsw);
296 empth_status("%s select on %d",
297 flags == EMPTH_FD_READ ? "read" : "write", fd);
307 FD_SET(fd, &readmask);
310 FD_SET(fd, &writemask);
313 logerror("bad flag %d passed to empth_select", flags);
317 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
320 if (errno == EINTR) {
321 /* go handle the signal */
322 empth_status("select broken by signal");
326 /* strange but we dont get EINTR on select broken by signal */
327 empth_status("select failed (%s)", strerror(errno));
332 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
333 empth_status("input ready");
336 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
337 empth_status("output ready");
343 pthread_mutex_lock(&mtx_ctxsw);
351 * Nothing to do --- we handle this signal just to let
352 * empth_wakeup() interrupt system calls.
354 empth_status("got alarm signal");
358 empth_wakeup(empth_t *a)
360 empth_status("waking up thread %s", a->name);
361 pthread_kill(a->id, SIGALRM);
365 empth_sleep(time_t until)
369 empth_status("going to sleep %ld sec", until - time(0));
370 pthread_mutex_unlock(&mtx_ctxsw);
371 tv.tv_sec = until - time(NULL);
374 select(0, NULL, NULL, NULL, &tv);
375 } while ((tv.tv_sec = until - time(NULL)) > 0);
376 empth_status("sleep done. Waiting for lock");
377 pthread_mutex_lock(&mtx_ctxsw);
382 empth_wait_for_signal(void)
388 sigaddset(&set, SIGHUP);
389 sigaddset(&set, SIGINT);
390 sigaddset(&set, SIGTERM);
391 pthread_mutex_unlock(&mtx_ctxsw);
393 empth_status("waiting for signals");
394 err = sigwait(&set, &sig);
395 if (CANT_HAPPEN(err)) {
399 empth_status("got awaited signal %d", sig);
400 pthread_mutex_lock(&mtx_ctxsw);
407 empth_sem_create(char *name, int cnt)
411 sm = malloc(sizeof(empth_sem_t));
413 logerror("out of memory at %s:%d", __FILE__, __LINE__);
416 strncpy(sm->name, name, sizeof(sm->name) - 1);
418 pthread_mutex_init(&sm->mtx_update, NULL);
419 pthread_mutex_init(&sm->mtx_sem, NULL);
420 pthread_cond_init(&sm->cnd_sem, NULL);
425 empth_sem_signal(empth_sem_t *sm)
427 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
428 pthread_mutex_lock(&sm->mtx_update);
429 if (sm->count++ < 0) {
430 pthread_mutex_unlock(&sm->mtx_update);
431 pthread_mutex_lock(&sm->mtx_sem);
432 pthread_cond_signal(&sm->cnd_sem);
433 pthread_mutex_unlock(&sm->mtx_sem);
435 pthread_mutex_unlock(&sm->mtx_update);
439 empth_sem_wait(empth_sem_t *sm)
441 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
442 pthread_mutex_lock(&sm->mtx_update);
443 if (--sm->count < 0) {
444 pthread_mutex_unlock(&sm->mtx_update);
445 empth_status("blocking");
446 pthread_mutex_unlock(&mtx_ctxsw);
447 pthread_mutex_lock(&sm->mtx_sem);
448 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
449 empth_status("waking up");
450 pthread_mutex_unlock(&sm->mtx_sem);
451 pthread_mutex_lock(&mtx_ctxsw);
454 pthread_mutex_unlock(&sm->mtx_update);