/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
*
* ---
*
- * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- * related information and legal notices. It is expected that any future
- * projects/authors will amend these files as needed.
+ * See files README, COPYING and CREDITS in the root of the source
+ * tree for related information and legal notices. It is expected
+ * that future projects/authors will amend these files as needed.
*
* ---
*
* Sasha Mikheev
* Doug Hay, 1998
* Steve McClure, 1998
+ * Markus Armbruster, 2005
*/
-#ifndef _EMTHREAD_H_
-#define _EMTHREAD_H_
+/*
+ * This header defines Empire's abstract thread interface. There are
+ * several concrete implementations.
+ *
+ * Empire threads are non-preemptive, i.e. they run until they
+ * voluntarily yield the processor. The thread scheduler then picks
+ * one of the runnable threads with the highest priority. Priorities
+ * are static. Empire code relies on these properties heavily. The
+ * most common form of yielding the processor is sleeping for some
+ * event to happen.
+ */
-#include "misc.h"
+#ifndef EMPTHREAD_H
+#define EMPTHREAD_H
+
+/* thread priorities */
+enum {
+ PP_MAIN = 7,
+ PP_UPDATE = 6,
+ PP_SHUTDOWN = 5,
+ PP_SCHED = 4,
+ PP_TIMESTAMP = 2,
+ PP_PLAYER = 3,
+ PP_ACCEPT = 3,
+ PP_KILLIDLE = 2
+};
+
+#ifdef EMPTH_LWP
+#include "lwp.h"
-#if defined(_WIN32)
-#undef _EMPTH_LWP
-#undef _EMPTH_POSIX
-#define _EMPTH_WIN32
-#endif
+/* Abstract data types */
-#ifdef _EMPTH_LWP
-#include "lwp.h"
+/* empth_t * represents a thread. */
typedef struct lwpProc empth_t;
+
+/* empth_sem_t * represents a semaphore */
typedef struct lwpSem empth_sem_t;
+
+/* Flags for empth_select(): whether to sleep on input or output */
#define EMPTH_FD_READ LWP_FD_READ
#define EMPTH_FD_WRITE LWP_FD_WRITE
+
+/* Flags for empth_init() and empth_create() */
+/* Request debug prints */
#define EMPTH_PRINT LWP_PRINT
+/* Request stack checking */
#define EMPTH_STACKCHECK LWP_STACKCHECK
-#endif
-#ifdef _EMPTH_POSIX
-#ifdef __linux__
-#define _MIT_POSIX_THREADS 1
-#endif
+#endif /* EMPTH_LWP */
+
+#ifdef EMPTH_POSIX
#include <pthread.h>
#define EMPTH_FD_READ 0x1
#define EMPTH_FD_WRITE 0x2
#define EMPTH_PRINT 0x1
#define EMPTH_STACKCHECK 0x2
-#define EMPTH_KILLED 1
-typedef struct empth_ctx_t {
- char *name; /* thread name */
- char *desc; /* description */
- void *ud; /* user data */
- int state; /* my state */
- void (*ep)(void *); /* entry point */
- pthread_t id; /* thread id */
-} empth_t;
-
-typedef struct {
- pthread_mutex_t mtx_update; /* use it to update count */
- int count;
- char name[80];
- pthread_mutex_t mtx_sem;
- pthread_cond_t cnd_sem;
-} empth_sem_t;
-
-#endif
-
-/* DEC has slightly different names for whatever reason... */
-#ifdef _DECTHREADS_
-#define pthread_key_create pthread_keycreate
-#define pthread_attr_init pthread_attr_create
-#define pthread_attr_destroy pthread_attr_delete
-
-#endif
+typedef struct empth_t empth_t;
+typedef struct empth_sem_t empth_sem_t;
+#endif /* EMPTH_POSIX */
-#if defined(_EMPTH_WIN32)
+#ifdef EMPTH_W32
/* The Windows NT Threads */
#define EMPTH_FD_READ 0x1
#define EMPTH_FD_WRITE 0x2
#define EMPTH_PRINT 0x1
#define EMPTH_STACKCHECK 0x2
-typedef void empth_t;
+typedef struct loc_Thread_t empth_t;
+typedef struct loc_Sem_t empth_sem_t;
-typedef void empth_sem_t;
+void empth_request_shutdown(void);
+#endif /* EMPTH_W32 */
-#endif
+/*
+ * Initialize thread package.
+ * CTX points to a thread context variable; see empth_create().
+ * FLAGS request optional features.
+ * Should return 0 on success, -1 on error, but currently always
+ * returns 0.
+ */
+int empth_init(void **ctx, int flags);
+
+/*
+ * Create a new thread.
+ * PRIO is the scheduling priority.
+ * ENTRY is the entry point. It will be called with argument UD.
+ * Thread stack is at least SIZE bytes.
+ * FLAGS should be the same as were passed to empth_init(), or zero.
+ * NAME is the threads name, and DESC its description. These are used
+ * for logging and debugging.
+ * UD is the value to pass to ENTRY. It is also assigned to the
+ * context variable defined with empth_init() whenever the thread gets
+ * scheduled.
+ * Yield the processor.
+ * Return the thread, or NULL on error.
+ */
+empth_t *empth_create(int prio, void (*entry)(void *),
+ int size, int flags, char *name, char *desc, void *ud);
-int empth_init(char **ctx, int flags);
-empth_t *empth_create(int, void (*)(void *), int, int, char *, char *, void *);
+/*
+ * Return the current thread.
+ */
empth_t *empth_self(void);
+
+/*
+ * Terminate the current thread.
+ * The current thread should not be the thread that executed main().
+ * If it is, implementations may terminate the process rather than the
+ * thread.
+ * Never returns.
+ */
void empth_exit(void);
+
+/*
+ * Yield the processor.
+ */
void empth_yield(void);
-void empth_terminate(empth_t *);
+
+/*
+ * Terminate THREAD.
+ * THREAD will not be scheduled again. Instead, it will terminate as
+ * if it executed empth_exit(). It is unspecified when exactly that
+ * happens.
+ * THREAD must not be the current thread.
+ */
+void empth_terminate(empth_t *thread);
+
+/*
+ * Put current thread to sleep until file descriptor FD is ready for I/O.
+ * If FLAGS & EMPTH_FD_READ, wake up if FD is ready for input.
+ * If FLAGS & EMPTH_FD_WRITE, wake up if FD is ready for output.
+ * At most one thread may sleep on the same file descriptor.
+ * Note: Currently, Empire sleeps only on network I/O, i.e. FD is a
+ * socket. Implementations should not rely on that.
+ */
void empth_select(int fd, int flags);
-void empth_wakeup(empth_t *);
-void empth_sleep(long until);
+
+/*
+ * Awaken THREAD if it is sleeping in empth_select().
+ * Note: This must not awaken threads sleeping in other functions.
+ */
+void empth_wakeup(empth_t *thread);
+
+/*
+ * Put current thread to sleep until the time is UNTIL.
+ * May sleep somehwat longer, but never shorter.
+ */
+void empth_sleep(time_t until);
+
+/*
+ * Wait for some implementation-defined external shutdown signal.
+ * Return a non-negative number identifying the signal.
+ */
+int empth_wait_for_shutdown(void);
+
+/*
+ * Create a semaphore.
+ * NAME is its name, it is used for debugging.
+ * COUNT is the initial count value of the semaphore, it must not be
+ * negative.
+ * Return the semaphore, or NULL on error.
+ */
empth_sem_t *empth_sem_create(char *name, int count);
-void empth_sem_signal(empth_sem_t *);
-void empth_sem_wait(empth_sem_t *);
-void empth_alarm(int);
+/*
+ * Signal SEM.
+ * Increase SEM's count. If threads are sleeping on it, wake up
+ * exactly one of them. If that thread has a higher priority, yield
+ * the processor.
+ * This semaphore operation is often called `down' or `V' otherwhere.
+ */
+void empth_sem_signal(empth_sem_t *sem);
+
+/*
+ * Wait for SEM.
+ * If SEM has a zero count, put current thread to sleep until
+ * empth_sem_signal() awakens it. SEM will have non-zero value then.
+ * Decrement SEM's count.
+ * This semaphore operation is often called `up' or `P' otherwhere.
+ */
+void empth_sem_wait(empth_sem_t *sem);
+
+
+/*
+ * Stuff for implementations, not for clients.
+ */
+
+void empth_init_signals(void);
-#include "prototypes.h" /* must come at end, after defines and typedefs */
#endif