/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* ---
*
* ntthread.c: Interface from Empire threads to Windows NT threads
- *
+ *
* Known contributors to this file:
* Doug Hay, 1998
* Steve McClure, 1998
- * Ron Koenderink, 2004-2007
+ * Ron Koenderink, 2004-2008
*/
/*
struct loc_Thread {
/* The thread name, passed in at create time. */
- char szName[17];
+ char *szName;
/* True if this is the main line, and not a real thread. */
BOOL bMainThread;
*
*/
struct loc_RWLock {
- char name[17]; /* The thread name, passed in at create time. */
+ char *name; /* The lock name, passed in at create time. */
HANDLE can_read; /* Manual event -- allows read locks */
HANDLE can_write; /* Auto-reset event -- allows write locks */
int nread; /* number of active readers */
/* Global flags. From empth_init parameter. */
static int global_flags;
+static void loc_debug(const char *, ...) ATTRIBUTE((format(printf, 1, 2)));
/************************
* loc_debug
if (pThread) {
if (pThread->hThreadEvent)
CloseHandle(pThread->hThreadEvent);
+ if (pThread->szName != NULL)
+ free(pThread->szName);
memset(pThread, 0, sizeof(*pThread));
free(pThread);
}
* System-Shutdown will initiate a shutdown.
* This is done by calling empth_request_shutdown()
*/
-static BOOL
+static BOOL WINAPI
loc_Exit_Handler(DWORD fdwCtrlType)
{
- switch (fdwCtrlType) {
+ switch (fdwCtrlType) {
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
- case CTRL_BREAK_EVENT:
- case CTRL_LOGOFF_EVENT:
- case CTRL_SHUTDOWN_EVENT:
+ case CTRL_BREAK_EVENT:
+ case CTRL_LOGOFF_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
empth_request_shutdown();
return TRUE;
default:
static void
empth_threadMain(void *pvData)
{
- time_t now;
-
empth_t *pThread = pvData;
/* Out of here... */
/* Signal that the thread has started. */
SetEvent(hThreadStartEvent);
- /*
- * seed the rand() function
- * In WIN32, each thread has seed
- */
- time(&now);
- srandom(now ^ (unsigned)pThread);
-
/* Switch to this thread context */
loc_RunThisThread(NULL);
logerror("Failed to create shutdown event %lu", GetLastError());
return 0;
}
- SetConsoleCtrlHandler((PHANDLER_ROUTINE)loc_Exit_Handler, TRUE);
+ SetConsoleCtrlHandler(loc_Exit_Handler, TRUE);
/* Create the global Thread context. */
pThread = malloc(sizeof(*pThread));
}
memset(pThread, 0, sizeof(*pThread));
- strncpy(pThread->szName, "Main", sizeof(pThread->szName) - 1);
+ pThread->szName = strdup("Main");
pThread->ulThreadID = GetCurrentThreadId();
pThread->bMainThread = TRUE;
}
memset(pThread, 0, sizeof(*pThread));
- strncpy(pThread->szName, name, sizeof(pThread->szName) - 1);
+ pThread->szName = strdup(name);
pThread->pvUserData = ud;
pThread->pfnEntry = entry;
pThread->bMainThread = FALSE;
size = loc_MIN_THREAD_STACK;
pThread->ulThreadID = _beginthread(empth_threadMain, size, pThread);
- if (pThread->ulThreadID == 1L) {
+ if (pThread->ulThreadID == 1L || pThread->ulThreadID == 0L) {
logerror("can not create thread: %s: %s", name, strerror(errno));
goto bad;
}
return pThread;
}
+/************************
+ * empth_name
+ */
+char *
+empth_name(empth_t *thread)
+{
+ return thread->szName;
+}
+
+/************************
+ * empth_set_name
+ * Set the thread name
+ */
+void
+empth_set_name(empth_t *thread, char *name)
+{
+ if (thread->szName != NULL)
+ free(thread->szName);
+ thread->szName = strdup(name);
+}
+
/************************
* empth_exit
*/
empth_yield(void)
{
loc_BlockThisThread();
+ Sleep(0);
loc_RunThisThread(NULL);
}
int
empth_sleep(time_t until)
{
- long lSec;
+ long lSec = until - time(0) > 0 ? until - time(0) : 0;
empth_t *pThread = TlsGetValue(dwTLSIndex);
int iReturn = 0;
- while (!iReturn && ((lSec = until - time(0)) > 0)) {
+ do {
loc_BlockThisThread();
loc_debug("going to sleep %ld sec", lSec);
loc_debug("sleep done. Waiting to run.");
loc_RunThisThread(NULL);
- }
+ } while (!iReturn && ((lSec = until - time(0)) > 0));
+
return iReturn;
}
return NULL;
memset(rwlock, 0, sizeof(*rwlock));
- strncpy(rwlock->name, name, sizeof(rwlock->name) - 1);
+ rwlock->name = strdup(name);
if ((rwlock->can_read = CreateEvent(NULL, TRUE, TRUE, NULL)) == NULL) {
logerror("rwlock_create: failed to create reader event %s at %s:%d",
name, __FILE__, __LINE__);
+ free(rwlock->name);
free(rwlock);
return NULL;
}
if ((rwlock->can_write = CreateEvent(NULL, FALSE, TRUE, NULL)) == NULL) {
logerror("rwlock_create: failed to create writer event %s at %s:%d",
name, __FILE__, __LINE__);
+ free(rwlock->name);
CloseHandle(rwlock->can_read);
free(rwlock);
return NULL;
{
if (CANT_HAPPEN(rwlock->nread || rwlock->nwrite))
return;
+ if (rwlock->name != NULL)
+ free(rwlock->name);
CloseHandle(rwlock->can_read);
CloseHandle(rwlock->can_write);
free(rwlock);