Index: ext/libicpf/src/mutex.cpp =================================================================== diff -u -r338a33bbdb8c82416f0351408eea3243520784e5 -re794066b77a356d36056bf2541bc7076c4f1887c --- ext/libicpf/src/mutex.cpp (.../mutex.cpp) (revision 338a33bbdb8c82416f0351408eea3243520784e5) +++ ext/libicpf/src/mutex.cpp (.../mutex.cpp) (revision e794066b77a356d36056bf2541bc7076c4f1887c) @@ -1,111 +1,75 @@ #include "mutex.h" -#ifdef _WIN32 - #include -#else - #include -#endif BEGIN_ICPF_NAMESPACE -#ifdef _WIN32 - #define m_pcsLock ((CRITICAL_SECTION*)m_pLock) -#else - #define m_pmLock ((pthread_mutex_t*)m_pLock) -#endif - -/** Standard constructor. - */ -mutex::mutex() : - m_pLock(NULL) +mutex::mutex() { - construct(); -} - -/** Compatibility layer constructor (with d_mutex). Can take a fake dumpctx pointer and a fake mutex name, - * although does nothing with it. Effectively it is almost the same as standard constructor. - */ -mutex::mutex(const char_t* /*pszStr*/) : - m_pLock(NULL) -{ - construct(); -} - -/** Destructs the mutex. - */ -mutex::~mutex() -{ #ifdef _WIN32 - ::DeleteCriticalSection(m_pcsLock); - delete m_pcsLock; + ::InitializeCriticalSection(&m_cs); #else - pthread_mutex_destroy(m_pmLock); - delete m_pmLock; + pthread_mutexattr_t mta; + pthread_mutexattr_init(&mta); +//#warning Recursive mutexes are disabled; Make sure you use them the right way. + pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE_NP); + pthread_mutex_init(&m_mutex, &mta); + + pthread_mutexattr_destroy(&mta); #endif } -/** Performs a construction of this mutex. Used by every constructor to alloc the internal members - * and initialize it. - */ -void mutex::construct() +mutex::mutex(const char_t* /*pszStr*/) { #ifdef _WIN32 - m_pLock=(void*)new CRITICAL_SECTION; - ::InitializeCriticalSection(m_pcsLock); + ::InitializeCriticalSection(&m_cs); #else - m_pLock=(void*)new pthread_mutex_t; - pthread_mutexattr_t mta; pthread_mutexattr_init(&mta); +//#warning Recursive mutexes are disabled; Make sure you use them the right way. pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE_NP); - pthread_mutex_init(m_pmLock, &mta); + pthread_mutex_init(&m_mutex, &mta); pthread_mutexattr_destroy(&mta); #endif } -/** Locks this mutex using an underlying, system-dependent locking mechanism. - */ +mutex::~mutex() +{ +#ifdef _WIN32 + ::DeleteCriticalSection(&m_cs); +#else + pthread_mutex_destroy(&m_mutex); +#endif +} + +// standard locking void mutex::lock() { #ifdef _WIN32 - ::EnterCriticalSection(m_pcsLock); + ::EnterCriticalSection(&m_cs); #else - pthread_mutex_lock(m_pmLock); + pthread_mutex_lock(&m_mutex) == 0; #endif } -/** Unlocks this mutex using an underlying, system-dependent locking mechanism. - */ void mutex::unlock() { #ifdef _WIN32 - ::LeaveCriticalSection(m_pcsLock); + ::LeaveCriticalSection(&m_cs); #else - pthread_mutex_unlock(m_pmLock); + pthread_mutex_unlock(&m_mutex) == 0; // return 0 on success #endif } #ifdef ENABLE_MUTEX_DEBUGGING -/** Locks this mutex using an underlying, system-dependent locking mechanism. - * This is a compatibility layer over d_mutex. This class does not use any of the - * parameters given - they are only to allow seamless migration to/from the d_mutex. - * \see MLOCK and MUNLOCK macros. - */ -void mutex::lock(const char_t* /*pszFile*/, ulong_t /*ulLine*/, const char_t* /*pszFunction*/) +void mutex::lock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) { lock(); } -/** Unlocks this mutex using an underlying, system-dependent locking mechanism. - * This is a compatibility layer over d_mutex. This class does not use any of the - * parameters given - they are only to allow seamless migration to/from the d_mutex. - * \see MLOCK and MUNLOCK macros. - */ -void mutex::unlock(const char_t* /*pszFile*/, ulong_t /*ulLine*/, const char_t* /*pszFunction*/) +void mutex::unlock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) { unlock(); } - #endif END_ICPF_NAMESPACE Index: ext/libicpf/src/mutex.h =================================================================== diff -u -r338a33bbdb8c82416f0351408eea3243520784e5 -re794066b77a356d36056bf2541bc7076c4f1887c --- ext/libicpf/src/mutex.h (.../mutex.h) (revision 338a33bbdb8c82416f0351408eea3243520784e5) +++ ext/libicpf/src/mutex.h (.../mutex.h) (revision e794066b77a356d36056bf2541bc7076c4f1887c) @@ -19,22 +19,27 @@ ***************************************************************************/ /** \file mutex.h * \brief Contains mutex class for thread safe access. - * \see The d_mutex class. */ #ifndef __MUTEX_H__ #define __MUTEX_H__ +#ifdef _WIN32 + #include +#else + #include +#endif + #include "libicpf.h" #include "gen_types.h" BEGIN_ICPF_NAMESPACE -#if defined(ENABLE_MUTEX_DEBUGGING) && defined(DEBUG_MUTEX) +#ifdef _DEBUG_MUTEX #define MLOCK(mutex) (mutex).lock(__FILE__, __LINE__, __FUNCTION__) - #define MUNLOCK(mutex) (mutex).unlock(__FILE__, __LINE__, __FUNCTION__) + #define MUNLOCK(mutex) (mutex).lock(__FILE__, __LINE__, __FUNCTION__) #else #define MLOCK(mutex) (mutex).lock() - #define MUNLOCK(mutex) (mutex).unlock() + #define MUNLOCK(mutex) (mutex).lock() #endif /** \brief Class provides the locking and unlocking capabilities for use with threads. @@ -48,29 +53,49 @@ public: /** \name Construction/destruction */ /**@{*/ - mutex(); ///< Standard constructor - // the constructor below will not be excluded without ENABLE_MUTEX_DEBUGGING, sice it would require - // too much changes throughout the code that once was designed for debugging. - explicit mutex(const char_t* /*pszStr*/); ///< Helper constructor, used as a compatibility layer with d_mutex + /** \brief Standard constructor + */ + mutex(); + explicit mutex(const char_t* pszStr); - virtual ~mutex(); ///< Standard destructor + /** \brief Standard destructor + */ + ~mutex(); /**@}*/ - + + // standard locking /** \name Locking/unlocking */ /**@{*/ - void lock(); ///< Locks this mutex - void unlock(); ///< Unlocks this mutex -#ifdef ENABLE_MUTEX_DEBUGGING - void lock(const char_t* /*pszFile*/, ulong_t /*ulLine*/, const char_t* /*pszFunction*/); ///< Locks this mutex (compatibility layer with d_mutex) - void unlock(const char_t* /*pszFile*/, ulong_t /*ulLine*/, const char_t* /*pszFunction*/); ///< Unlocks this mutex (compatibility layer with d_mutex) -#endif -/**@}*/ -protected: - void construct(); ///< Helper function - initializes the internal members, used by constructors + /** \brief Locks access to some part of code for the current thread + * + * Locks access to some code using the platform specific functions. + * \return True if succeeded or false if not. + * \note The call under windows always return true. + */ + void lock(); + /** \brief Unlock access to some locked part of code + * + * Unlocks access to some code using the platform specific functions. + * \return True if succeeded or false if not. + * \note The call under windows always return true. + */ + void unlock(); +/**@}*/ + +#ifdef ENABLE_MUTEX_DEBUGGING + bool lock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction); + bool unlock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction); +#endif private: - void* m_pLock; ///< Pointer to a system-specific structure used to lock +#ifdef _WIN32 + /// Underlying windows locking structure + CRITICAL_SECTION m_cs; +#else + /// Underlying linux locking structure/handle + pthread_mutex_t m_mutex; +#endif }; END_ICPF_NAMESPACE