diff options
-rw-r--r-- | src/common/console.h | 7 | ||||
-rw-r--r-- | src/common/mutex.c | 80 | ||||
-rw-r--r-- | src/common/mutex.h | 26 |
3 files changed, 56 insertions, 57 deletions
diff --git a/src/common/console.h b/src/common/console.h index 57c750a7d..f97e36456 100644 --- a/src/common/console.h +++ b/src/common/console.h @@ -22,12 +22,13 @@ #include "common/hercules.h" #include "common/db.h" -#include "common/mutex.h" #include "common/spinlock.h" #include "common/thread.h" /* Forward Declarations */ struct Sql; // common/sql.h +struct mutex_data; +struct cond_data; /** * Queue Max @@ -74,8 +75,8 @@ struct console_input_interface { SPIN_LOCK ptlock;/* parse thread lock */ rAthread *pthread;/* parse thread */ volatile int32 ptstate;/* parse thread state */ - ramutex *ptmutex;/* parse thread mutex */ - racond *ptcond;/* parse thread cond */ + 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/mutex.c b/src/common/mutex.c index 15c0c5101..a3f0fd4e5 100644 --- a/src/common/mutex.c +++ b/src/common/mutex.c @@ -37,7 +37,7 @@ struct mutex_interface mutex_s; struct mutex_interface *mutex; -struct ramutex{ +struct mutex_data { #ifdef WIN32 CRITICAL_SECTION hMutex; #else @@ -45,15 +45,13 @@ struct ramutex{ #endif }; -struct racond{ +struct cond_data { #ifdef WIN32 HANDLE events[2]; ra_align(8) volatile LONG nWaiters; CRITICAL_SECTION waiters_lock; - #define EVENT_COND_SIGNAL 0 #define EVENT_COND_BROADCAST 1 - #else pthread_cond_t hCond; #endif @@ -65,12 +63,11 @@ struct racond{ // Implementation: // -ramutex *ramutex_create(void) { - struct ramutex *m; - - m = (struct ramutex*)aMalloc( sizeof(struct ramutex) ); +struct mutex_data *ramutex_create(void) +{ + struct mutex_data *m = aMalloc(sizeof(struct mutex_data)); if (m == NULL) { - ShowFatalError("ramutex_create: OOM while allocating %"PRIuS" bytes.\n", sizeof(struct ramutex)); + ShowFatalError("ramutex_create: OOM while allocating %"PRIuS" bytes.\n", sizeof(struct mutex_data)); return NULL; } @@ -83,8 +80,8 @@ ramutex *ramutex_create(void) { return m; }//end: ramutex_create() -void ramutex_destroy(ramutex *m) { - +void ramutex_destroy(struct mutex_data *m) +{ #ifdef WIN32 DeleteCriticalSection(&m->hMutex); #else @@ -92,11 +89,10 @@ void ramutex_destroy(ramutex *m) { #endif aFree(m); - }//end: ramutex_destroy() -void ramutex_lock(ramutex *m) { - +void ramutex_lock(struct mutex_data *m) +{ #ifdef WIN32 EnterCriticalSection(&m->hMutex); #else @@ -104,27 +100,25 @@ void ramutex_lock(ramutex *m) { #endif }//end: ramutex_lock -bool ramutex_trylock(ramutex *m) { +bool ramutex_trylock(struct mutex_data *m) +{ #ifdef WIN32 - if(TryEnterCriticalSection(&m->hMutex) != FALSE) + if (TryEnterCriticalSection(&m->hMutex) != FALSE) return true; - - return false; #else - if(pthread_mutex_trylock(&m->hMutex) == 0) + if (pthread_mutex_trylock(&m->hMutex) == 0) return true; - - return false; #endif + return false; }//end: ramutex_trylock() -void ramutex_unlock(ramutex *m) { +void ramutex_unlock(struct mutex_data *m) +{ #ifdef WIN32 LeaveCriticalSection(&m->hMutex); #else pthread_mutex_unlock(&m->hMutex); #endif - }//end: ramutex_unlock() /////////////// @@ -133,12 +127,11 @@ void ramutex_unlock(ramutex *m) { // Implementation: // -racond *racond_create(void) { - struct racond *c; - - c = (struct racond*)aMalloc( sizeof(struct racond) ); +struct cond_data *racond_create(void) +{ + struct cond_data *c = aMalloc(sizeof(struct cond_data)); if (c == NULL) { - ShowFatalError("racond_create: OOM while allocating %"PRIuS" bytes\n", sizeof(struct racond)); + ShowFatalError("racond_create: OOM while allocating %"PRIuS" bytes\n", sizeof(struct cond_data)); return NULL; } @@ -154,11 +147,12 @@ racond *racond_create(void) { return c; }//end: racond_create() -void racond_destroy(racond *c) { +void racond_destroy(struct cond_data *c) +{ #ifdef WIN32 - CloseHandle( c->events[ EVENT_COND_SIGNAL ] ); - CloseHandle( c->events[ EVENT_COND_BROADCAST ] ); - DeleteCriticalSection( &c->waiters_lock ); + CloseHandle(c->events[EVENT_COND_SIGNAL]); + CloseHandle(c->events[EVENT_COND_BROADCAST]); + DeleteCriticalSection(&c->waiters_lock); #else pthread_cond_destroy(&c->hCond); #endif @@ -166,7 +160,8 @@ void racond_destroy(racond *c) { aFree(c); }//end: racond_destroy() -void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { +void racond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) +{ #ifdef WIN32 register DWORD ms; int result; @@ -176,7 +171,7 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { c->nWaiters++; LeaveCriticalSection(&c->waiters_lock); - if(timeout_ticks < 0) + if (timeout_ticks < 0) ms = INFINITE; else ms = (timeout_ticks > MAXDWORD) ? (MAXDWORD - 1) : (DWORD)timeout_ticks; @@ -190,21 +185,21 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { EnterCriticalSection(&c->waiters_lock); c->nWaiters--; - if( (result == WAIT_OBJECT_0 + EVENT_COND_BROADCAST) && (c->nWaiters == 0) ) + if ((result == WAIT_OBJECT_0 + EVENT_COND_BROADCAST) && (c->nWaiters == 0)) is_last = true; // Broadcast called! LeaveCriticalSection(&c->waiters_lock); // we are the last waiter that has to be notified, or to stop waiting // so we have to do a manual reset - if(is_last == true) + if (is_last == true) ResetEvent( c->events[EVENT_COND_BROADCAST] ); mutex->lock(m); #else - if(timeout_ticks < 0){ - pthread_cond_wait( &c->hCond, &m->hMutex ); - }else{ + if (timeout_ticks < 0) { + pthread_cond_wait(&c->hCond, &m->hMutex); + } else { struct timespec wtime; int64 exact_timeout = timer->gettick() + timeout_ticks; @@ -213,11 +208,11 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { pthread_cond_timedwait( &c->hCond, &m->hMutex, &wtime); } - #endif }//end: racond_wait() -void racond_signal(racond *c) { +void racond_signal(struct cond_data *c) +{ #ifdef WIN32 # if 0 bool has_waiters = false; @@ -234,7 +229,8 @@ void racond_signal(racond *c) { #endif }//end: racond_signal() -void racond_broadcast(racond *c) { +void racond_broadcast(struct cond_data *c) +{ #ifdef WIN32 # if 0 bool has_waiters = false; diff --git a/src/common/mutex.h b/src/common/mutex.h index 54e304add..2365cf4e9 100644 --- a/src/common/mutex.h +++ b/src/common/mutex.h @@ -23,30 +23,32 @@ #include "common/hercules.h" -typedef struct ramutex ramutex; // Mutex -typedef struct racond racond; // Condition Var +/* Opaque types */ +struct mutex_data; ///< Mutex +struct cond_data; ///< Conditional variable +/* Interface */ struct mutex_interface { /** * Creates a Mutex * * @return The created mutex. */ - ramutex *(*create) (void); + struct mutex_data *(*create) (void); /** * Destroys a Mutex. * * @param m the mutex to destroy. */ - void (*destroy) (ramutex *m); + void (*destroy) (struct mutex_data *m); /** * Gets a lock. * * @param m The mutex to lock. */ - void (*lock) (ramutex *m); + void (*lock) (struct mutex_data *m); /** * Tries to get a lock. @@ -56,14 +58,14 @@ struct mutex_interface { * @retval true if the lock was acquired. * @retval false if the mutex couldn't be locked. */ - bool (*trylock) (ramutex *m); + bool (*trylock) (struct mutex_data *m); /** * Unlocks a mutex. * * @param m The mutex to unlock. */ - void (*unlock) (ramutex *m); + void (*unlock) (struct mutex_data *m); /** @@ -71,14 +73,14 @@ struct mutex_interface { * * @return the created condition variable. */ - racond *(*cond_create) (void); + struct cond_data *(*cond_create) (void); /** * Destroys a Condition variable. * * @param c the condition variable to destroy. */ - void (*cond_destroy) (racond *c); + void (*cond_destroy) (struct cond_data *c); /** * Waits Until state is signaled. @@ -87,7 +89,7 @@ struct mutex_interface { * @param m The mutex used for synchronization. * @param timeout_ticks Timeout in ticks (-1 = INFINITE) */ - void (*cond_wait) (racond *c, ramutex *m, sysint timeout_ticks); + void (*cond_wait) (struct cond_data *c, struct mutex_data *m, sysint timeout_ticks); /** * Sets the given condition var to signaled state. @@ -97,7 +99,7 @@ struct mutex_interface { * * @param c Condition var to set in signaled state. */ - void (*cond_signal) (racond *c); + void (*cond_signal) (struct cond_data *c); /** * Sets notifies all waiting threads thats signaled. @@ -107,7 +109,7 @@ struct mutex_interface { * * @param c Condition var to set in signaled state. */ - void (*cond_broadcast) (racond *c); + void (*cond_broadcast) (struct cond_data *c); }; #ifdef HERCULES_CORE |