2002-08-11 05:49:45 +00:00
|
|
|
|
#ifndef THREADS_H_
|
|
|
|
|
#define THREADS_H_
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// macro DEFAULT_THREADING
|
|
|
|
|
// Selects the default threading model for certain components of Loki
|
|
|
|
|
// If you don't define it, it defaults to single-threaded
|
|
|
|
|
// All classes in Loki have configurable threading model; DEFAULT_THREADING
|
|
|
|
|
// affects only default template arguments
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2005-07-28 14:26:10 +00:00
|
|
|
|
// $Header$
|
2005-07-28 14:04:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(CLASS_LEVEL_THREADING) || defined(OBJECT_LEVEL_THREADING)
|
2005-07-28 14:13:46 +00:00
|
|
|
|
// threads only on windows
|
2005-07-28 14:04:07 +00:00
|
|
|
|
#include <windows.h>
|
2005-07-28 14:13:46 +00:00
|
|
|
|
#define SINGLETON_DEFAULT_THREADING ::Loki::ClassLevelLockable
|
|
|
|
|
#if defined(CLASS_LEVEL_THREADING)
|
|
|
|
|
#define DEFAULT_THREADING ::Loki::ClassLevelLockable
|
|
|
|
|
#else
|
|
|
|
|
#define DEFAULT_THREADING ::Loki::ObjectLevelLockable
|
|
|
|
|
#endif
|
2005-07-28 14:04:07 +00:00
|
|
|
|
#else
|
2005-07-28 14:13:46 +00:00
|
|
|
|
#define DEFAULT_THREADING ::Loki::SingleThreaded
|
2005-07-28 14:04:07 +00:00
|
|
|
|
#endif
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2005-07-26 14:30:17 +00:00
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
namespace Loki
|
|
|
|
|
{
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// class template SingleThreaded
|
|
|
|
|
// Implementation of the ThreadingModel policy used by various classes
|
|
|
|
|
// Implements a single-threaded model; no synchronization
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class Host>
|
|
|
|
|
class SingleThreaded
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct Lock
|
|
|
|
|
{
|
|
|
|
|
Lock() {}
|
2003-02-27 20:09:08 +00:00
|
|
|
|
explicit Lock(const SingleThreaded&) {}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef Host VolatileType;
|
|
|
|
|
|
|
|
|
|
typedef int IntType;
|
|
|
|
|
|
|
|
|
|
static IntType AtomicAdd(volatile IntType& lval, IntType val)
|
|
|
|
|
{ return lval += val; }
|
|
|
|
|
|
|
|
|
|
static IntType AtomicSubtract(volatile IntType& lval, IntType val)
|
|
|
|
|
{ return lval -= val; }
|
|
|
|
|
|
|
|
|
|
static IntType AtomicMultiply(volatile IntType& lval, IntType val)
|
|
|
|
|
{ return lval *= val; }
|
|
|
|
|
|
|
|
|
|
static IntType AtomicDivide(volatile IntType& lval, IntType val)
|
|
|
|
|
{ return lval /= val; }
|
|
|
|
|
|
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval)
|
|
|
|
|
{ return ++lval; }
|
|
|
|
|
|
2002-08-15 02:49:29 +00:00
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval)
|
2002-11-07 18:53:47 +00:00
|
|
|
|
{ return --lval; }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
static void AtomicAssign(volatile IntType & lval, IntType val)
|
|
|
|
|
{ lval = val; }
|
|
|
|
|
|
|
|
|
|
static void AtomicAssign(IntType & lval, volatile IntType & val)
|
|
|
|
|
{ lval = val; }
|
|
|
|
|
};
|
|
|
|
|
|
2005-07-28 14:04:07 +00:00
|
|
|
|
#if defined(_WINDOWS_) || defined(_WINDOWS_H)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// class template ObjectLevelLockable
|
|
|
|
|
// Implementation of the ThreadingModel policy used by various classes
|
|
|
|
|
// Implements a object-level locking scheme
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class Host>
|
|
|
|
|
class ObjectLevelLockable
|
|
|
|
|
{
|
2003-12-02 18:52:18 +00:00
|
|
|
|
mutable CRITICAL_SECTION mtx_;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ObjectLevelLockable()
|
|
|
|
|
{
|
|
|
|
|
::InitializeCriticalSection(&mtx_);
|
|
|
|
|
}
|
2005-07-24 19:19:41 +00:00
|
|
|
|
|
2005-07-24 20:01:54 +00:00
|
|
|
|
ObjectLevelLockable(const ObjectLevelLockable&)
|
2005-07-24 19:19:41 +00:00
|
|
|
|
{
|
|
|
|
|
::InitializeCriticalSection(&mtx_);
|
|
|
|
|
}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
~ObjectLevelLockable()
|
|
|
|
|
{
|
|
|
|
|
::DeleteCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Lock;
|
|
|
|
|
friend class Lock;
|
|
|
|
|
|
|
|
|
|
class Lock
|
|
|
|
|
{
|
2003-12-02 18:52:18 +00:00
|
|
|
|
ObjectLevelLockable const& host_;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
Lock(const Lock&);
|
|
|
|
|
Lock& operator=(const Lock&);
|
|
|
|
|
public:
|
2003-02-27 20:09:08 +00:00
|
|
|
|
|
2003-12-02 18:52:18 +00:00
|
|
|
|
explicit Lock(const ObjectLevelLockable& host) : host_(host)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
|
|
|
|
::EnterCriticalSection(&host_.mtx_);
|
|
|
|
|
}
|
2003-02-27 20:09:08 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
~Lock()
|
|
|
|
|
{
|
|
|
|
|
::LeaveCriticalSection(&host_.mtx_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef volatile Host VolatileType;
|
|
|
|
|
|
|
|
|
|
typedef LONG IntType;
|
|
|
|
|
|
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval)
|
|
|
|
|
{ return InterlockedIncrement(&const_cast<IntType&>(lval)); }
|
|
|
|
|
|
2002-08-15 02:49:29 +00:00
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return InterlockedDecrement(&const_cast<IntType&>(lval)); }
|
|
|
|
|
|
|
|
|
|
static void AtomicAssign(volatile IntType& lval, IntType val)
|
|
|
|
|
{ InterlockedExchange(&const_cast<IntType&>(lval), val); }
|
|
|
|
|
|
|
|
|
|
static void AtomicAssign(IntType& lval, volatile IntType& val)
|
|
|
|
|
{ InterlockedExchange(&lval, val); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class Host>
|
|
|
|
|
class ClassLevelLockable
|
|
|
|
|
{
|
|
|
|
|
struct Initializer
|
2003-02-27 20:09:08 +00:00
|
|
|
|
{
|
|
|
|
|
CRITICAL_SECTION mtx_;
|
2005-07-26 15:11:48 +00:00
|
|
|
|
bool init_;
|
|
|
|
|
|
2005-07-27 17:33:50 +00:00
|
|
|
|
Initializer():init_(false)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2005-07-27 16:32:20 +00:00
|
|
|
|
::InitializeCriticalSection(&mtx_);
|
|
|
|
|
init_=true;
|
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
|
|
|
|
::DeleteCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Initializer initializer_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
class Lock;
|
|
|
|
|
friend class Lock;
|
|
|
|
|
|
|
|
|
|
class Lock
|
|
|
|
|
{
|
|
|
|
|
Lock(const Lock&);
|
|
|
|
|
Lock& operator=(const Lock&);
|
|
|
|
|
public:
|
|
|
|
|
Lock()
|
|
|
|
|
{
|
2005-07-27 16:32:20 +00:00
|
|
|
|
assert(initializer_.init_);
|
2005-07-27 17:33:50 +00:00
|
|
|
|
::EnterCriticalSection(&initializer_.mtx_);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
2003-12-02 18:52:18 +00:00
|
|
|
|
explicit Lock(const ClassLevelLockable&)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2005-07-27 16:32:20 +00:00
|
|
|
|
assert(initializer_.init_);
|
2003-02-27 20:09:08 +00:00
|
|
|
|
::EnterCriticalSection(&initializer_.mtx_);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
~Lock()
|
|
|
|
|
{
|
2005-07-27 16:32:20 +00:00
|
|
|
|
assert(initializer_.init_);
|
2003-02-27 20:09:08 +00:00
|
|
|
|
::LeaveCriticalSection(&initializer_.mtx_);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef volatile Host VolatileType;
|
|
|
|
|
|
|
|
|
|
typedef LONG IntType;
|
|
|
|
|
|
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval)
|
|
|
|
|
{ return InterlockedIncrement(&const_cast<IntType&>(lval)); }
|
|
|
|
|
|
2002-08-15 02:49:29 +00:00
|
|
|
|
static IntType AtomicDecrement(volatile IntType& lval)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return InterlockedDecrement(&const_cast<IntType&>(lval)); }
|
|
|
|
|
|
|
|
|
|
static void AtomicAssign(volatile IntType& lval, IntType val)
|
|
|
|
|
{ InterlockedExchange(&const_cast<IntType&>(lval), val); }
|
|
|
|
|
|
|
|
|
|
static void AtomicAssign(IntType& lval, volatile IntType& val)
|
|
|
|
|
{ InterlockedExchange(&lval, val); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class Host>
|
|
|
|
|
typename ClassLevelLockable<Host>::Initializer
|
|
|
|
|
ClassLevelLockable<Host>::initializer_;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Change log:
|
|
|
|
|
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
|
2005-07-26 14:30:17 +00:00
|
|
|
|
// July 26, 2005: some asserts by Peter K<>mmel
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2005-07-28 14:04:07 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
#endif
|
2005-07-27 17:33:50 +00:00
|
|
|
|
|
2005-07-28 14:26:10 +00:00
|
|
|
|
// $Log$
|
|
|
|
|
// Revision 1.11 2005/07/28 14:26:09 syntheticpp
|
|
|
|
|
// add cvs Header/Log
|
|
|
|
|
//
|
2005-07-28 14:13:46 +00:00
|
|
|
|
|