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_status("empth_exit");
268 pthread_mutex_unlock(&mtx_ctxsw);
269 free(pthread_getspecific(ctx_key));
276 pthread_mutex_unlock(&mtx_ctxsw);
277 pthread_mutex_lock(&mtx_ctxsw);
282 empth_terminate(empth_t *a)
284 empth_status("killing thread %s", a->name);
285 a->state = EMPTH_KILLED;
286 pthread_kill(a->id, SIGALRM);
290 empth_select(int fd, int flags)
298 pthread_mutex_unlock(&mtx_ctxsw);
299 empth_status("%s select on %d",
300 flags == EMPTH_FD_READ ? "read" : "write", fd);
310 FD_SET(fd, &readmask);
313 FD_SET(fd, &writemask);
316 logerror("bad flag %d passed to empth_select", flags);
320 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
323 if (errno == EINTR) {
324 /* go handle the signal */
325 empth_status("select broken by signal");
329 /* strange but we dont get EINTR on select broken by signal */
330 empth_status("select failed (%s)", strerror(errno));
335 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
336 empth_status("input ready");
339 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
340 empth_status("output ready");
346 pthread_mutex_lock(&mtx_ctxsw);
354 * Nothing to do --- we handle this signal just to let
355 * empth_wakeup() interrupt system calls.
357 empth_status("got alarm signal");
361 empth_wakeup(empth_t *a)
363 empth_status("waking up thread %s", a->name);
364 pthread_kill(a->id, SIGALRM);
368 empth_sleep(time_t until)
372 empth_status("going to sleep %ld sec", until - time(0));
373 pthread_mutex_unlock(&mtx_ctxsw);
374 tv.tv_sec = until - time(NULL);
377 select(0, NULL, NULL, NULL, &tv);
378 } while ((tv.tv_sec = until - time(NULL)) > 0);
379 empth_status("sleep done. Waiting for lock");
380 pthread_mutex_lock(&mtx_ctxsw);
385 empth_wait_for_signal(void)
391 sigaddset(&set, SIGHUP);
392 sigaddset(&set, SIGINT);
393 sigaddset(&set, SIGTERM);
394 pthread_mutex_unlock(&mtx_ctxsw);
396 empth_status("waiting for signals");
397 err = sigwait(&set, &sig);
398 if (CANT_HAPPEN(err)) {
402 empth_status("got awaited signal %d", sig);
403 pthread_mutex_lock(&mtx_ctxsw);
410 empth_sem_create(char *name, int cnt)
414 sm = malloc(sizeof(empth_sem_t));
416 logerror("out of memory at %s:%d", __FILE__, __LINE__);
419 strncpy(sm->name, name, sizeof(sm->name) - 1);
421 pthread_mutex_init(&sm->mtx_update, NULL);
422 pthread_mutex_init(&sm->mtx_sem, NULL);
423 pthread_cond_init(&sm->cnd_sem, NULL);
428 empth_sem_signal(empth_sem_t *sm)
430 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
431 pthread_mutex_lock(&sm->mtx_update);
432 if (sm->count++ < 0) {
433 pthread_mutex_unlock(&sm->mtx_update);
434 pthread_mutex_lock(&sm->mtx_sem);
435 pthread_cond_signal(&sm->cnd_sem);
436 pthread_mutex_unlock(&sm->mtx_sem);
438 pthread_mutex_unlock(&sm->mtx_update);
442 empth_sem_wait(empth_sem_t *sm)
444 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
445 pthread_mutex_lock(&sm->mtx_update);
446 if (--sm->count < 0) {
447 pthread_mutex_unlock(&sm->mtx_update);
448 empth_status("blocking");
449 pthread_mutex_unlock(&mtx_ctxsw);
450 pthread_mutex_lock(&sm->mtx_sem);
451 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
452 empth_status("waking up");
453 pthread_mutex_unlock(&sm->mtx_sem);
454 pthread_mutex_lock(&mtx_ctxsw);
457 pthread_mutex_unlock(&sm->mtx_update);
461 empth_rwlock_create(char *name)
463 empth_rwlock_t *rwlock;
465 rwlock = malloc(sizeof(*rwlock));
469 if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
474 rwlock->name = strdup(name);
479 empth_rwlock_destroy(empth_rwlock_t *rwlock)
481 pthread_rwlock_destroy(&rwlock->lock);
486 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
488 pthread_mutex_unlock(&mtx_ctxsw);
489 pthread_rwlock_wrlock(&rwlock->lock);
490 pthread_mutex_lock(&mtx_ctxsw);
495 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
497 pthread_mutex_unlock(&mtx_ctxsw);
498 pthread_rwlock_rdlock(&rwlock->lock);
499 pthread_mutex_lock(&mtx_ctxsw);
504 empth_rwlock_unlock(empth_rwlock_t *rwlock)
506 pthread_rwlock_unlock(&rwlock->lock);