diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common/console.c | 14 | ||||
-rw-r--r-- | src/common/console.h | 15 | ||||
-rw-r--r-- | src/common/spinlock.h | 25 | ||||
-rw-r--r-- | src/common/thread.c | 73 | ||||
-rw-r--r-- | src/common/thread.h | 33 | ||||
-rw-r--r-- | src/test/test_spinlock.c | 9 |
6 files changed, 91 insertions, 78 deletions
diff --git a/src/common/console.c b/src/common/console.c index 4a12ba24b..f1b4523e2 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -57,6 +57,7 @@ struct console_interface console_s; struct console_interface *console; #ifdef CONSOLE_INPUT struct console_input_interface console_input_s; +struct spin_lock console_ptlock_s; struct { char queue[CONSOLE_PARSE_SIZE][MAX_CONSOLE_INPUT]; @@ -464,15 +465,15 @@ void *cThread_main(void *x) { console->input->parse(input); if( input[0] != '\0' ) {/* did we get something? */ - EnterSpinLock(&console->input->ptlock); + EnterSpinLock(console->input->ptlock); if( cinput.count == CONSOLE_PARSE_SIZE ) { - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); continue;/* drop */ } safestrncpy(cinput.queue[cinput.count++],input,MAX_CONSOLE_INPUT); - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); } } mutex->lock(console->input->ptmutex); @@ -484,12 +485,12 @@ void *cThread_main(void *x) { } int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { int i; - EnterSpinLock(&console->input->ptlock); + EnterSpinLock(console->input->ptlock); for(i = 0; i < cinput.count; i++) { console->input->parse_sub(cinput.queue[i]); } cinput.count = 0; - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); mutex->cond_signal(console->input->ptcond); return 0; } @@ -510,7 +511,7 @@ void console_parse_init(void) { console->input->ptstate = 1; - InitializeSpinLock(&console->input->ptlock); + InitializeSpinLock(console->input->ptlock); console->input->ptmutex = mutex->create(); console->input->ptcond = mutex->cond_create(); @@ -563,6 +564,7 @@ void console_defaults(void) console->display_gplnotice = display_gplnotice; #ifdef CONSOLE_INPUT console->input = &console_input_s; + console->input->ptlock = &console_ptlock_s; console->input->parse_init = console_parse_init; console->input->parse_final = console_parse_final; console->input->parse_timer = console_parse_timer; diff --git a/src/common/console.h b/src/common/console.h index f97e36456..dd3a9cf2e 100644 --- a/src/common/console.h +++ b/src/common/console.h @@ -23,12 +23,13 @@ #include "common/hercules.h" #include "common/db.h" #include "common/spinlock.h" -#include "common/thread.h" /* Forward Declarations */ struct Sql; // common/sql.h -struct mutex_data; struct cond_data; +struct mutex_data; +struct spin_lock; +struct thread_handle; /** * Queue Max @@ -72,11 +73,11 @@ struct CParseEntry { struct console_input_interface { #ifdef CONSOLE_INPUT /* vars */ - SPIN_LOCK ptlock;/* parse thread lock */ - rAthread *pthread;/* parse thread */ - volatile int32 ptstate;/* parse thread state */ - struct mutex_data *ptmutex; ///< parse thread mutex. - struct cond_data *ptcond; ///< parse thread conditional variable. + struct spin_lock *ptlock; ///< parse thread lock. + struct thread_handle *pthread; ///< parse thread. + volatile int32 ptstate; ///< parse thread state. + struct mutex_data *ptmutex; ///< parse thread mutex. + struct cond_data *ptcond; ///< parse thread conditional variable. /* */ VECTOR_DECL(struct CParseEntry *) command_list; VECTOR_DECL(struct CParseEntry *) commands; diff --git a/src/common/spinlock.h b/src/common/spinlock.h index f1df6ef34..c04416285 100644 --- a/src/common/spinlock.h +++ b/src/common/spinlock.h @@ -37,31 +37,28 @@ #endif #ifdef WIN32 - -typedef struct __declspec( align(64) ) SPIN_LOCK{ +struct __declspec(align(64)) spin_lock { volatile LONG lock; volatile LONG nest; volatile LONG sync_lock; -} SPIN_LOCK; +}; #else -typedef struct SPIN_LOCK{ - volatile int32 lock; - volatile int32 nest; // nesting level. - - volatile int32 sync_lock; -} __attribute__((aligned(64))) SPIN_LOCK; +struct spin_lock { + volatile int32 lock; + volatile int32 nest; // nesting level. + volatile int32 sync_lock; +} __attribute__((aligned(64))); #endif - #ifdef HERCULES_CORE -static forceinline void InitializeSpinLock(SPIN_LOCK *lck) +static forceinline void InitializeSpinLock(struct spin_lock *lck) { lck->lock = 0; lck->nest = 0; lck->sync_lock = 0; } -static forceinline void FinalizeSpinLock(SPIN_LOCK *lck) +static forceinline void FinalizeSpinLock(struct spin_lock *lck) { return; } @@ -70,7 +67,7 @@ static forceinline void FinalizeSpinLock(SPIN_LOCK *lck) #define getsynclock(l) do { if(InterlockedCompareExchange((l), 1, 0) == 0) break; thread->yield(); } while(/*always*/1) #define dropsynclock(l) do { InterlockedExchange((l), 0); } while(0) -static forceinline void EnterSpinLock(SPIN_LOCK *lck) +static forceinline void EnterSpinLock(struct spin_lock *lck) { int tid = thread->get_tid(); @@ -97,7 +94,7 @@ static forceinline void EnterSpinLock(SPIN_LOCK *lck) } -static forceinline void LeaveSpinLock(SPIN_LOCK *lck) +static forceinline void LeaveSpinLock(struct spin_lock *lck) { int tid = thread->get_tid(); diff --git a/src/common/thread.c b/src/common/thread.c index 7bcffaf86..8d862bbfc 100644 --- a/src/common/thread.c +++ b/src/common/thread.c @@ -48,13 +48,13 @@ #define HAS_TLS #endif -#define RA_THREADS_MAX 64 +#define THREADS_MAX 64 -struct rAthread { +struct thread_handle { unsigned int myID; - RATHREAD_PRIO prio; - rAthreadProc proc; + enum thread_priority prio; + threadFunc proc; void *param; #ifdef WIN32 @@ -74,13 +74,13 @@ struct thread_interface *thread; /// /// Subystem Code /// -static struct rAthread l_threads[RA_THREADS_MAX]; +static struct thread_handle l_threads[THREADS_MAX]; void rathread_init(void) { register unsigned int i; - memset(&l_threads, 0x00, RA_THREADS_MAX * sizeof(struct rAthread) ); + memset(&l_threads, 0x00, THREADS_MAX * sizeof(struct thread_handle)); - for(i = 0; i < RA_THREADS_MAX; i++){ + for (i = 0; i < THREADS_MAX; i++) { l_threads[i].myID = i; } @@ -88,8 +88,8 @@ void rathread_init(void) { #ifdef HAS_TLS g_rathread_ID = 0; #endif - l_threads[0].prio = RAT_PRIO_NORMAL; - l_threads[0].proc = (rAthreadProc)0xDEADCAFE; + l_threads[0].prio = THREADPRIO_NORMAL; + l_threads[0].proc = (threadFunc)0xDEADCAFE; }//end: rathread_init() @@ -100,7 +100,7 @@ void rathread_final(void) { // Shouldn't happen .. // Kill 'em all! // - for(i = 1; i < RA_THREADS_MAX; i++){ + for (i = 1; i < THREADS_MAX; i++) { if(l_threads[i].proc != NULL){ ShowWarning("rAthread_final: unterminated Thread (tid %u entryPoint %p) - forcing to terminate (kill)\n", i, l_threads[i].proc); thread->destroy(&l_threads[i]); @@ -110,24 +110,28 @@ void rathread_final(void) { }//end: rathread_final() // gets called whenever a thread terminated .. -static void rat_thread_terminated(rAthread *handle) { +static void rat_thread_terminated(struct thread_handle *handle) +{ // Preserve handle->myID and handle->hThread, set everything else to its default value handle->param = NULL; handle->proc = NULL; - handle->prio = RAT_PRIO_NORMAL; + handle->prio = THREADPRIO_NORMAL; }//end: rat_thread_terminated() #ifdef WIN32 -DWORD WINAPI raThreadMainRedirector(LPVOID p){ +DWORD WINAPI raThreadMainRedirector(LPVOID p) +{ #else -static void *raThreadMainRedirector( void *p ){ +static void *raThreadMainRedirector(void *p) +{ sigset_t set; // on Posix Thread platforms #endif void *ret; + struct thread_handle *self = p; // Update myID @ TLS to right id. #ifdef HAS_TLS - g_rathread_ID = ((rAthread*)p)->myID; + g_rathread_ID = self->myID; #endif #ifndef WIN32 @@ -144,13 +148,13 @@ static void *raThreadMainRedirector( void *p ){ #endif - ret = ((rAthread*)p)->proc( ((rAthread*)p)->param ) ; + ret = self->proc(self->param); #ifdef WIN32 - CloseHandle( ((rAthread*)p)->hThread ); + CloseHandle(self->hThread); #endif - rat_thread_terminated( (rAthread*)p ); + rat_thread_terminated(self); #ifdef WIN32 return (DWORD)ret; #else @@ -161,17 +165,19 @@ static void *raThreadMainRedirector( void *p ){ /// /// API Level /// -rAthread *rathread_create(rAthreadProc entryPoint, void *param) { - return thread->createEx(entryPoint, param, (1<<23) /*8MB*/, RAT_PRIO_NORMAL); +struct thread_handle *rathread_create(threadFunc entryPoint, void *param) +{ + return thread->createEx(entryPoint, param, (1<<23) /*8MB*/, THREADPRIO_NORMAL); }//end: rathread_create() -rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio) { +struct thread_handle *rathread_createEx(threadFunc entryPoint, void *param, size_t szStack, enum thread_priority prio) +{ #ifndef WIN32 pthread_attr_t attr; #endif size_t tmp; unsigned int i; - rAthread *handle = NULL; + struct thread_handle *handle = NULL; // given stacksize aligned to systems pagesize? tmp = szStack % sysinfo->getpagesize(); @@ -179,7 +185,7 @@ rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack szStack += tmp; // Get a free Thread Slot. - for(i = 0; i < RA_THREADS_MAX; i++){ + for (i = 0; i < THREADS_MAX; i++) { if(l_threads[i].proc == NULL){ handle = &l_threads[i]; break; @@ -213,7 +219,8 @@ rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack return handle; }//end: rathread_createEx -void rathread_destroy(rAthread *handle) { +void rathread_destroy(struct thread_handle *handle) +{ #ifdef WIN32 if( TerminateThread(handle->hThread, 0) != FALSE){ CloseHandle(handle->hThread); @@ -230,9 +237,10 @@ void rathread_destroy(rAthread *handle) { #endif }//end: rathread_destroy() -rAthread *rathread_self(void) { +struct thread_handle *rathread_self(void) +{ #ifdef HAS_TLS - rAthread *handle = &l_threads[g_rathread_ID]; + struct thread_handle *handle = &l_threads[g_rathread_ID]; if(handle->proc != NULL) // entry point set, so its used! return handle; @@ -248,7 +256,7 @@ rAthread *rathread_self(void) { hSelf = pthread_self(); #endif - for(i = 0; i < RA_THREADS_MAX; i++){ + for (i = 0; i < THREADS_MAX; i++) { if(l_threads[i].hThread == hSelf && l_threads[i].proc != NULL) return &l_threads[i]; } @@ -272,7 +280,8 @@ int rathread_get_tid(void) { }//end: rathread_get_tid() -bool rathread_wait(rAthread *handle, void **out_exitCode) { +bool rathread_wait(struct thread_handle *handle, void **out_exitCode) +{ // Hint: // no thread data cleanup routine call here! // its managed by the callProxy itself.. @@ -288,12 +297,14 @@ bool rathread_wait(rAthread *handle, void **out_exitCode) { }//end: rathread_wait() -void rathread_prio_set(rAthread *handle, RATHREAD_PRIO prio) { - handle->prio = RAT_PRIO_NORMAL; +void rathread_prio_set(struct thread_handle *handle, enum thread_priority prio) +{ + handle->prio = THREADPRIO_NORMAL; //@TODO }//end: rathread_prio_set() -RATHREAD_PRIO rathread_prio_get(rAthread *handle) { +enum thread_priority rathread_prio_get(struct thread_handle *handle) +{ return handle->prio; }//end: rathread_prio_get() diff --git a/src/common/thread.h b/src/common/thread.h index be34ea830..bde35f738 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -23,14 +23,15 @@ #include "common/hercules.h" -typedef struct rAthread rAthread; -typedef void* (*rAthreadProc)(void*); - -typedef enum RATHREAD_PRIO { - RAT_PRIO_LOW = 0, - RAT_PRIO_NORMAL, - RAT_PRIO_HIGH -} RATHREAD_PRIO; +/* Opaque Types */ +struct thread_handle; ///< Thread handle. +typedef void *(*threadFunc)(void *); ///< Thread entry point function. + +enum thread_priority { + THREADPRIO_LOW = 0, + THREADPRIO_NORMAL, + THREADPRIO_HIGH +}; struct thread_interface { /** @@ -43,7 +44,7 @@ struct thread_interface { * @return The created thread object. * @retval NULL in vase of failure. */ - rAthread *(*create) (rAthreadProc entryPoint, void *param); + struct thread_handle *(*create) (threadFunc entryPoint, void *param); /** * Creates a new Thread (with more creation options). @@ -57,7 +58,7 @@ struct thread_interface { * @return The created thread object. * @retval NULL in case of failure. */ - rAthread *(*createEx) (rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio); + struct thread_handle *(*createEx) (threadFunc entryPoint, void *param, size_t szStack, enum thread_priority prio); /** * Destroys the given Thread immediately. @@ -67,7 +68,7 @@ struct thread_interface { * * @param handle The thread to destroy. */ - void (*destroy) (rAthread *handle); + void (*destroy) (struct thread_handle *handle); /** * Returns the thread handle of the thread calling this function. @@ -82,7 +83,7 @@ struct thread_interface { * @return the thread handle. * @retval NULL in case of failure. */ - rAthread *(*self) (void); + struct thread_handle *(*self) (void); /** * Returns own thread id (TID). @@ -105,22 +106,22 @@ struct thread_interface { * * @retval true if the given thread has been terminated. */ - bool (*wait) (rAthread *handle, void **out_exitCode); + bool (*wait) (struct thread_handle *handle, void **out_exitCode); /** * Sets the given priority in the OS scheduler. * * @param handle The thread to set the priority for. - * @param prio The priority to set (@see enum RATHREAD_PRIO). + * @param prio The priority to set (@see enum thread_priority). */ - void (*prio_set) (rAthread *handle, RATHREAD_PRIO prio); + void (*prio_set) (struct thread_handle *handle, enum thread_priority prio); /** * Gets the current priority of the given thread. * * @param handle The thread to get the priority for. */ - RATHREAD_PRIO (*prio_get) (rAthread *handle); + enum thread_priority (*prio_get) (struct thread_handle *handle); /** * Tells the OS scheduler to yield the execution of the calling thread. diff --git a/src/test/test_spinlock.c b/src/test/test_spinlock.c index cb46b239c..5ea3ee94d 100644 --- a/src/test/test_spinlock.c +++ b/src/test/test_spinlock.c @@ -38,7 +38,7 @@ #define PERINC 100000 #define LOOPS 47 -static SPIN_LOCK lock; +static struct spin_lock lock; static unsigned int val = 0; static volatile int32 done_threads = 0; @@ -60,8 +60,9 @@ static void *worker(void *p){ return NULL; }//end: worker() -int do_init(int argc, char **argv){ - rAthread *t[THRC]; +int do_init(int argc, char **argv) +{ + struct thread_handle *t[THRC]; int j, i; int ok; @@ -78,7 +79,7 @@ int do_init(int argc, char **argv){ InitializeSpinLock(&lock); for(i =0; i < THRC; i++){ - t[i] = thread->createEx(worker, NULL, 1024*512, RAT_PRIO_NORMAL); + t[i] = thread->createEx(worker, NULL, 1024*512, THREADPRIO_NORMAL); } while(1){ |