1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*!
\section terms_of_use Terms of Use
libspl - the simple plugin layer library.
Copyright (C) 2004 Andreas Loeffler and Ren� Stuhr (www.unitedbytes.de).
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\section author Author
Ren� Stuhr (www.unitedbytes.de)
*/
//---------------------------------------------------------------------------------------------------------------------------
/// @file spl_threadlock.h
/// @version 1.0
/// @brief Simple utility locking class.
/// @author Ren� Stuhr
/// @remarks Lock object to make operations thread safe.
//---------------------------------------------------------------------------------------------------------------------------
#ifndef __SPL_THREADLOCK_H__
#define __SPL_THREADLOCK_H__
#ifndef __SPL_TYPEDEFS_H__
#include "spl_typedefs.h"
#endif
#ifndef __SPL_MUTEX_H__
#include "spl_mutex.h"
#endif
#if (SPL_PLATFORM == SPL_PLATFORM_LINUX) || (SPL_PLATFORM == SPL_PLATFORM_HURD) || (SPL_PLATFORM == SPL_PLATFORM_KFREEBSD)
#include <errno.h>
#endif
#if SPL_PLATFORM == SPL_PLATFORM_WIN32
#if defined ( SPL_MAKE_DLL )
#define SPL_API __declspec( dllexport )
#elif defined ( SPL_USE_DLL )
#define SPL_API __declspec( dllimport )
#else
#define SPL_API
#endif
#else
#define SPL_API
#endif
namespace SPL
{
//---------------------------------------------------------------------------------------------------------------------------
/// @brief Thread safe utility class.
/// @ingroup SPL
//---------------------------------------------------------------------------------------------------------------------------
class slcThreadLock
{
//
//-----------------------------------------------------------------------------------------------------------------------
//*********************************************** CON/DESTRUCTION ***********************************************
//-----------------------------------------------------------------------------------------------------------------------
//
public:
//-----------------------------------------------------------------------------------------------------------------------
/// @brief Initialisation constructor.
/// @param a_pMutex [in] Pointer to a slcMutex object for locking (usually "this").
/// @param a_bLock [in] Flag to lock automatically for implicitly or explicitly usage.
//-----------------------------------------------------------------------------------------------------------------------
SPL_API slcThreadLock( slcMutex* a_pMutex, bool a_bLock = false );
//-----------------------------------------------------------------------------------------------------------------------
/// @brief Default destructor.
/// @remarks Clears all open (forgotten) locks for deadlock safety.
//-----------------------------------------------------------------------------------------------------------------------
virtual SPL_API ~slcThreadLock( void );
//
//-----------------------------------------------------------------------------------------------------------------------
//************************************************ MODIFICATION *************************************************
//-----------------------------------------------------------------------------------------------------------------------
//
public:
//-----------------------------------------------------------------------------------------------------------------------
/// @brief Locks a section - If section is already busy then block and wait until it's ready again.
//-----------------------------------------------------------------------------------------------------------------------
virtual void SPL_API Lock( void );
//-----------------------------------------------------------------------------------------------------------------------
/// @brief Locks a section - If section is busy, return without blocking.
/// @return The method returns one of the following values:
/// @retval EBUSY | Section is still locked from another caller.
/// @retval 0 | Section is now locked by current call.
//-----------------------------------------------------------------------------------------------------------------------
virtual long SPL_API TryLock( void );
//-----------------------------------------------------------------------------------------------------------------------
/// @brief Unlocks a section.
//-----------------------------------------------------------------------------------------------------------------------
virtual void SPL_API Unlock( void );
//
//-----------------------------------------------------------------------------------------------------------------------
//************************************************* ATTRIBUTES **************************************************
//-----------------------------------------------------------------------------------------------------------------------
//
private:
slcMutex* m_pMutex; ///< Pointer to mutex object.
long m_lLockCount; ///< Internal lock counter.
};
} // End of namespace SPL
#endif // __SPL_THREADLOCK_H__
|