2002-08-23 07:22:07 +00:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2002-08-11 05:48:53 +00:00
|
|
|
|
// The Loki Library
|
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
|
// This code accompanies the book:
|
|
|
|
|
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
|
|
|
|
|
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
|
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
|
// The author or Addison-Welsey Longman make no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
|
// without express or implied warranty.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2002-08-23 07:22:07 +00:00
|
|
|
|
// Last update: August 9, 2002
|
|
|
|
|
|
|
|
|
|
#ifndef THREADS_INC_
|
|
|
|
|
#define THREADS_INC_
|
2002-08-11 05:48:53 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// 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
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2002-08-23 07:22:07 +00:00
|
|
|
|
#ifdef __WIN32__
|
|
|
|
|
# ifndef _WINDOWS_
|
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
// Last update: June 20, 2001
|
|
|
|
|
|
|
|
|
|
#ifndef DEFAULT_THREADING
|
|
|
|
|
#define DEFAULT_THREADING /**/ ::Loki::SingleThreaded
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
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() {}
|
|
|
|
|
Lock(const Host&) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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; }
|
2003-03-07 08:05:35 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
static void AtomicAssign(volatile IntType & lval, IntType val)
|
|
|
|
|
{ lval = val; }
|
|
|
|
|
|
|
|
|
|
static void AtomicAssign(IntType & lval, volatile IntType & val)
|
|
|
|
|
{ lval = val; }
|
|
|
|
|
};
|
|
|
|
|
|
2002-08-23 07:22:07 +00:00
|
|
|
|
//#ifdef _WINDOWS_ Borland doesn't use this
|
|
|
|
|
#ifdef __WIN32__
|
2002-08-11 05:48:53 +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:48:53 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ObjectLevelLockable()
|
|
|
|
|
{
|
|
|
|
|
::InitializeCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ObjectLevelLockable()
|
|
|
|
|
{
|
|
|
|
|
::DeleteCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Lock;
|
|
|
|
|
friend class Lock;
|
|
|
|
|
|
|
|
|
|
class Lock
|
|
|
|
|
{
|
2003-12-02 18:52:18 +00:00
|
|
|
|
ObjectLevelLockable const& host_;
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
Lock(const Lock&);
|
|
|
|
|
Lock& operator=(const Lock&);
|
|
|
|
|
public:
|
2003-12-02 18:52:18 +00:00
|
|
|
|
Lock(const Host& host) : host_(host)
|
2002-08-11 05:48:53 +00:00
|
|
|
|
{
|
|
|
|
|
::EnterCriticalSection(&host_.mtx_);
|
|
|
|
|
}
|
|
|
|
|
~Lock()
|
|
|
|
|
{
|
|
|
|
|
::LeaveCriticalSection(&host_.mtx_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef volatile Host VolatileType;
|
|
|
|
|
|
|
|
|
|
typedef LONG IntType;
|
|
|
|
|
|
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval)
|
|
|
|
|
{ return InterlockedIncrement(&const_cast<IntType&>(lval)); }
|
2003-03-07 08:05:35 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
static void AtomicAssign(volatile IntType& lval, IntType val)
|
|
|
|
|
{ InterlockedExchange(&const_cast<IntType&>(lval), val); }
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
static void AtomicAssign(IntType& lval, volatile IntType& val)
|
|
|
|
|
{ InterlockedExchange(&lval, val); }
|
|
|
|
|
};
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
template <class Host>
|
|
|
|
|
class ClassLevelLockable
|
|
|
|
|
{
|
|
|
|
|
static CRITICAL_SECTION mtx_;
|
2002-08-23 07:22:07 +00:00
|
|
|
|
static volatile LONG mtx_initialisation_started;
|
|
|
|
|
static volatile LONG mtx_initialisation_done;
|
2002-08-11 05:48:53 +00:00
|
|
|
|
|
2002-08-23 07:22:07 +00:00
|
|
|
|
//### Borland C++ doesn't like this (it is ignored)
|
|
|
|
|
/*
|
2002-08-11 05:48:53 +00:00
|
|
|
|
struct Initializer;
|
|
|
|
|
friend struct Initializer;
|
|
|
|
|
struct Initializer
|
|
|
|
|
{
|
|
|
|
|
Initializer()
|
|
|
|
|
{
|
|
|
|
|
::InitializeCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
~Initializer()
|
|
|
|
|
{
|
|
|
|
|
::DeleteCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
};
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
static Initializer initializer_;
|
2002-08-23 07:22:07 +00:00
|
|
|
|
*/
|
2002-08-11 05:48:53 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
class Lock;
|
|
|
|
|
friend class Lock;
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
|
|
|
|
typedef volatile Host VolatileType;
|
|
|
|
|
|
|
|
|
|
typedef LONG IntType;
|
|
|
|
|
|
|
|
|
|
static IntType AtomicIncrement(volatile IntType& lval)
|
|
|
|
|
{ return InterlockedIncrement(&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); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// used to protect critical section initialisation
|
|
|
|
|
static LONG AtomicExchange(volatile IntType& lval, IntType& new_value) {
|
|
|
|
|
return InterlockedExchange(&lval, new_value);
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
class Lock
|
|
|
|
|
{
|
|
|
|
|
Lock(const Lock&);
|
|
|
|
|
Lock& operator=(const Lock&);
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
|
|
|
|
//### BCB: the initialisation itself must be protected as it is not done during static initialisation
|
|
|
|
|
void initialize_impl(void)
|
|
|
|
|
{
|
|
|
|
|
LONG now = true;
|
|
|
|
|
now = ClassLevelLockable<Host>::AtomicExchange(ClassLevelLockable<Host>::mtx_initialisation_started, now);
|
|
|
|
|
assert(ClassLevelLockable<Host>::mtx_initialisation_started);
|
|
|
|
|
if (!now) {
|
|
|
|
|
assert(!ClassLevelLockable<Host>::mtx_initialisation_done);
|
|
|
|
|
::InitializeCriticalSection(&mtx_);
|
|
|
|
|
ClassLevelLockable<Host>::mtx_initialisation_done = true;
|
|
|
|
|
} else {
|
|
|
|
|
// critical section is just being initialized by other thread
|
|
|
|
|
while (!ClassLevelLockable<Host>::mtx_initialisation_done) {
|
|
|
|
|
Sleep(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
public:
|
|
|
|
|
Lock()
|
|
|
|
|
{
|
2002-08-23 07:22:07 +00:00
|
|
|
|
//### Here's the trick to make it working on BC++B 6.0
|
|
|
|
|
// (because the static Initializer struct is ignored)
|
|
|
|
|
//
|
|
|
|
|
// The critical section isn't deleted by atexit().
|
|
|
|
|
// Shouldn't matter in practise. You may add it here if you want.
|
|
|
|
|
if (!mtx_initialisation_done) {
|
|
|
|
|
initialize_impl();
|
|
|
|
|
}
|
2002-08-11 05:48:53 +00:00
|
|
|
|
::EnterCriticalSection(&mtx_);
|
|
|
|
|
}
|
2003-12-02 18:52:18 +00:00
|
|
|
|
Lock(const Host&)
|
2002-08-11 05:48:53 +00:00
|
|
|
|
{
|
2002-08-23 07:22:07 +00:00
|
|
|
|
if (!mtx_initialisation_done) {
|
|
|
|
|
initialize_impl();
|
|
|
|
|
}
|
2002-08-11 05:48:53 +00:00
|
|
|
|
::EnterCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
~Lock()
|
|
|
|
|
{
|
|
|
|
|
::LeaveCriticalSection(&mtx_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
template <class Host>
|
|
|
|
|
CRITICAL_SECTION ClassLevelLockable<Host>::mtx_;
|
2002-08-23 07:22:07 +00:00
|
|
|
|
|
2002-08-11 05:48:53 +00:00
|
|
|
|
template <class Host>
|
2002-08-23 07:22:07 +00:00
|
|
|
|
LONG volatile ClassLevelLockable<Host>::mtx_initialisation_started;
|
|
|
|
|
template <class Host>
|
|
|
|
|
LONG volatile ClassLevelLockable<Host>::mtx_initialisation_done;
|
|
|
|
|
|
|
|
|
|
//### Borland C++ does like this
|
|
|
|
|
// template <class Host>
|
|
|
|
|
// typename ClassLevelLockable<Host>::Initializer
|
|
|
|
|
// ClassLevelLockable<Host>::initializer_;
|
|
|
|
|
|
|
|
|
|
#endif
|
2002-08-11 05:48:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Change log:
|
|
|
|
|
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
|
|
|
|
|
// January 10, 2002: Fixed bug in AtomicDivide - credit due to Jordi Guerrero
|
2002-08-23 07:22:07 +00:00
|
|
|
|
// July 16, 2002: Ported by Terje Sletteb<65> and Pavel Vozenilek to BCC 5.6
|
2003-03-07 08:05:35 +00:00
|
|
|
|
// March 06, 2003: Removed wrong AtomicDivide - credit due to Arthur Pawlak
|
2002-08-11 05:48:53 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#endif
|