Index: ext/libicpf/src/dmutex.cpp =================================================================== diff -u -N -re39a4610720bbf5b718bac51c635248ca0054844 -r12de61bfcf4d1ffef0339bee2e4c4a152209fd30 --- ext/libicpf/src/dmutex.cpp (.../dmutex.cpp) (revision e39a4610720bbf5b718bac51c635248ca0054844) +++ ext/libicpf/src/dmutex.cpp (.../dmutex.cpp) (revision 12de61bfcf4d1ffef0339bee2e4c4a152209fd30) @@ -7,8 +7,13 @@ /////////////////////////////////////////////////////////////// // debuggable mutex +/** Constructs an unnamed mutex with a given dump context which will receive + * notifications about locking and unlocking of this mutex. + * + * \param[in] pctx - dump context that will receive notifications about lock/unlock + */ d_mutex::d_mutex(dumpctx* pctx) - : mutex() + : mutex(pctx) { const char_t* psz="Unnamed"; m_pszName=new char_t[strlen(psz)+1]; @@ -19,26 +24,39 @@ m_ulLockCount=0; } +/** Constructs a named mutex with a given dump context which will receive + * notifications about locking and unlocking of this mutex. + * + * \param[in] pszStr - name of this mutex (will be used for logging purposes) + * \param[in] pctx - dump context that will receive notifications about lock/unlock + */ d_mutex::d_mutex(const char_t* pszStr, dumpctx* pctx) : - mutex(pszStr) + mutex(pszStr, pctx) { m_pszName=new char_t[strlen(pszStr)+1]; strcpy(m_pszName, pszStr); m_pContext=pctx; } +/** Destructs the object + */ d_mutex::~d_mutex() { delete [] m_pszName; } -bool d_mutex::lock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) +/** Locks this mutex. Takes some parameters that should identify the place in code which + * at which the locking occurs. + * + * \param[in] pszFile - name of the source file in which the locking was requested + * \param[in] ulLine - line of code in the file at which the locking was requested + * \param[in] pszFunction - name of the function in which the locking was requested + */ +void d_mutex::lock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) { assert(m_pContext); - ((mutex*)this)->lock(); - m_ulLockCount++; // log the attempt and lock it @@ -48,10 +66,17 @@ m_pContext->open(sz); m_pContext->close(); - return true; + ((mutex*)this)->lock(); } -bool d_mutex::unlock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) +/** Unlocks this mutex. Takes some parameters that should identify the place in code which + * at which the unlocking occurs. + * + * \param[in] pszFile - name of the source file in which the unlocking was requested + * \param[in] ulLine - line of code in the file at which the unlocking was requested + * \param[in] pszFunction - name of the function in which the unlocking was requested + */ +void d_mutex::unlock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) { assert(m_pContext); @@ -66,8 +91,6 @@ m_pContext->close(); ((mutex*)this)->unlock(); - - return true; } END_ICPF_NAMESPACE Index: ext/libicpf/src/dmutex.h =================================================================== diff -u -N -re39a4610720bbf5b718bac51c635248ca0054844 -r12de61bfcf4d1ffef0339bee2e4c4a152209fd30 --- ext/libicpf/src/dmutex.h (.../dmutex.h) (revision e39a4610720bbf5b718bac51c635248ca0054844) +++ ext/libicpf/src/dmutex.h (.../dmutex.h) (revision 12de61bfcf4d1ffef0339bee2e4c4a152209fd30) @@ -1,3 +1,26 @@ +/*************************************************************************** + * Copyright (C) 2004-2006 by J�zef Starosczyk * + * ixen@draknet.sytes.net * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +/** \file dmutex.h + * \brief Contains mutex class for thread safe access with debugging capabilities. + * \see The mutex class. + */ #ifndef __DMUTEX_H__ #define __DMUTEX_H__ @@ -8,27 +31,36 @@ BEGIN_ICPF_NAMESPACE +/** \brief Class provides the locking and unlocking capabilities for use with threads. + * + * Class is a simple wrapper over the system related thread locking functions. In linux + * those functions are pthread_mutex_* and in windoze the functions related to CRITICAL_SECTION + * structure. + * This class is very similar to the mutex class, with the difference that it allows logging + * of the locking/unlocking allowing easier debugging of the mutexes. Interface is almost + * out-of-the-box replaceable with standard mutex class. + */ class LIBICPF_API d_mutex : public mutex { public: /** \name Construction/destruction */ /**@{*/ - d_mutex(dumpctx* pctx); - d_mutex(const char_t* pszStr, dumpctx* pctx); - ~d_mutex(); + d_mutex(dumpctx* pctx); ///< Constructs an unnamed mutex + d_mutex(const char_t* pszStr, dumpctx* pctx); ///< Constructs a named mutex + virtual ~d_mutex(); ///< Standard destructor /**@}*/ // standard locking /** \name Locking/unlocking */ /**@{*/ - 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); + void lock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction); ///< Locking with logging + void unlock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction); ///< Unlocking with logging /**@}*/ private: - char* m_pszName; - dumpctx* m_pContext; - ulong_t m_ulLockCount; + char* m_pszName; ///< Name of the mutex + dumpctx* m_pContext; ///< Dump context that will receive informations about locking/unlocking + ulong_t m_ulLockCount; ///< Current lock count }; END_ICPF_NAMESPACE Index: ext/libicpf/src/mutex.cpp =================================================================== diff -u -N -r2b72d8a437d5843db8c14f68c8aa8484614d2f5b -r12de61bfcf4d1ffef0339bee2e4c4a152209fd30 --- ext/libicpf/src/mutex.cpp (.../mutex.cpp) (revision 2b72d8a437d5843db8c14f68c8aa8484614d2f5b) +++ ext/libicpf/src/mutex.cpp (.../mutex.cpp) (revision 12de61bfcf4d1ffef0339bee2e4c4a152209fd30) @@ -1,75 +1,111 @@ #include "mutex.h" +#ifdef _WIN32 + #include +#else + #include +#endif BEGIN_ICPF_NAMESPACE +#define m_pcsLock ((CRITICAL_SECTION*)m_pLock) +#define m_pmLock ((pthread_mutex_t*)m_pLock) + +/** Standard constructor. + */ mutex::mutex() { + construct(); +} + +/** Compatibility layer constructor (with d_mutex). Can take a fake dumpctx pointer, although + * does nothing with it. Effectively it is almost the same as standard constructor. + */ +mutex::mutex(void* /*pUnused*/) +{ + 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*/, void* /*pUnused*/) +{ + construct(); +} + +/** Destructs the mutex. + */ +mutex::~mutex() +{ #ifdef _WIN32 - ::InitializeCriticalSection(&m_cs); + ::DeleteCriticalSection(m_pcsLock); + delete m_pcsLock; #else - 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); + pthread_mutex_destroy(m_pmLock); + delete m_pmLock; #endif } -mutex::mutex(const char_t* /*pszStr*/) +/** Performs a construction of this mutex. Used by every constructor to alloc the internal members + * and initialize it. + */ +void mutex::construct() { #ifdef _WIN32 - ::InitializeCriticalSection(&m_cs); + m_pLock=(void*)new CRITICAL_SECTION; + ::InitializeCriticalSection(m_pcsLock); #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_mutex, &mta); + pthread_mutex_init(m_pmLock, &mta); pthread_mutexattr_destroy(&mta); #endif } -mutex::~mutex() +/** Locks this mutex using an underlying, system-dependent locking mechanism. + */ +void mutex::lock() { #ifdef _WIN32 - ::DeleteCriticalSection(&m_cs); + ::EnterCriticalSection(m_pcsLock); #else - pthread_mutex_destroy(&m_mutex); + pthread_mutex_lock(m_pmLock); #endif } - -// standard locking -bool mutex::lock() -{ -#ifdef _WIN32 - ::EnterCriticalSection(&m_cs); - return true; -#else - return pthread_mutex_lock(&m_mutex) == 0; -#endif -} -bool mutex::unlock() +/** Unlocks this mutex using an underlying, system-dependent locking mechanism. + */ +void mutex::unlock() { #ifdef _WIN32 - ::LeaveCriticalSection(&m_cs); - return true; + ::LeaveCriticalSection(m_pcsLock); #else - return pthread_mutex_unlock(&m_mutex) == 0; // return 0 on success + pthread_mutex_unlock(m_pmLock); #endif } -bool mutex::lock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) +/** 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*/) { - return lock(); + lock(); } -bool mutex::unlock(const char_t* pszFile, ulong_t ulLine, const char_t* pszFunction) +/** 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*/) { - return unlock(); + unlock(); } END_ICPF_NAMESPACE Index: ext/libicpf/src/mutex.h =================================================================== diff -u -N -r2b72d8a437d5843db8c14f68c8aa8484614d2f5b -r12de61bfcf4d1ffef0339bee2e4c4a152209fd30 --- ext/libicpf/src/mutex.h (.../mutex.h) (revision 2b72d8a437d5843db8c14f68c8aa8484614d2f5b) +++ ext/libicpf/src/mutex.h (.../mutex.h) (revision 12de61bfcf4d1ffef0339bee2e4c4a152209fd30) @@ -19,16 +19,11 @@ ***************************************************************************/ /** \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" @@ -53,48 +48,33 @@ public: /** \name Construction/destruction */ /**@{*/ - /** \brief Standard constructor - */ - mutex(); - mutex(const char_t* pszStr); + mutex(); ///< Standard constructor + mutex(void* /*pUnused*/); ///< Helper constructor, used as a compatibility layer with d_mutex + mutex(const char_t* /*pszStr*/, void* /*pUnused*/); ///< Helper constructor, used as a compatibility layer with d_mutex - /** \brief Standard destructor - */ - ~mutex(); + virtual ~mutex(); ///< Standard destructor /**@}*/ - // standard locking /** \name Locking/unlocking */ /**@{*/ - - /** \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. - */ - bool 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. - */ - bool unlock(); + void lock(); ///< Locks this mutex + void unlock(); ///< Unlocks this mutex + 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) /**@}*/ - 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); +protected: + void construct(); ///< Helper function - initializes the internal members, used by constructors private: -#ifdef _WIN32 - /// Underlying windows locking structure - CRITICAL_SECTION m_cs; -#else - /// Underlying linux locking structure/handle - pthread_mutex_t m_mutex; -#endif + 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