Added Mutex class to Loki. Made it the default policy class for locking.
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@502 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
a36bcf0b79
commit
713d41a7b2
2 changed files with 58 additions and 41 deletions
|
@ -27,7 +27,7 @@ namespace Loki
|
||||||
the mutex type as a LockingPolicy class. The only requirements for a
|
the mutex type as a LockingPolicy class. The only requirements for a
|
||||||
LockingPolicy class are to provide Lock and Unlock methods.
|
LockingPolicy class are to provide Lock and Unlock methods.
|
||||||
*/
|
*/
|
||||||
template < typename SharedObject, typename LockingPolicy >
|
template < typename SharedObject, typename LockingPolicy = Loki::Mutex >
|
||||||
class LockingPtr
|
class LockingPtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -85,7 +85,7 @@ namespace Loki
|
||||||
a const SharedObject instead of a mutuable SharedObject.
|
a const SharedObject instead of a mutuable SharedObject.
|
||||||
@see LockingPtr
|
@see LockingPtr
|
||||||
*/
|
*/
|
||||||
template < typename SharedObject, typename LockingPolicy >
|
template < typename SharedObject, typename LockingPolicy = Loki::Mutex >
|
||||||
class ConstLockingPtr
|
class ConstLockingPtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -144,6 +144,9 @@ namespace Loki
|
||||||
#endif // end file guardian
|
#endif // end file guardian
|
||||||
|
|
||||||
// $Log$
|
// $Log$
|
||||||
|
// Revision 1.5 2006/01/21 01:02:12 rich_sposato
|
||||||
|
// Added Mutex class to Loki. Made it the default policy class for locking.
|
||||||
|
//
|
||||||
// Revision 1.4 2006/01/19 19:34:19 rich_sposato
|
// Revision 1.4 2006/01/19 19:34:19 rich_sposato
|
||||||
// Added ConstLockingPtr class.
|
// Added ConstLockingPtr class.
|
||||||
//
|
//
|
||||||
|
|
|
@ -184,6 +184,28 @@ namespace Loki
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// \class Mutex
|
||||||
|
//
|
||||||
|
/// \ingroup ThreadingGroup
|
||||||
|
/// A simple and portable Mutex. A default policy class for locking objects.
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class Mutex
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Mutex() { LOKI_THREADS_MUTEX_INIT ( &mtx_ ); }
|
||||||
|
~Mutex() { LOKI_THREADS_MUTEX_DELETE( &mtx_ ); }
|
||||||
|
void Lock() { LOKI_THREADS_MUTEX_LOCK ( &mtx_ ); }
|
||||||
|
void Unlock() { LOKI_THREADS_MUTEX_UNLOCK( &mtx_ ); }
|
||||||
|
private:
|
||||||
|
/// Copy-constructor not implemented.
|
||||||
|
Mutex( const Mutex & );
|
||||||
|
/// Copy-assignement operator not implemented.
|
||||||
|
Mutex & operator = ( const Mutex & );
|
||||||
|
LOKI_THREADS_MUTEX mtx_;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#if defined(_WINDOWS_) || defined(_WINDOWS_H) || defined(_PTHREAD_H)
|
#if defined(_WINDOWS_) || defined(_WINDOWS_H) || defined(_PTHREAD_H)
|
||||||
|
|
||||||
|
@ -194,26 +216,17 @@ namespace Loki
|
||||||
/// Implementation of the ThreadingModel policy used by various classes
|
/// Implementation of the ThreadingModel policy used by various classes
|
||||||
/// Implements a object-level locking scheme
|
/// Implements a object-level locking scheme
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
template <class Host>
|
template < class Host, class MutexPolicy = Loki::Mutex >
|
||||||
class ObjectLevelLockable
|
class ObjectLevelLockable
|
||||||
{
|
{
|
||||||
mutable LOKI_THREADS_MUTEX mtx_;
|
mutable volatile MutexPolicy mtx_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ObjectLevelLockable()
|
ObjectLevelLockable() : mtx_() {}
|
||||||
{
|
|
||||||
LOKI_THREADS_MUTEX_INIT(&mtx_);
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjectLevelLockable(const ObjectLevelLockable&)
|
|
||||||
{
|
|
||||||
LOKI_THREADS_MUTEX_INIT(&mtx_);
|
|
||||||
}
|
|
||||||
|
|
||||||
~ObjectLevelLockable()
|
ObjectLevelLockable(const ObjectLevelLockable&) : mtx_() {}
|
||||||
{
|
|
||||||
LOKI_THREADS_MUTEX_DELETE(&mtx_);
|
~ObjectLevelLockable() {}
|
||||||
}
|
|
||||||
|
|
||||||
class Lock;
|
class Lock;
|
||||||
friend class Lock;
|
friend class Lock;
|
||||||
|
@ -227,21 +240,21 @@ namespace Loki
|
||||||
/// Lock object
|
/// Lock object
|
||||||
explicit Lock(const ObjectLevelLockable& host) : host_(host)
|
explicit Lock(const ObjectLevelLockable& host) : host_(host)
|
||||||
{
|
{
|
||||||
LOKI_THREADS_MUTEX_LOCK(&host_.mtx_);
|
host_.mtx_.Lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lock object
|
/// Lock object
|
||||||
explicit Lock(const ObjectLevelLockable* host) : host_(*host)
|
explicit Lock(const ObjectLevelLockable* host) : host_(*host)
|
||||||
{
|
{
|
||||||
LOKI_THREADS_MUTEX_LOCK(&host_.mtx_);
|
host_.mtx_.Lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unlock object
|
/// Unlock object
|
||||||
~Lock()
|
~Lock()
|
||||||
{
|
{
|
||||||
LOKI_THREADS_MUTEX_UNLOCK(&host_.mtx_);
|
host_.mtx_.Unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// private by design of the object level threading
|
/// private by design of the object level threading
|
||||||
Lock();
|
Lock();
|
||||||
|
@ -270,33 +283,31 @@ namespace Loki
|
||||||
/// Implementation of the ThreadingModel policy used by various classes
|
/// Implementation of the ThreadingModel policy used by various classes
|
||||||
/// Implements a class-level locking scheme
|
/// Implements a class-level locking scheme
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
template <class Host>
|
template <class Host, class MutexPolicy = Loki::Mutex >
|
||||||
class ClassLevelLockable
|
class ClassLevelLockable
|
||||||
{
|
{
|
||||||
struct Initializer
|
struct Initializer
|
||||||
{
|
{
|
||||||
LOKI_THREADS_MUTEX mtx_;
|
|
||||||
bool init_;
|
bool init_;
|
||||||
|
volatile MutexPolicy mtx_;
|
||||||
|
|
||||||
Initializer() : init_(false)
|
Initializer() : init_(false), mtx()
|
||||||
{
|
{
|
||||||
LOKI_THREADS_MUTEX_INIT(&mtx_);
|
|
||||||
init_ = true;
|
init_ = true;
|
||||||
}
|
}
|
||||||
~Initializer()
|
~Initializer()
|
||||||
{
|
{
|
||||||
assert(init_);
|
assert(init_);
|
||||||
LOKI_THREADS_MUTEX_DELETE(&mtx_);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static Initializer initializer_;
|
static Initializer initializer_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
class Lock;
|
class Lock;
|
||||||
friend class Lock;
|
friend class Lock;
|
||||||
|
|
||||||
/// \struct Lock
|
/// \struct Lock
|
||||||
/// Lock class to lock on class level
|
/// Lock class to lock on class level
|
||||||
class Lock
|
class Lock
|
||||||
|
@ -307,30 +318,30 @@ namespace Loki
|
||||||
Lock()
|
Lock()
|
||||||
{
|
{
|
||||||
assert(initializer_.init_);
|
assert(initializer_.init_);
|
||||||
LOKI_THREADS_MUTEX_LOCK(&initializer_.mtx_);
|
initializer_.mtx_.Lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lock class
|
/// Lock class
|
||||||
explicit Lock(const ClassLevelLockable&)
|
explicit Lock(const ClassLevelLockable&)
|
||||||
{
|
{
|
||||||
assert(initializer_.init_);
|
assert(initializer_.init_);
|
||||||
LOKI_THREADS_MUTEX_LOCK(&initializer_.mtx_);
|
initializer_.mtx_.Lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lock class
|
/// Lock class
|
||||||
explicit Lock(const ClassLevelLockable*)
|
explicit Lock(const ClassLevelLockable*)
|
||||||
{
|
{
|
||||||
assert(initializer_.init_);
|
assert(initializer_.init_);
|
||||||
LOKI_THREADS_MUTEX_LOCK(&initializer_.mtx_);
|
initializer_.mtx_.Lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unlock class
|
/// Unlock class
|
||||||
~Lock()
|
~Lock()
|
||||||
{
|
{
|
||||||
assert(initializer_.init_);
|
assert(initializer_.init_);
|
||||||
LOKI_THREADS_MUTEX_UNLOCK(&initializer_.mtx_);
|
initializer_.mtx_.Unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Lock(const Lock&);
|
Lock(const Lock&);
|
||||||
Lock& operator=(const Lock&);
|
Lock& operator=(const Lock&);
|
||||||
|
@ -349,9 +360,9 @@ namespace Loki
|
||||||
pthread_mutex_t ClassLevelLockable<Host>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t ClassLevelLockable<Host>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <class Host>
|
template < class Host, class MutexPolicy >
|
||||||
typename ClassLevelLockable<Host>::Initializer
|
typename ClassLevelLockable< Host, MutexPolicy >::Initializer
|
||||||
ClassLevelLockable<Host>::initializer_;
|
ClassLevelLockable< Host, MutexPolicy >::initializer_;
|
||||||
|
|
||||||
#endif // defined(_WINDOWS_) || defined(_WINDOWS_H) || defined(_PTHREAD_H)
|
#endif // defined(_WINDOWS_) || defined(_WINDOWS_H) || defined(_PTHREAD_H)
|
||||||
|
|
||||||
|
@ -369,6 +380,9 @@ namespace Loki
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// $Log$
|
// $Log$
|
||||||
|
// Revision 1.21 2006/01/21 01:02:12 rich_sposato
|
||||||
|
// Added Mutex class to Loki. Made it the default policy class for locking.
|
||||||
|
//
|
||||||
// Revision 1.20 2006/01/16 19:05:09 rich_sposato
|
// Revision 1.20 2006/01/16 19:05:09 rich_sposato
|
||||||
// Added cvs keywords.
|
// Added cvs keywords.
|
||||||
//
|
//
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue