From 909b9d35ff9bdaaac784cf88f669eea0982fdd58 Mon Sep 17 00:00:00 2001 From: hemagx Date: Wed, 17 Feb 2016 15:29:36 +0200 Subject: Interface mutex.c --- src/common/console.c | 20 +++---- src/common/core.c | 2 + src/common/mutex.c | 25 +++++++- src/common/mutex.h | 152 +++++++++++++++++++++++++---------------------- src/plugins/HPMHooking.c | 1 + 5 files changed, 115 insertions(+), 85 deletions(-) diff --git a/src/common/console.c b/src/common/console.c index 0be33e5c3..bca9f36aa 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -475,9 +475,9 @@ void *cThread_main(void *x) { LeaveSpinLock(&console->input->ptlock); } } - ramutex_lock( console->input->ptmutex ); - racond_wait( console->input->ptcond, console->input->ptmutex, -1 ); - ramutex_unlock( console->input->ptmutex ); + mutex->lock(console->input->ptmutex); + mutex->cond_wait(console->input->ptcond, console->input->ptmutex, -1); + mutex->unlock(console->input->ptmutex); } return NULL; @@ -490,19 +490,19 @@ int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { } cinput.count = 0; LeaveSpinLock(&console->input->ptlock); - racond_signal(console->input->ptcond); + mutex->cond_signal(console->input->ptcond); return 0; } void console_parse_final(void) { if( console->input->ptstate ) { InterlockedDecrement(&console->input->ptstate); - racond_signal(console->input->ptcond); + mutex->cond_signal(console->input->ptcond); /* wait for thread to close */ rathread_wait(console->input->pthread, NULL); - racond_destroy(console->input->ptcond); - ramutex_destroy(console->input->ptmutex); + mutex->cond_destroy(console->input->ptcond); + mutex->destroy(console->input->ptmutex); } } void console_parse_init(void) { @@ -512,8 +512,8 @@ void console_parse_init(void) { InitializeSpinLock(&console->input->ptlock); - console->input->ptmutex = ramutex_create(); - console->input->ptcond = racond_create(); + console->input->ptmutex = mutex->create(); + console->input->ptcond = mutex->cond_create(); if( (console->input->pthread = rathread_create(console->input->pthread_main, NULL)) == NULL ){ ShowFatalError("console_parse_init: failed to spawn console_parse thread.\n"); diff --git a/src/common/core.c b/src/common/core.c index 19f2ef0c7..314bdf947 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -41,6 +41,7 @@ # include "common/conf.h" # include "common/ers.h" # include "common/md5calc.h" +# include "common/mutex.h" # include "common/socket.h" # include "common/sql.h" # include "common/thread.h" @@ -257,6 +258,7 @@ void core_defaults(void) { cmdline_defaults(); des_defaults(); #ifndef MINICORE + mutex_defaults(); libconfig_defaults(); sql_defaults(); timer_defaults(); diff --git a/src/common/mutex.c b/src/common/mutex.c index 0f02b153f..15c0c5101 100644 --- a/src/common/mutex.c +++ b/src/common/mutex.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -34,6 +34,9 @@ #include #endif +struct mutex_interface mutex_s; +struct mutex_interface *mutex; + struct ramutex{ #ifdef WIN32 CRITICAL_SECTION hMutex; @@ -181,7 +184,7 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { // we can release the mutex (m) here, cause win's // manual reset events maintain state when used with // SetEvent() - ramutex_unlock(m); + mutex->unlock(m); result = WaitForMultipleObjects(2, c->events, FALSE, ms); @@ -196,7 +199,7 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { if(is_last == true) ResetEvent( c->events[EVENT_COND_BROADCAST] ); - ramutex_lock(m); + mutex->lock(m); #else if(timeout_ticks < 0){ @@ -247,3 +250,19 @@ void racond_broadcast(racond *c) { pthread_cond_broadcast(&c->hCond); #endif }//end: racond_broadcast() + +void mutex_defaults(void) +{ + mutex = &mutex_s; + mutex->create = ramutex_create; + mutex->destroy = ramutex_destroy; + mutex->lock = ramutex_lock; + mutex->trylock = ramutex_trylock; + mutex->unlock = ramutex_unlock; + + mutex->cond_create = racond_create; + mutex->cond_destroy = racond_destroy; + mutex->cond_wait = racond_wait; + mutex->cond_signal = racond_signal; + mutex->cond_broadcast = racond_broadcast; +} diff --git a/src/common/mutex.h b/src/common/mutex.h index e49791493..54e304add 100644 --- a/src/common/mutex.h +++ b/src/common/mutex.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -21,91 +21,99 @@ #ifndef COMMON_MUTEX_H #define COMMON_MUTEX_H -#include "common/cbasetypes.h" +#include "common/hercules.h" typedef struct ramutex ramutex; // Mutex typedef struct racond racond; // Condition Var -#ifdef HERCULES_CORE -/** - * Creates a Mutex - * - * @return not NULL - */ -ramutex *ramutex_create(void); +struct mutex_interface { + /** + * Creates a Mutex + * + * @return The created mutex. + */ + ramutex *(*create) (void); -/** - * Destroys a Mutex - * - * @param m - the mutex to destroy - */ -void ramutex_destroy(ramutex *m); + /** + * Destroys a Mutex. + * + * @param m the mutex to destroy. + */ + void (*destroy) (ramutex *m); -/** - * Gets a lock - * - * @param m - the mutex to lock - */ -void ramutex_lock(ramutex *m); + /** + * Gets a lock. + * + * @param m The mutex to lock. + */ + void (*lock) (ramutex *m); -/** - * Trys to get the Lock - * - * @param m - the mutex try to lock - * - * @return boolean (true = got the lock) - */ -bool ramutex_trylock(ramutex *m); + /** + * Tries to get a lock. + * + * @param m The mutex to try to lock. + * @return success status. + * @retval true if the lock was acquired. + * @retval false if the mutex couldn't be locked. + */ + bool (*trylock) (ramutex *m); -/** - * Unlocks a mutex - * - * @param m - the mutex to unlock - */ -void ramutex_unlock(ramutex *m); + /** + * Unlocks a mutex. + * + * @param m The mutex to unlock. + */ + void (*unlock) (ramutex *m); -/** - * Creates a Condition variable - * - * @return not NULL - */ -racond *racond_create(void); + /** + * Creates a Condition variable. + * + * @return the created condition variable. + */ + racond *(*cond_create) (void); -/** - * Destroy a Condition variable - * - * @param c - the condition variable to destroy - */ -void racond_destroy(racond *c); + /** + * Destroys a Condition variable. + * + * @param c the condition variable to destroy. + */ + void (*cond_destroy) (racond *c); -/** - * Waits Until state is signaled - * - * @param c - the condition var to wait for signaled state - * @param m - the mutex used for synchronization - * @param timeout_ticks - timeout in ticks ( -1 = INFINITE ) - */ -void racond_wait(racond *c, ramutex *m, sysint timeout_ticks); + /** + * Waits Until state is signaled. + * + * @param c The condition var to wait for signaled state. + * @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); -/** - * Sets the given condition var to signaled state - * - * @param c - condition var to set in signaled state. - * - * @note: - * Only one waiter gets notified. - */ -void racond_signal(racond *c); + /** + * Sets the given condition var to signaled state. + * + * @remark + * Only one waiter gets notified. + * + * @param c Condition var to set in signaled state. + */ + void (*cond_signal) (racond *c); -/** - * Sets notifies all waiting threads thats signaled. - * @param c - condition var to set in signaled state - * - * @note: - * All Waiters getting notified. - */ -void racond_broadcast(racond *c); + /** + * Sets notifies all waiting threads thats signaled. + * + * @remark + * All Waiters getting notified. + * + * @param c Condition var to set in signaled state. + */ + void (*cond_broadcast) (racond *c); +}; + +#ifdef HERCULES_CORE +void mutex_defaults(void); #endif // HERCULES_CORE +HPShared struct mutex_interface *mutex; + #endif /* COMMON_MUTEX_H */ diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index be6947754..0b1b67e61 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -112,6 +112,7 @@ #include "common/des.h" #include "common/md5calc.h" #include "common/memmgr.h" +#include "common/mutex.h" #include "common/nullpo.h" #include "common/showmsg.h" #include "common/socket.h" -- cgit v1.2.3-60-g2f50