(loc_GVAR): Putting file local variables into a struct serves no

useful purpose whatsoever.  Peel off the struct.
This commit is contained in:
Markus Armbruster 2005-06-30 19:53:03 +00:00
parent dfe767e7e0
commit 8e8ffb483c

View file

@ -107,37 +107,35 @@ struct loc_Sem_t {
int count; int count;
}; };
static struct { /* This is the thread exclusion/non-premption mutex. */
/* This is the thread exclusion/non-premption mutex. */ /* The running thread has this MUTEX, and all others are */
/* The running thread has this MUTEX, and all others are */ /* either blocked on it, or waiting for some OS response. */
/* either blocked on it, or waiting for some OS response. */ static HANDLE hThreadMutex;
HANDLE hThreadMutex;
/* This is the thread startup event sem. */ /* This is the thread startup event sem. */
/* We use this to lockstep when we are starting up threads. */ /* We use this to lockstep when we are starting up threads. */
HANDLE hThreadStartEvent; static HANDLE hThreadStartEvent;
/* This is an event used to wakeup the main thread */ /* This is an event used to wakeup the main thread */
/* to start the shutdown sequence. */ /* to start the shutdown sequence. */
HANDLE hShutdownEvent; static HANDLE hShutdownEvent;
/* The Thread Local Storage index. We store the pThread pointer */ /* The Thread Local Storage index. We store the pThread pointer */
/* for each thread at this index. */ /* for each thread at this index. */
DWORD dwTLSIndex; static DWORD dwTLSIndex;
/* The current running thread. */ /* The current running thread. */
empth_t *pCurThread; static empth_t *pCurThread;
/* Ticks at start */ /* Ticks at start */
unsigned long ulTickAtStart; static unsigned long ulTickAtStart;
/* Pointer out to global context. "player". */ /* Pointer out to global context. "player". */
/* From empth_init parameter. */ /* From empth_init parameter. */
void **ppvUserData; static void **ppvUserData;
/* Global flags. From empth_init parameter. */ /* Global flags. From empth_init parameter. */
int flags; static int flags;
} loc_GVAR;
/************************ /************************
@ -152,15 +150,15 @@ loc_debug(const char *pszFmt, ...)
unsigned long ulCurTick; unsigned long ulCurTick;
unsigned long ulRunTick; unsigned long ulRunTick;
unsigned long ulMs, ulSec, ulMin, ulHr; unsigned long ulMs, ulSec, ulMin, ulHr;
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
char buf[1024]; char buf[1024];
if ((loc_GVAR.flags & EMPTH_PRINT) != 0) { if ((flags & EMPTH_PRINT) != 0) {
/* Ticks are in milliseconds */ /* Ticks are in milliseconds */
ulCurTick = GetTickCount(); ulCurTick = GetTickCount();
ulRunTick = ulCurTick - loc_GVAR.ulTickAtStart; ulRunTick = ulCurTick - ulTickAtStart;
ulMs = ulRunTick % 1000L; ulMs = ulRunTick % 1000L;
ulSec = (ulRunTick / 1000L) % 60L; ulSec = (ulRunTick / 1000L) % 60L;
ulMin = (ulRunTick / (60L * 1000L)) % 60L; ulMin = (ulRunTick / (60L * 1000L)) % 60L;
@ -206,24 +204,24 @@ loc_FreeThreadInfo(empth_t *pThread)
static void static void
loc_RunThisThread() loc_RunThisThread()
{ {
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
if (pThread->bKilled) { if (pThread->bKilled) {
if (!pThread->bMainThread) { if (!pThread->bMainThread) {
TlsSetValue(loc_GVAR.dwTLSIndex, NULL); TlsSetValue(dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread); loc_FreeThreadInfo(pThread);
_endthread(); _endthread();
} }
} }
/* Get the MUTEX semaphore, wait forever. */ /* Get the MUTEX semaphore, wait forever. */
WaitForSingleObject(loc_GVAR.hThreadMutex, INFINITE); WaitForSingleObject(hThreadMutex, INFINITE);
if (!loc_GVAR.pCurThread) { if (!pCurThread) {
/* Set the globals to this thread. */ /* Set the globals to this thread. */
*loc_GVAR.ppvUserData = pThread->pvUserData; *ppvUserData = pThread->pvUserData;
loc_GVAR.pCurThread = pThread; pCurThread = pThread;
} else { } else {
/* Hmm, a problem, eh? */ /* Hmm, a problem, eh? */
logerror("RunThisThread, someone already running."); logerror("RunThisThread, someone already running.");
@ -238,16 +236,16 @@ loc_RunThisThread()
static void static void
loc_BlockThisThread() loc_BlockThisThread()
{ {
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
if (loc_GVAR.pCurThread == pThread) { if (pCurThread == pThread) {
/* Reset the globals back to original */ /* Reset the globals back to original */
loc_GVAR.pCurThread = NULL; pCurThread = NULL;
*loc_GVAR.ppvUserData = NULL; *ppvUserData = NULL;
/* Release the MUTEX */ /* Release the MUTEX */
ReleaseMutex(loc_GVAR.hThreadMutex); ReleaseMutex(hThreadMutex);
} else { } else {
/* Hmm, this thread was not the running one. */ /* Hmm, this thread was not the running one. */
logerror("BlockThisThread, not running."); logerror("BlockThisThread, not running.");
@ -287,7 +285,7 @@ loc_Exit_Handler(DWORD fdwCtrlType)
void void
empth_request_shutdown(void) empth_request_shutdown(void)
{ {
SetEvent(loc_GVAR.hShutdownEvent); SetEvent(hShutdownEvent);
} }
/************************ /************************
@ -300,7 +298,7 @@ static void
loc_BlockMainThread(void) loc_BlockMainThread(void)
{ {
/* Get the MUTEX semaphore, wait the number of MS */ /* Get the MUTEX semaphore, wait the number of MS */
WaitForSingleObject(loc_GVAR.hShutdownEvent, INFINITE); WaitForSingleObject(hShutdownEvent, INFINITE);
} }
/************************ /************************
@ -321,13 +319,13 @@ empth_threadMain(void *pvData)
return; return;
/* Store pThread on this thread. */ /* Store pThread on this thread. */
TlsSetValue(loc_GVAR.dwTLSIndex, pvData); TlsSetValue(dwTLSIndex, pvData);
/* Get the ID of the thread. */ /* Get the ID of the thread. */
pThread->ulThreadID = GetCurrentThreadId(); pThread->ulThreadID = GetCurrentThreadId();
/* Signal that the thread has started. */ /* Signal that the thread has started. */
SetEvent(loc_GVAR.hThreadStartEvent); SetEvent(hThreadStartEvent);
/* seed the rand() function */ /* seed the rand() function */
time(&now); time(&now);
@ -356,31 +354,31 @@ empth_init(void **ctx_ptr, int flags)
{ {
empth_t *pThread = NULL; empth_t *pThread = NULL;
loc_GVAR.ulTickAtStart = GetTickCount(); ulTickAtStart = GetTickCount();
loc_GVAR.ppvUserData = ctx_ptr; ppvUserData = ctx_ptr;
loc_GVAR.flags = flags; flags = flags;
loc_GVAR.dwTLSIndex = TlsAlloc(); dwTLSIndex = TlsAlloc();
/* Create the thread mutex sem. */ /* Create the thread mutex sem. */
/* Initally unowned. */ /* Initally unowned. */
loc_GVAR.hThreadMutex = CreateMutex(NULL, FALSE, NULL); hThreadMutex = CreateMutex(NULL, FALSE, NULL);
if (!loc_GVAR.hThreadMutex) { if (!hThreadMutex) {
logerror("Failed to create mutex %d", GetLastError()); logerror("Failed to create mutex %d", GetLastError());
return 0; return 0;
} }
/* Create the thread start event sem. */ /* Create the thread start event sem. */
/* Automatic state reset. */ /* Automatic state reset. */
loc_GVAR.hThreadStartEvent = CreateEvent(NULL, FALSE, FALSE, NULL); hThreadStartEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!loc_GVAR.hThreadStartEvent) { if (!hThreadStartEvent) {
logerror("Failed to create start event %d", GetLastError()); logerror("Failed to create start event %d", GetLastError());
return 0; return 0;
} }
/* Create the shutdown event for the main thread. */ /* Create the shutdown event for the main thread. */
/* Manual reset */ /* Manual reset */
loc_GVAR.hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL); hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!loc_GVAR.hShutdownEvent) { if (!hShutdownEvent) {
logerror("Failed to create shutdown event %d", GetLastError()); logerror("Failed to create shutdown event %d", GetLastError());
return 0; return 0;
} }
@ -400,7 +398,7 @@ empth_init(void **ctx_ptr, int flags)
pThread->ulThreadID = GetCurrentThreadId(); pThread->ulThreadID = GetCurrentThreadId();
pThread->bMainThread = TRUE; pThread->bMainThread = TRUE;
TlsSetValue(loc_GVAR.dwTLSIndex, pThread); TlsSetValue(dwTLSIndex, pThread);
/* Make this the running thread. */ /* Make this the running thread. */
loc_RunThisThread(); loc_RunThisThread();
@ -478,7 +476,7 @@ empth_create(int prio, void (*entry)(void *), int size, int flags,
empth_t * empth_t *
empth_self(void) empth_self(void)
{ {
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
return pThread; return pThread;
} }
@ -489,7 +487,7 @@ empth_self(void)
void void
empth_exit(void) empth_exit(void)
{ {
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
loc_BlockThisThread(); loc_BlockThisThread();
@ -500,7 +498,7 @@ empth_exit(void)
loc_RunThisThread(); loc_RunThisThread();
shutdwn(0); shutdwn(0);
} else { } else {
TlsSetValue(loc_GVAR.dwTLSIndex, NULL); TlsSetValue(dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread); loc_FreeThreadInfo(pThread);
_endthread(); _endthread();
} }
@ -544,7 +542,7 @@ void
empth_select(int fd, int flags) empth_select(int fd, int flags)
{ {
WSAEVENT hEventObject[2]; WSAEVENT hEventObject[2];
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
loc_debug("%s select on %d", loc_debug("%s select on %d",
flags == EMPTH_FD_READ ? "read" : "write", fd); flags == EMPTH_FD_READ ? "read" : "write", fd);
@ -577,7 +575,7 @@ empth_select(int fd, int flags)
void void
empth_alarm(int sig) empth_alarm(int sig)
{ {
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
loc_debug("got alarm signal %d", sig); loc_debug("got alarm signal %d", sig);
@ -676,7 +674,7 @@ empth_sem_signal(empth_sem_t *pSem)
void void
empth_sem_wait(empth_sem_t *pSem) empth_sem_wait(empth_sem_t *pSem)
{ {
empth_t *pThread = TlsGetValue(loc_GVAR.dwTLSIndex); empth_t *pThread = TlsGetValue(dwTLSIndex);
loc_debug("wait on semaphore %s:%d", pSem->szName, pSem->count); loc_debug("wait on semaphore %s:%d", pSem->szName, pSem->count);