2006-01-16 19:05:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The Loki Library
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
// This code accompanies the book:
|
2008-11-10 05:47:06 +00:00
|
|
|
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
|
2006-01-16 19:05:09 +00:00
|
|
|
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
|
2011-09-23 00:46:21 +00:00
|
|
|
// Code covered by the MIT License
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
2006-01-16 19:05:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-10-17 19:49:26 +00:00
|
|
|
#ifndef LOKI_THREADS_INC_
|
|
|
|
#define LOKI_THREADS_INC_
|
2006-01-16 19:05:09 +00:00
|
|
|
|
2006-10-17 19:49:26 +00:00
|
|
|
// $Id$
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2005-07-28 14:04:07 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// @defgroup ThreadingGroup Threading
|
|
|
|
/// Policies to for the threading model:
|
|
|
|
///
|
|
|
|
/// - SingleThreaded
|
|
|
|
/// - ObjectLevelLockable
|
|
|
|
/// - ClassLevelLockable
|
|
|
|
///
|
|
|
|
/// All classes in Loki have configurable threading model.
|
|
|
|
///
|
2008-11-10 05:47:06 +00:00
|
|
|
/// The macro LOKI_DEFAULT_THREADING selects the default
|
|
|
|
/// threading model for certain components of Loki
|
2005-11-03 12:43:55 +00:00
|
|
|
/// (it affects only default template arguments)
|
2008-11-10 05:47:06 +00:00
|
|
|
///
|
2005-11-03 12:43:55 +00:00
|
|
|
/// \par Usage:
|
2008-11-10 05:47:06 +00:00
|
|
|
///
|
2005-11-03 12:43:55 +00:00
|
|
|
/// To use a specific threading model define
|
|
|
|
///
|
|
|
|
/// - nothing, single-theading is default
|
|
|
|
/// - LOKI_OBJECT_LEVEL_THREADING for object-level-threading
|
|
|
|
/// - LOKI_CLASS_LEVEL_THREADING for class-level-threading
|
|
|
|
///
|
|
|
|
/// \par Supported platfroms:
|
|
|
|
///
|
|
|
|
/// - Windows (windows.h)
|
2006-07-03 08:43:35 +00:00
|
|
|
/// - POSIX (pthread.h):
|
2008-11-10 05:47:06 +00:00
|
|
|
/// No recursive mutex support with pthread.
|
|
|
|
/// This means: calling Lock() on a Loki::Mutex twice from the
|
|
|
|
/// same thread before unlocking the mutex deadlocks the system.
|
2006-07-03 08:43:35 +00:00
|
|
|
/// To avoid this redesign your synchronization. See also:
|
|
|
|
/// http://sourceforge.net/tracker/index.php?func=detail&aid=1516182&group_id=29557&atid=396647
|
2005-11-03 12:43:55 +00:00
|
|
|
|
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
#include <cassert>
|
2005-07-28 14:04:07 +00:00
|
|
|
|
2011-06-21 03:32:19 +00:00
|
|
|
#if defined( LOKI_CLASS_LEVEL_THREADING ) || defined( LOKI_OBJECT_LEVEL_THREADING )
|
2005-10-24 15:05:24 +00:00
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL ::Loki::ClassLevelLockable
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
#if defined(LOKI_CLASS_LEVEL_THREADING) && !defined(LOKI_OBJECT_LEVEL_THREADING)
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_DEFAULT_THREADING ::Loki::ClassLevelLockable
|
2005-07-28 14:13:46 +00:00
|
|
|
#else
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_DEFAULT_THREADING ::Loki::ObjectLevelLockable
|
2005-07-28 14:13:46 +00:00
|
|
|
#endif
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2006-07-01 10:26:25 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2008-11-10 05:47:06 +00:00
|
|
|
#include <windows.h>
|
2006-09-08 17:52:58 +00:00
|
|
|
#define LOKI_WINDOWS_H
|
2005-10-24 15:05:24 +00:00
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
2006-09-08 17:52:58 +00:00
|
|
|
#define LOKI_PTHREAD_H
|
2005-10-24 15:05:24 +00:00
|
|
|
#endif
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2005-07-28 14:04:07 +00:00
|
|
|
#else
|
2005-10-24 15:05:24 +00:00
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_DEFAULT_THREADING ::Loki::SingleThreaded
|
2005-10-24 15:05:24 +00:00
|
|
|
#define LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL ::Loki::SingleThreaded
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2005-07-28 14:04:07 +00:00
|
|
|
#endif
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2011-06-21 03:32:19 +00:00
|
|
|
#if !defined( LOKI_DEFAULT_MUTEX )
|
|
|
|
#define LOKI_DEFAULT_MUTEX ::Loki::Mutex
|
2006-01-22 13:37:33 +00:00
|
|
|
#endif
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2011-06-21 03:32:19 +00:00
|
|
|
#if defined( LOKI_WINDOWS_H )
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2006-07-01 10:26:25 +00:00
|
|
|
#define LOKI_THREADS_MUTEX(x) CRITICAL_SECTION (x);
|
|
|
|
#define LOKI_THREADS_MUTEX_INIT(x) ::InitializeCriticalSection (x)
|
|
|
|
#define LOKI_THREADS_MUTEX_DELETE(x) ::DeleteCriticalSection (x)
|
|
|
|
#define LOKI_THREADS_MUTEX_LOCK(x) ::EnterCriticalSection (x)
|
|
|
|
#define LOKI_THREADS_MUTEX_UNLOCK(x) ::LeaveCriticalSection (x)
|
2005-10-24 20:35:12 +00:00
|
|
|
#define LOKI_THREADS_LONG LONG
|
2007-07-01 18:14:46 +00:00
|
|
|
#define LOKI_THREADS_MUTEX_CTOR(x)
|
2005-10-24 15:05:24 +00:00
|
|
|
|
|
|
|
#define LOKI_THREADS_ATOMIC_FUNCTIONS \
|
2009-08-16 20:29:58 +00:00
|
|
|
private: \
|
|
|
|
static CRITICAL_SECTION atomic_mutex_; \
|
|
|
|
public: \
|
2008-11-10 05:47:06 +00:00
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, const IntType val) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
lval *= val; \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, const IntType val) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
lval /= val; \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
2005-10-24 15:05:24 +00:00
|
|
|
static IntType AtomicIncrement(volatile IntType& lval) \
|
2008-11-10 05:47:06 +00:00
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
++lval; \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2005-10-24 15:05:24 +00:00
|
|
|
\
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval) \
|
2008-11-10 05:47:06 +00:00
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
--lval; \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2005-10-24 15:05:24 +00:00
|
|
|
\
|
2011-09-06 23:21:38 +00:00
|
|
|
static IntType AtomicAssign(volatile IntType& lval, const IntType val) \
|
|
|
|
{ \
|
|
|
|
InterlockedExchange(&const_cast<IntType&>(lval), val); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2005-10-24 15:05:24 +00:00
|
|
|
\
|
2011-09-06 23:21:38 +00:00
|
|
|
static IntType AtomicAssign(IntType& lval, volatile const IntType& val) \
|
|
|
|
{ \
|
|
|
|
InterlockedExchange(&lval, val); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2008-11-10 05:47:06 +00:00
|
|
|
\
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
++lval; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
--lval; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicAdd(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
lval += val; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicSubtract(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
lval -= val; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
lval *= val; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::EnterCriticalSection( &atomic_mutex_ ); \
|
|
|
|
lval /= val; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::LeaveCriticalSection( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
}
|
2005-10-24 15:05:24 +00:00
|
|
|
|
2006-09-08 17:52:58 +00:00
|
|
|
#elif defined(LOKI_PTHREAD_H)
|
2005-10-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
2006-07-01 10:26:25 +00:00
|
|
|
#define LOKI_THREADS_MUTEX(x) pthread_mutex_t (x);
|
|
|
|
|
|
|
|
#define LOKI_THREADS_MUTEX_INIT(x) ::pthread_mutex_init(x, 0)
|
|
|
|
|
2007-07-01 18:14:46 +00:00
|
|
|
// define to 1 to enable recursive mutex support
|
|
|
|
#if 0
|
|
|
|
// experimental recursive mutex support
|
|
|
|
#define LOKI_THREADS_MUTEX_CTOR(x) : x(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
|
|
|
|
#else
|
|
|
|
// no recursive mutex support
|
|
|
|
#define LOKI_THREADS_MUTEX_CTOR(x)
|
|
|
|
#endif
|
|
|
|
|
2006-07-01 10:26:25 +00:00
|
|
|
#define LOKI_THREADS_MUTEX_DELETE(x) ::pthread_mutex_destroy (x)
|
|
|
|
#define LOKI_THREADS_MUTEX_LOCK(x) ::pthread_mutex_lock (x)
|
|
|
|
#define LOKI_THREADS_MUTEX_UNLOCK(x) ::pthread_mutex_unlock (x)
|
2005-10-24 20:35:12 +00:00
|
|
|
#define LOKI_THREADS_LONG long
|
2005-10-24 15:05:24 +00:00
|
|
|
|
|
|
|
#define LOKI_THREADS_ATOMIC(x) \
|
|
|
|
pthread_mutex_lock(&atomic_mutex_); \
|
|
|
|
x; \
|
2008-11-10 05:47:06 +00:00
|
|
|
pthread_mutex_unlock(&atomic_mutex_)
|
|
|
|
|
|
|
|
#define LOKI_THREADS_ATOMIC_FUNCTIONS \
|
|
|
|
private: \
|
|
|
|
static pthread_mutex_t atomic_mutex_; \
|
|
|
|
public: \
|
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, const IntType val) \
|
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
lval *= val; \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, const IntType val) \
|
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
lval /= val; \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
2005-10-24 15:05:24 +00:00
|
|
|
static IntType AtomicIncrement(volatile IntType& lval) \
|
2008-11-10 05:47:06 +00:00
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
++lval; \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2005-10-24 15:05:24 +00:00
|
|
|
\
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval) \
|
2008-11-10 05:47:06 +00:00
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
--lval; \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2005-10-24 15:05:24 +00:00
|
|
|
\
|
2011-09-06 23:21:38 +00:00
|
|
|
static IntType AtomicAssign(volatile IntType& lval, const IntType val) \
|
2008-11-10 05:47:06 +00:00
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
lval = val; \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
2005-10-24 15:05:24 +00:00
|
|
|
\
|
2011-09-06 23:21:38 +00:00
|
|
|
static IntType AtomicAssign(IntType& lval, volatile const IntType& val) \
|
2008-11-10 05:47:06 +00:00
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
lval = val; \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
++lval; \
|
|
|
|
matches = ( compare == lval ); \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
--lval; \
|
|
|
|
matches = ( compare == lval ); \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
lval *= val; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
|
|
|
|
{ \
|
|
|
|
::pthread_mutex_lock( &atomic_mutex_ ); \
|
|
|
|
lval /= val; \
|
|
|
|
matches = ( lval == compare ); \
|
|
|
|
::pthread_mutex_unlock( &atomic_mutex_ ); \
|
|
|
|
return lval; \
|
|
|
|
}
|
2005-10-24 15:05:24 +00:00
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
#else // single threaded
|
|
|
|
|
|
|
|
#define LOKI_THREADS_MUTEX(x)
|
2008-11-10 05:47:06 +00:00
|
|
|
#define LOKI_THREADS_MUTEX_INIT(x)
|
|
|
|
#define LOKI_THREADS_MUTEX_DELETE(x)
|
|
|
|
#define LOKI_THREADS_MUTEX_LOCK(x)
|
|
|
|
#define LOKI_THREADS_MUTEX_UNLOCK(x)
|
|
|
|
#define LOKI_THREADS_LONG
|
2007-07-01 18:14:46 +00:00
|
|
|
#define LOKI_THREADS_MUTEX_CTOR(x)
|
2006-01-22 00:32:29 +00:00
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
#endif
|
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Loki
|
|
|
|
{
|
2006-01-21 13:09:45 +00:00
|
|
|
|
2006-01-21 01:02:12 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \class Mutex
|
|
|
|
//
|
|
|
|
/// \ingroup ThreadingGroup
|
|
|
|
/// A simple and portable Mutex. A default policy class for locking objects.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class Mutex
|
|
|
|
{
|
|
|
|
public:
|
2007-07-01 18:14:46 +00:00
|
|
|
Mutex() LOKI_THREADS_MUTEX_CTOR(mtx_)
|
2006-04-15 00:39:50 +00:00
|
|
|
{
|
2006-07-01 10:26:25 +00:00
|
|
|
LOKI_THREADS_MUTEX_INIT(&mtx_);
|
2006-04-15 00:39:50 +00:00
|
|
|
}
|
|
|
|
~Mutex()
|
|
|
|
{
|
2006-07-01 10:26:25 +00:00
|
|
|
LOKI_THREADS_MUTEX_DELETE(&mtx_);
|
2006-04-15 00:39:50 +00:00
|
|
|
}
|
|
|
|
void Lock()
|
|
|
|
{
|
2006-07-01 10:26:25 +00:00
|
|
|
LOKI_THREADS_MUTEX_LOCK(&mtx_);
|
2006-04-15 00:39:50 +00:00
|
|
|
}
|
|
|
|
void Unlock()
|
|
|
|
{
|
2006-07-01 10:26:25 +00:00
|
|
|
LOKI_THREADS_MUTEX_UNLOCK(&mtx_);
|
2006-04-15 00:39:50 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
private:
|
|
|
|
/// Copy-constructor not implemented.
|
2006-07-01 10:26:25 +00:00
|
|
|
Mutex(const Mutex &);
|
2006-01-21 01:02:12 +00:00
|
|
|
/// Copy-assignement operator not implemented.
|
2006-07-01 10:26:25 +00:00
|
|
|
Mutex & operator = (const Mutex &);
|
|
|
|
LOKI_THREADS_MUTEX(mtx_)
|
2006-01-21 01:02:12 +00:00
|
|
|
};
|
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \class SingleThreaded
|
|
|
|
///
|
|
|
|
/// \ingroup ThreadingGroup
|
|
|
|
/// Implementation of the ThreadingModel policy used by various classes
|
|
|
|
/// Implements a single-threaded model; no synchronization
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-22 13:37:33 +00:00
|
|
|
template <class Host, class MutexPolicy = LOKI_DEFAULT_MUTEX>
|
2006-01-22 00:32:29 +00:00
|
|
|
class SingleThreaded
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// \struct Lock
|
|
|
|
/// Dummy Lock class
|
|
|
|
struct Lock
|
|
|
|
{
|
|
|
|
Lock() {}
|
|
|
|
explicit Lock(const SingleThreaded&) {}
|
|
|
|
explicit Lock(const SingleThreaded*) {}
|
|
|
|
};
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
typedef Host VolatileType;
|
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
typedef int IntType;
|
2006-01-22 00:32:29 +00:00
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
static IntType AtomicAdd(volatile IntType& lval, const IntType val)
|
2006-01-22 00:32:29 +00:00
|
|
|
{ return lval += val; }
|
2008-11-10 05:47:06 +00:00
|
|
|
|
|
|
|
static IntType AtomicSubtract(volatile IntType& lval, const IntType val)
|
2006-01-22 00:32:29 +00:00
|
|
|
{ return lval -= val; }
|
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, const IntType val)
|
2006-01-22 00:32:29 +00:00
|
|
|
{ return lval *= val; }
|
2008-11-10 05:47:06 +00:00
|
|
|
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, const IntType val)
|
2006-01-22 00:32:29 +00:00
|
|
|
{ return lval /= val; }
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
static IntType AtomicIncrement(volatile IntType& lval)
|
|
|
|
{ return ++lval; }
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
static IntType AtomicDecrement(volatile IntType& lval)
|
|
|
|
{ return --lval; }
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2011-09-06 23:21:38 +00:00
|
|
|
static IntType AtomicAssign(volatile IntType & lval, const IntType val)
|
|
|
|
{
|
|
|
|
lval = val;
|
|
|
|
return lval;
|
|
|
|
}
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2011-09-06 23:21:38 +00:00
|
|
|
static IntType AtomicAssign(IntType & lval, volatile IntType & val)
|
|
|
|
{
|
|
|
|
lval = val;
|
|
|
|
return lval;
|
|
|
|
}
|
2008-11-10 05:47:06 +00:00
|
|
|
|
|
|
|
static IntType AtomicAdd(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
|
|
|
|
{
|
|
|
|
lval += val;
|
|
|
|
matches = ( lval == compare );
|
|
|
|
return lval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntType AtomicSubtract(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
|
|
|
|
{
|
|
|
|
lval -= val;
|
|
|
|
matches = ( lval == compare );
|
|
|
|
return lval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
|
|
|
|
{
|
|
|
|
lval *= val;
|
|
|
|
matches = ( lval == compare );
|
|
|
|
return lval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
|
|
|
|
{
|
|
|
|
lval /= val;
|
|
|
|
matches = ( lval == compare );
|
|
|
|
return lval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval, const IntType compare, bool & matches )
|
|
|
|
{
|
|
|
|
++lval;
|
|
|
|
matches = ( lval == compare );
|
|
|
|
return lval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval, const IntType compare, bool & matches )
|
|
|
|
{
|
|
|
|
--lval;
|
|
|
|
matches = ( lval == compare );
|
|
|
|
return lval;
|
|
|
|
}
|
|
|
|
|
2006-01-22 00:32:29 +00:00
|
|
|
};
|
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
|
|
|
|
#if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H)
|
2005-07-31 14:00:48 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \class ObjectLevelLockable
|
|
|
|
///
|
|
|
|
/// \ingroup ThreadingGroup
|
|
|
|
/// Implementation of the ThreadingModel policy used by various classes
|
|
|
|
/// Implements a object-level locking scheme
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-22 13:37:33 +00:00
|
|
|
template < class Host, class MutexPolicy = LOKI_DEFAULT_MUTEX >
|
2002-08-11 05:49:45 +00:00
|
|
|
class ObjectLevelLockable
|
|
|
|
{
|
2006-01-21 01:05:44 +00:00
|
|
|
mutable MutexPolicy mtx_;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
public:
|
2006-01-21 01:02:12 +00:00
|
|
|
ObjectLevelLockable() : mtx_() {}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2006-01-21 01:02:12 +00:00
|
|
|
ObjectLevelLockable(const ObjectLevelLockable&) : mtx_() {}
|
|
|
|
|
|
|
|
~ObjectLevelLockable() {}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
class Lock;
|
|
|
|
friend class Lock;
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// \struct Lock
|
|
|
|
/// Lock class to lock on object level
|
2002-08-11 05:49:45 +00:00
|
|
|
class Lock
|
2008-11-10 05:47:06 +00:00
|
|
|
{
|
2002-08-11 05:49:45 +00:00
|
|
|
public:
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Lock object
|
2003-12-02 18:52:18 +00:00
|
|
|
explicit Lock(const ObjectLevelLockable& host) : host_(host)
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
2006-01-21 01:02:12 +00:00
|
|
|
host_.mtx_.Lock();
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2003-02-27 20:09:08 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Lock object
|
2005-10-30 14:03:23 +00:00
|
|
|
explicit Lock(const ObjectLevelLockable* host) : host_(*host)
|
2005-10-24 20:35:12 +00:00
|
|
|
{
|
2006-01-21 01:02:12 +00:00
|
|
|
host_.mtx_.Lock();
|
2005-10-24 20:35:12 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Unlock object
|
2002-08-11 05:49:45 +00:00
|
|
|
~Lock()
|
|
|
|
{
|
2006-01-21 01:02:12 +00:00
|
|
|
host_.mtx_.Unlock();
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
private:
|
2005-11-03 12:43:55 +00:00
|
|
|
/// private by design of the object level threading
|
2005-10-30 14:03:23 +00:00
|
|
|
Lock();
|
2005-10-24 15:05:24 +00:00
|
|
|
Lock(const Lock&);
|
|
|
|
Lock& operator=(const Lock&);
|
2005-10-24 20:35:12 +00:00
|
|
|
const ObjectLevelLockable& host_;
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef volatile Host VolatileType;
|
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
typedef LOKI_THREADS_LONG IntType;
|
|
|
|
|
|
|
|
LOKI_THREADS_ATOMIC_FUNCTIONS
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
2005-10-24 20:35:12 +00:00
|
|
|
|
2006-09-08 17:52:58 +00:00
|
|
|
#ifdef LOKI_PTHREAD_H
|
2006-01-21 14:11:09 +00:00
|
|
|
template <class Host, class MutexPolicy>
|
|
|
|
pthread_mutex_t ObjectLevelLockable<Host, MutexPolicy>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
|
2005-10-24 20:35:12 +00:00
|
|
|
#endif
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \class ClassLevelLockable
|
|
|
|
///
|
|
|
|
/// \ingroup ThreadingGroup
|
|
|
|
/// Implementation of the ThreadingModel policy used by various classes
|
|
|
|
/// Implements a class-level locking scheme
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-22 13:37:33 +00:00
|
|
|
template <class Host, class MutexPolicy = LOKI_DEFAULT_MUTEX >
|
2002-08-11 05:49:45 +00:00
|
|
|
class ClassLevelLockable
|
|
|
|
{
|
|
|
|
struct Initializer
|
2008-11-10 05:47:06 +00:00
|
|
|
{
|
2009-02-02 07:45:50 +00:00
|
|
|
|
|
|
|
/// This function provides a Scott-Meyers type of Singleton as the initializer
|
|
|
|
/// for the shared mutex.
|
|
|
|
static Initializer & GetIt( void )
|
|
|
|
{
|
|
|
|
static Initializer initializer_;
|
|
|
|
return initializer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsInit( void ) { return init_; }
|
|
|
|
inline MutexPolicy & GetMutex( void ) { return mtx_; }
|
|
|
|
|
|
|
|
private:
|
2005-07-26 15:11:48 +00:00
|
|
|
bool init_;
|
2006-01-21 01:05:44 +00:00
|
|
|
MutexPolicy mtx_;
|
2005-07-26 15:11:48 +00:00
|
|
|
|
2006-01-21 14:11:09 +00:00
|
|
|
Initializer() : init_(false), mtx_()
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
2005-10-24 15:05:24 +00:00
|
|
|
init_ = true;
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2006-07-01 10:26:25 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
~Initializer()
|
|
|
|
{
|
2005-07-27 16:32:20 +00:00
|
|
|
assert(init_);
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2009-02-02 07:45:50 +00:00
|
|
|
Initializer( const Initializer & );
|
|
|
|
Initializer & operator = ( const Initializer & );
|
|
|
|
};
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
public:
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
class Lock;
|
|
|
|
friend class Lock;
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// \struct Lock
|
|
|
|
/// Lock class to lock on class level
|
2002-08-11 05:49:45 +00:00
|
|
|
class Lock
|
2008-11-10 05:47:06 +00:00
|
|
|
{
|
2002-08-11 05:49:45 +00:00
|
|
|
public:
|
2005-11-03 12:43:55 +00:00
|
|
|
|
|
|
|
/// Lock class
|
2002-08-11 05:49:45 +00:00
|
|
|
Lock()
|
|
|
|
{
|
2009-02-02 07:45:50 +00:00
|
|
|
Initializer & initializer = Initializer::GetIt();
|
|
|
|
assert( initializer.IsInit() );
|
|
|
|
initializer.GetMutex().Lock();
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Lock class
|
2003-12-02 18:52:18 +00:00
|
|
|
explicit Lock(const ClassLevelLockable&)
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
2009-02-02 07:45:50 +00:00
|
|
|
Initializer & initializer = Initializer::GetIt();
|
|
|
|
assert( initializer.IsInit() );
|
|
|
|
initializer.GetMutex().Lock();
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Lock class
|
2005-10-30 14:03:23 +00:00
|
|
|
explicit Lock(const ClassLevelLockable*)
|
2005-10-24 20:35:12 +00:00
|
|
|
{
|
2009-02-02 07:45:50 +00:00
|
|
|
Initializer & initializer = Initializer::GetIt();
|
|
|
|
assert( initializer.IsInit() );
|
|
|
|
initializer.GetMutex().Lock();
|
2005-10-24 20:35:12 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
/// Unlock class
|
2002-08-11 05:49:45 +00:00
|
|
|
~Lock()
|
|
|
|
{
|
2009-02-02 07:45:50 +00:00
|
|
|
Initializer & initializer = Initializer::GetIt();
|
|
|
|
assert( initializer.IsInit() );
|
|
|
|
initializer.GetMutex().Unlock();
|
2002-08-11 05:49:45 +00:00
|
|
|
}
|
2006-01-21 01:02:12 +00:00
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
private:
|
|
|
|
Lock(const Lock&);
|
|
|
|
Lock& operator=(const Lock&);
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef volatile Host VolatileType;
|
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
typedef LOKI_THREADS_LONG IntType;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
LOKI_THREADS_ATOMIC_FUNCTIONS
|
2008-11-10 05:47:06 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
2005-10-24 15:05:24 +00:00
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
#ifdef LOKI_PTHREAD_H
|
2006-01-21 14:11:09 +00:00
|
|
|
template <class Host, class MutexPolicy>
|
|
|
|
pthread_mutex_t ClassLevelLockable<Host, MutexPolicy>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
|
2005-10-24 20:35:12 +00:00
|
|
|
#endif
|
|
|
|
|
2008-11-10 05:47:06 +00:00
|
|
|
#endif // #if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H)
|
|
|
|
|
2005-10-24 15:05:24 +00:00
|
|
|
} // namespace Loki
|
|
|
|
|
|
|
|
|
2006-10-17 19:30:40 +00:00
|
|
|
#endif // end file guardian
|
2005-07-28 14:13:46 +00:00
|
|
|
|