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
47 #include <sys/types.h>
54 #include "empthread.h"
56 #define EMPTH_KILLED 1
59 char *name; /* thread name */
60 char *desc; /* description */
61 void *ud; /* user data */
62 int state; /* my state */
63 void (*ep)(void *); /* entry point */
64 pthread_t id; /* thread id */
68 pthread_mutex_t mtx_update; /* use it to update count */
71 pthread_mutex_t mtx_sem;
72 pthread_cond_t cnd_sem;
75 /* Thread-specific data key */
76 static pthread_key_t ctx_key;
78 /* Flags that were passed to empth_init() */
79 static int empth_flags;
81 /* Pointer to thread context variable */
85 * Non-preemption mutex.
86 * Empire code outside this file is only executed while holding this
87 * mutex. This makes sure Empire code is never preempted by Empire
90 static pthread_mutex_t mtx_ctxsw;
92 static void empth_status(char *format, ...)
93 ATTRIBUTE((format (printf, 1, 2)));
94 static void empth_alarm(int sig);
97 empth_start(void *arg)
101 ctx->id = pthread_self();
102 pthread_setspecific(ctx_key, ctx);
103 pthread_mutex_lock(&mtx_ctxsw);
111 empth_status(char *format, ...)
114 static struct timeval startTime;
120 va_start(ap, format);
121 if (empth_flags & EMPTH_PRINT) {
122 if (startTime.tv_sec == 0)
123 gettimeofday(&startTime, 0);
124 gettimeofday(&tv, 0);
125 sec = tv.tv_sec - startTime.tv_sec;
126 msec = (tv.tv_usec - startTime.tv_usec) / 1000;
131 vsprintf(buf, format, ap);
133 printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
142 empth_init(void **ctx_ptr, int flags)
146 struct sigaction act;
151 empth_init_signals();
153 sigaddset(&set, SIGHUP);
154 sigaddset(&set, SIGINT);
155 sigaddset(&set, SIGTERM);
156 pthread_sigmask(SIG_BLOCK, &set, NULL);
158 sigemptyset(&act.sa_mask);
159 act.sa_handler = empth_alarm;
160 sigaction(SIGALRM, &act, NULL);
162 pthread_key_create(&ctx_key, NULL);
163 pthread_mutex_init(&mtx_ctxsw, NULL);
165 ctx = malloc(sizeof(empth_t));
167 logerror("pthread init failed: not enough memory");
171 ctx->desc = "empire main";
174 ctx->id = pthread_self();
176 pthread_setspecific(ctx_key, ctx);
177 pthread_mutex_lock(&mtx_ctxsw);
178 logerror("pthreads initialized");
184 * prio can be used for setting scheeduling policy but...
185 * it seems to be optional in POSIX threads and Solaris
186 * for example just ignores it.
187 * More then that priority is not needed even in lwp threads.
190 empth_create(int prio, void (*entry)(void *), int size, int flags,
191 char *name, char *desc, void *ud)
198 empth_status("creating new thread %s:%s", name, desc);
200 ctx = malloc(sizeof(empth_t));
202 logerror("not enough memory to create thread: %s (%s)",
206 ctx->name = strdup(name);
207 ctx->desc = strdup(desc);
212 eno = pthread_attr_init(&attr);
214 logerror("can not create thread attribute %s (%s): %s",
215 name, desc, strerror(eno));
218 if (size < PTHREAD_STACK_MIN)
219 size = PTHREAD_STACK_MIN;
220 pthread_attr_setstacksize(&attr, size);
221 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
223 eno = pthread_create(&t, &attr, empth_start, ctx);
225 logerror("can not create thread: %s (%s): %s",
226 name, desc, strerror(eno));
229 empth_status("new thread id is %ld", (long)t);
234 pthread_attr_destroy(&attr);
241 empth_restorectx(void)
245 ctx_ptr = pthread_getspecific(ctx_key);
246 *udata = ctx_ptr->ud;
247 if (ctx_ptr->state == EMPTH_KILLED) {
248 empth_status("i am dead");
251 empth_status("context restored");
257 return pthread_getspecific(ctx_key);
263 empth_status("empth_exit");
264 pthread_mutex_unlock(&mtx_ctxsw);
265 free(pthread_getspecific(ctx_key));
272 pthread_mutex_unlock(&mtx_ctxsw);
273 pthread_mutex_lock(&mtx_ctxsw);
278 empth_terminate(empth_t *a)
280 empth_status("killing thread %s", a->name);
281 a->state = EMPTH_KILLED;
282 pthread_kill(a->id, SIGALRM);
286 empth_select(int fd, int flags)
294 pthread_mutex_unlock(&mtx_ctxsw);
295 empth_status("%s select on %d",
296 flags == EMPTH_FD_READ ? "read" : "write", fd);
306 FD_SET(fd, &readmask);
309 FD_SET(fd, &writemask);
312 logerror("bad flag %d passed to empth_select", flags);
316 n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
319 if (errno == EINTR) {
320 /* go handle the signal */
321 empth_status("select broken by signal");
325 /* strange but we dont get EINTR on select broken by signal */
326 empth_status("select failed (%s)", strerror(errno));
331 if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
332 empth_status("input ready");
335 if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
336 empth_status("output ready");
342 pthread_mutex_lock(&mtx_ctxsw);
350 * Nothing to do --- we handle this signal just to let
351 * empth_wakeup() interrupt system calls.
353 empth_status("got alarm signal");
357 empth_wakeup(empth_t *a)
359 empth_status("waking up thread %s", a->name);
360 pthread_kill(a->id, SIGALRM);
364 empth_sleep(time_t until)
368 empth_status("going to sleep %ld sec", until - time(0));
369 pthread_mutex_unlock(&mtx_ctxsw);
370 tv.tv_sec = until - time(NULL);
373 select(0, NULL, NULL, NULL, &tv);
374 } while ((tv.tv_sec = until - time(NULL)) > 0);
375 empth_status("sleep done. Waiting for lock");
376 pthread_mutex_lock(&mtx_ctxsw);
381 empth_wait_for_signal(void)
387 sigaddset(&set, SIGHUP);
388 sigaddset(&set, SIGINT);
389 sigaddset(&set, SIGTERM);
390 pthread_mutex_unlock(&mtx_ctxsw);
392 empth_status("waiting for signals");
393 err = sigwait(&set, &sig);
394 if (CANT_HAPPEN(err)) {
398 empth_status("got awaited signal %d", sig);
399 pthread_mutex_lock(&mtx_ctxsw);
406 empth_sem_create(char *name, int cnt)
410 sm = malloc(sizeof(empth_sem_t));
412 logerror("out of memory at %s:%d", __FILE__, __LINE__);
415 strncpy(sm->name, name, sizeof(sm->name) - 1);
417 pthread_mutex_init(&sm->mtx_update, NULL);
418 pthread_mutex_init(&sm->mtx_sem, NULL);
419 pthread_cond_init(&sm->cnd_sem, NULL);
424 empth_sem_signal(empth_sem_t *sm)
426 empth_status("signal on semaphore %s:%d", sm->name, sm->count);
427 pthread_mutex_lock(&sm->mtx_update);
428 if (sm->count++ < 0) {
429 pthread_mutex_unlock(&sm->mtx_update);
430 pthread_mutex_lock(&sm->mtx_sem);
431 pthread_cond_signal(&sm->cnd_sem);
432 pthread_mutex_unlock(&sm->mtx_sem);
434 pthread_mutex_unlock(&sm->mtx_update);
438 empth_sem_wait(empth_sem_t *sm)
440 empth_status("wait on semaphore %s:%d", sm->name, sm->count);
441 pthread_mutex_lock(&sm->mtx_update);
442 if (--sm->count < 0) {
443 pthread_mutex_unlock(&sm->mtx_update);
444 empth_status("blocking");
445 pthread_mutex_unlock(&mtx_ctxsw);
446 pthread_mutex_lock(&sm->mtx_sem);
447 pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
448 empth_status("waking up");
449 pthread_mutex_unlock(&sm->mtx_sem);
450 pthread_mutex_lock(&mtx_ctxsw);
453 pthread_mutex_unlock(&sm->mtx_update);