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-2007
34 * Ron Koenderink, 2007
37 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
38 #define _XOPEN_SOURCE 500
50 #include <sys/types.h>
57 #include "empthread.h"
58 #include "prototypes.h"
60 #define EMPTH_KILLED 1
63 char *name; /* thread name */
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 struct empth_rwlock_t {
80 pthread_rwlock_t lock;
83 /* Thread-specific data key */
84 static pthread_key_t ctx_key;
86 /* Flags that were passed to empth_init() */
87 static int empth_flags;
89 /* Pointer to thread context variable */
93 * Non-preemption mutex.
94 * Empire code outside this file is only executed while holding this
95 * mutex. This makes sure Empire code is never preempted by Empire
98 static pthread_mutex_t mtx_ctxsw;
100 static void empth_status(char *format, ...)
101 ATTRIBUTE((format (printf, 1, 2)));
102 static void empth_alarm(int sig);
105 empth_start(void *arg)
109 ctx->id = pthread_self();
110 pthread_setspecific(ctx_key, ctx);
111 pthread_mutex_lock(&mtx_ctxsw);
119 empth_status(char *format, ...)
122 static struct timeval startTime;
128 va_start(ap, format);
129 if (empth_flags & EMPTH_PRINT) {
130 if (startTime.tv_sec == 0)
131 gettimeofday(&startTime, 0);
132 gettimeofday(&tv, 0);
133 sec = tv.tv_sec - startTime.tv_sec;
134 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
139 vsprintf(buf, format, ap);
141 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
150 empth_init(void **ctx_ptr, int flags)
154 struct sigaction act;
159 empth_init_signals();
161 sigaddset(&set, SIGHUP);
162 sigaddset(&set, SIGINT);
163 sigaddset(&set, SIGTERM);
164 pthread_sigmask(SIG_BLOCK, &set, NULL);
166 sigemptyset(&act.sa_mask);
167 act.sa_handler = empth_alarm;
168 sigaction(SIGALRM, &act, NULL);
170 pthread_key_create(&ctx_key, NULL);
171 pthread_mutex_init(&mtx_ctxsw, NULL);
173 ctx = malloc(sizeof(empth_t));
175 logerror("pthread init failed: not enough memory");
181 ctx->id = pthread_self();
183 pthread_setspecific(ctx_key, ctx);
184 pthread_mutex_lock(&mtx_ctxsw);
185 logerror("pthreads initialized");
191 * prio can be used for setting scheeduling policy but...
192 * it seems to be optional in POSIX threads and Solaris
193 * for example just ignores it.
194 * More then that priority is not needed even in lwp threads.
197 empth_create(int prio, void (*entry)(void *), int size, int flags,
198 char *name, void *ud)
205 empth_status("creating new thread %s", name);
207 ctx = malloc(sizeof(empth_t));
209 logerror("not enough memory to create thread %s", name);
212 ctx->name = strdup(name);
217 eno = pthread_attr_init(&attr);
219 logerror("can not create thread attribute %s: %s",
220 name, strerror(eno));
223 if (size < PTHREAD_STACK_MIN)
224 size = PTHREAD_STACK_MIN;
225 pthread_attr_setstacksize(&attr, size);
226 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
228 eno = pthread_create(&t, &attr, empth_start, ctx);
230 logerror("can not create thread: %s: %s", name, strerror(eno));
233 empth_status("new thread id is %ld", (long)t);
238 pthread_attr_destroy(&attr);
245 empth_restorectx(void)
249 ctx_ptr = pthread_getspecific(ctx_key);
250 *udata = ctx_ptr->ud;
251 if (ctx_ptr->state == EMPTH_KILLED) {
252 empth_status("i am dead");
255 empth_status("context restored");
261 return pthread_getspecific(ctx_key);
267 empth_t *ctx = pthread_getspecific(ctx_key);
269 empth_status("empth_exit");
270 pthread_mutex_unlock(&mtx_ctxsw);
279 pthread_mutex_unlock(&mtx_ctxsw);
280 pthread_mutex_lock(&mtx_ctxsw);
285 empth_terminate(empth_t *a)
287 empth_status("killing thread %s", a->name);
288 a->state = EMPTH_KILLED;
289 pthread_kill(a->id, SIGALRM);
293 empth_select(int fd, int flags)
301 pthread_mutex_unlock(&mtx_ctxsw);
302 empth_status("%s select on %d",
303 flags == EMPTH_FD_READ ? "read" : "write", fd);
313 FD_SET(fd, &readmask);
316 FD_SET(fd, &writemask);
319 logerror("bad flag %d passed to empth_select", flags);
323 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
326 if (errno == EINTR) {
327 /* go handle the signal */
328 empth_status("select broken by signal");
332 /* strange but we dont get EINTR on select broken by signal */
333 empth_status("select failed (%s)", strerror(errno));
338 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
339 empth_status("input ready");
342 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
343 empth_status("output ready");
349 pthread_mutex_lock(&mtx_ctxsw);
357 * Nothing to do --- we handle this signal just to let
358 * empth_wakeup() interrupt system calls.
360 empth_status("got alarm signal");
364 empth_wakeup(empth_t *a)
366 empth_status("waking up thread %s", a->name);
367 pthread_kill(a->id, SIGALRM);
371 empth_sleep(time_t until)
375 empth_status("going to sleep %ld sec", until - time(0));
376 pthread_mutex_unlock(&mtx_ctxsw);
377 tv.tv_sec = until - time(NULL);
380 select(0, NULL, NULL, NULL, &tv);
381 } while ((tv.tv_sec = until - time(NULL)) > 0);
382 empth_status("sleep done. Waiting for lock");
383 pthread_mutex_lock(&mtx_ctxsw);
388 empth_wait_for_signal(void)
394 sigaddset(&set, SIGHUP);
395 sigaddset(&set, SIGINT);
396 sigaddset(&set, SIGTERM);
397 pthread_mutex_unlock(&mtx_ctxsw);
399 empth_status("waiting for signals");
400 err = sigwait(&set, &sig);
401 if (CANT_HAPPEN(err)) {
405 empth_status("got awaited signal %d", sig);
406 pthread_mutex_lock(&mtx_ctxsw);
413 empth_sem_create(char *name, int cnt)
417 sm = malloc(sizeof(empth_sem_t));
419 logerror("out of memory at %s:%d", __FILE__, __LINE__);
422 strncpy(sm->name, name, sizeof(sm->name) - 1);
424 pthread_mutex_init(&sm->mtx_update, NULL);
425 pthread_mutex_init(&sm->mtx_sem, NULL);
426 pthread_cond_init(&sm->cnd_sem, NULL);
431 empth_sem_signal(empth_sem_t *sm)
433 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
434 pthread_mutex_lock(&sm->mtx_update);
435 if (sm->count++ < 0) {
436 pthread_mutex_unlock(&sm->mtx_update);
437 pthread_mutex_lock(&sm->mtx_sem);
438 pthread_cond_signal(&sm->cnd_sem);
439 pthread_mutex_unlock(&sm->mtx_sem);
441 pthread_mutex_unlock(&sm->mtx_update);
445 empth_sem_wait(empth_sem_t *sm)
447 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
448 pthread_mutex_lock(&sm->mtx_update);
449 if (--sm->count < 0) {
450 pthread_mutex_unlock(&sm->mtx_update);
451 empth_status("blocking");
452 pthread_mutex_unlock(&mtx_ctxsw);
453 pthread_mutex_lock(&sm->mtx_sem);
454 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
455 empth_status("waking up");
456 pthread_mutex_unlock(&sm->mtx_sem);
457 pthread_mutex_lock(&mtx_ctxsw);
460 pthread_mutex_unlock(&sm->mtx_update);
464 empth_rwlock_create(char *name)
466 empth_rwlock_t *rwlock;
468 rwlock = malloc(sizeof(*rwlock));
472 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
477 rwlock->name = strdup(name);
482 empth_rwlock_destroy(empth_rwlock_t *rwlock)
484 pthread_rwlock_destroy(&rwlock->lock);
490 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
492 pthread_mutex_unlock(&mtx_ctxsw);
493 pthread_rwlock_wrlock(&rwlock->lock);
494 pthread_mutex_lock(&mtx_ctxsw);
499 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
501 pthread_mutex_unlock(&mtx_ctxsw);
502 pthread_rwlock_rdlock(&rwlock->lock);
503 pthread_mutex_lock(&mtx_ctxsw);
508 empth_rwlock_unlock(empth_rwlock_t *rwlock)
510 pthread_rwlock_unlock(&rwlock->lock);