2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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
49 #include <sys/types.h>
56 #include "empthread.h"
57 #include "prototypes.h"
59 #define EMPTH_KILLED 1
62 char *name; /* thread name */
63 char *desc; /* description */
64 void *ud; /* user data */
65 int state; /* my state */
66 void (*ep)(void *); /* entry point */
67 pthread_t id; /* thread id */
71 pthread_mutex_t mtx_update; /* use it to update count */
74 pthread_mutex_t mtx_sem;
75 pthread_cond_t cnd_sem;
78 /* Thread-specific data key */
79 static pthread_key_t ctx_key;
81 /* Flags that were passed to empth_init() */
82 static int empth_flags;
84 /* Pointer to thread context variable */
88 * Non-preemption mutex.
89 * Empire code outside this file is only executed while holding this
90 * mutex. This makes sure Empire code is never preempted by Empire
93 static pthread_mutex_t mtx_ctxsw;
95 static void empth_status(char *format, ...)
96 ATTRIBUTE((format (printf, 1, 2)));
97 static void empth_alarm(int sig);
100 empth_start(void *arg)
104 ctx->id = pthread_self();
105 pthread_setspecific(ctx_key, ctx);
106 pthread_mutex_lock(&mtx_ctxsw);
114 empth_status(char *format, ...)
117 static struct timeval startTime;
123 va_start(ap, format);
124 if (empth_flags & EMPTH_PRINT) {
125 if (startTime.tv_sec == 0)
126 gettimeofday(&startTime, 0);
127 gettimeofday(&tv, 0);
128 sec = tv.tv_sec - startTime.tv_sec;
129 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
134 vsprintf(buf, format, ap);
136 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
145 empth_init(void **ctx_ptr, int flags)
149 struct sigaction act;
154 empth_init_signals();
156 sigaddset(&set, SIGHUP);
157 sigaddset(&set, SIGINT);
158 sigaddset(&set, SIGTERM);
159 pthread_sigmask(SIG_BLOCK, &set, NULL);
161 sigemptyset(&act.sa_mask);
162 act.sa_handler = empth_alarm;
163 sigaction(SIGALRM, &act, NULL);
165 pthread_key_create(&ctx_key, NULL);
166 pthread_mutex_init(&mtx_ctxsw, NULL);
168 ctx = malloc(sizeof(empth_t));
170 logerror("pthread init failed: not enough memory");
174 ctx->desc = "empire main";
177 ctx->id = pthread_self();
179 pthread_setspecific(ctx_key, ctx);
180 pthread_mutex_lock(&mtx_ctxsw);
181 logerror("pthreads initialized");
187 * prio can be used for setting scheeduling policy but...
188 * it seems to be optional in POSIX threads and Solaris
189 * for example just ignores it.
190 * More then that priority is not needed even in lwp threads.
193 empth_create(int prio, void (*entry)(void *), int size, int flags,
194 char *name, char *desc, void *ud)
201 empth_status("creating new thread %s:%s", name, desc);
203 ctx = malloc(sizeof(empth_t));
205 logerror("not enough memory to create thread: %s (%s)",
209 ctx->name = strdup(name);
210 ctx->desc = strdup(desc);
215 eno = pthread_attr_init(&attr);
217 logerror("can not create thread attribute %s (%s): %s",
218 name, desc, strerror(eno));
221 if (size < PTHREAD_STACK_MIN)
222 size = PTHREAD_STACK_MIN;
223 pthread_attr_setstacksize(&attr, size);
224 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
226 eno = pthread_create(&t, &attr, empth_start, ctx);
228 logerror("can not create thread: %s (%s): %s",
229 name, desc, strerror(eno));
232 empth_status("new thread id is %ld", (long)t);
237 pthread_attr_destroy(&attr);
244 empth_restorectx(void)
248 ctx_ptr = pthread_getspecific(ctx_key);
249 *udata = ctx_ptr->ud;
250 if (ctx_ptr->state == EMPTH_KILLED) {
251 empth_status("i am dead");
254 empth_status("context restored");
260 return pthread_getspecific(ctx_key);
266 empth_status("empth_exit");
267 pthread_mutex_unlock(&mtx_ctxsw);
268 free(pthread_getspecific(ctx_key));
275 pthread_mutex_unlock(&mtx_ctxsw);
276 pthread_mutex_lock(&mtx_ctxsw);
281 empth_terminate(empth_t *a)
283 empth_status("killing thread %s", a->name);
284 a->state = EMPTH_KILLED;
285 pthread_kill(a->id, SIGALRM);
289 empth_select(int fd, int flags)
297 pthread_mutex_unlock(&mtx_ctxsw);
298 empth_status("%s select on %d",
299 flags == EMPTH_FD_READ ? "read" : "write", fd);
309 FD_SET(fd, &readmask);
312 FD_SET(fd, &writemask);
315 logerror("bad flag %d passed to empth_select", flags);
319 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
322 if (errno == EINTR) {
323 /* go handle the signal */
324 empth_status("select broken by signal");
328 /* strange but we dont get EINTR on select broken by signal */
329 empth_status("select failed (%s)", strerror(errno));
334 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
335 empth_status("input ready");
338 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
339 empth_status("output ready");
345 pthread_mutex_lock(&mtx_ctxsw);
353 * Nothing to do --- we handle this signal just to let
354 * empth_wakeup() interrupt system calls.
356 empth_status("got alarm signal");
360 empth_wakeup(empth_t *a)
362 empth_status("waking up thread %s", a->name);
363 pthread_kill(a->id, SIGALRM);
367 empth_sleep(time_t until)
371 empth_status("going to sleep %ld sec", until - time(0));
372 pthread_mutex_unlock(&mtx_ctxsw);
373 tv.tv_sec = until - time(NULL);
376 select(0, NULL, NULL, NULL, &tv);
377 } while ((tv.tv_sec = until - time(NULL)) > 0);
378 empth_status("sleep done. Waiting for lock");
379 pthread_mutex_lock(&mtx_ctxsw);
384 empth_wait_for_signal(void)
390 sigaddset(&set, SIGHUP);
391 sigaddset(&set, SIGINT);
392 sigaddset(&set, SIGTERM);
393 pthread_mutex_unlock(&mtx_ctxsw);
395 empth_status("waiting for signals");
396 err = sigwait(&set, &sig);
397 if (CANT_HAPPEN(err)) {
401 empth_status("got awaited signal %d", sig);
402 pthread_mutex_lock(&mtx_ctxsw);
409 empth_sem_create(char *name, int cnt)
413 sm = malloc(sizeof(empth_sem_t));
415 logerror("out of memory at %s:%d", __FILE__, __LINE__);
418 strncpy(sm->name, name, sizeof(sm->name) - 1);
420 pthread_mutex_init(&sm->mtx_update, NULL);
421 pthread_mutex_init(&sm->mtx_sem, NULL);
422 pthread_cond_init(&sm->cnd_sem, NULL);
427 empth_sem_signal(empth_sem_t *sm)
429 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
430 pthread_mutex_lock(&sm->mtx_update);
431 if (sm->count++ < 0) {
432 pthread_mutex_unlock(&sm->mtx_update);
433 pthread_mutex_lock(&sm->mtx_sem);
434 pthread_cond_signal(&sm->cnd_sem);
435 pthread_mutex_unlock(&sm->mtx_sem);
437 pthread_mutex_unlock(&sm->mtx_update);
441 empth_sem_wait(empth_sem_t *sm)
443 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
444 pthread_mutex_lock(&sm->mtx_update);
445 if (--sm->count < 0) {
446 pthread_mutex_unlock(&sm->mtx_update);
447 empth_status("blocking");
448 pthread_mutex_unlock(&mtx_ctxsw);
449 pthread_mutex_lock(&sm->mtx_sem);
450 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
451 empth_status("waking up");
452 pthread_mutex_unlock(&sm->mtx_sem);
453 pthread_mutex_lock(&mtx_ctxsw);
456 pthread_mutex_unlock(&sm->mtx_update);