git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@30 7ec92016-0320-0410-acc4-a06ded1c099a
242 lines
6.9 KiB
C++
242 lines
6.9 KiB
C++
head 1.1;
|
||
access;
|
||
symbols;
|
||
locks; strict;
|
||
comment @ * @;
|
||
|
||
|
||
1.1
|
||
date 2002.07.16.22.42.05; author tslettebo; state Exp;
|
||
branches;
|
||
next ;
|
||
|
||
|
||
desc
|
||
@@
|
||
|
||
|
||
1.1
|
||
log
|
||
@Initial commit
|
||
@
|
||
text
|
||
@////////////////////////////////////////////////////////////////////////////////
|
||
// 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.
|
||
////////////////////////////////////////////////////////////////////////////////
|
||
|
||
#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
|
||
////////////////////////////////////////////////////////////////////////////////
|
||
|
||
// 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; }
|
||
|
||
static IntType AtomicDivide(volatile IntType& lval)
|
||
{ return lval /= val; }
|
||
|
||
static void AtomicAssign(volatile IntType & lval, IntType val)
|
||
{ lval = val; }
|
||
|
||
static void AtomicAssign(IntType & lval, volatile IntType & val)
|
||
{ lval = val; }
|
||
};
|
||
|
||
#ifdef _WINDOWS_
|
||
|
||
////////////////////////////////////////////////////////////////////////////////
|
||
// class template ObjectLevelLockable
|
||
// Implementation of the ThreadingModel policy used by various classes
|
||
// Implements a object-level locking scheme
|
||
////////////////////////////////////////////////////////////////////////////////
|
||
|
||
template <class Host>
|
||
class ObjectLevelLockable
|
||
{
|
||
CRITICAL_SECTION mtx_;
|
||
|
||
public:
|
||
ObjectLevelLockable()
|
||
{
|
||
::InitializeCriticalSection(&mtx_);
|
||
}
|
||
|
||
~ObjectLevelLockable()
|
||
{
|
||
::DeleteCriticalSection(&mtx_);
|
||
}
|
||
|
||
class Lock;
|
||
friend class Lock;
|
||
|
||
class Lock
|
||
{
|
||
ObjectLevelLockable& host_;
|
||
|
||
Lock(const Lock&);
|
||
Lock& operator=(const Lock&);
|
||
public:
|
||
Lock(Host& host) : host_(host)
|
||
{
|
||
::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)); }
|
||
|
||
static IntType AtomicDivide(volatile IntType& lval)
|
||
{ 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
|
||
{
|
||
static CRITICAL_SECTION mtx_;
|
||
|
||
struct Initializer;
|
||
friend struct Initializer;
|
||
struct Initializer
|
||
{
|
||
Initializer()
|
||
{
|
||
::InitializeCriticalSection(&mtx_);
|
||
}
|
||
~Initializer()
|
||
{
|
||
::DeleteCriticalSection(&mtx_);
|
||
}
|
||
};
|
||
|
||
static Initializer initializer_;
|
||
|
||
public:
|
||
class Lock;
|
||
friend class Lock;
|
||
|
||
class Lock
|
||
{
|
||
Lock(const Lock&);
|
||
Lock& operator=(const Lock&);
|
||
public:
|
||
Lock()
|
||
{
|
||
::EnterCriticalSection(&mtx_);
|
||
}
|
||
Lock(Host&)
|
||
{
|
||
::EnterCriticalSection(&mtx_);
|
||
}
|
||
~Lock()
|
||
{
|
||
::LeaveCriticalSection(&mtx_);
|
||
}
|
||
};
|
||
|
||
typedef volatile Host VolatileType;
|
||
|
||
typedef LONG IntType;
|
||
|
||
static IntType AtomicIncrement(volatile IntType& lval)
|
||
{ return InterlockedIncrement(&const_cast<IntType&>(lval)); }
|
||
|
||
static IntType AtomicDivide(volatile IntType& lval)
|
||
{ 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>
|
||
CRITICAL_SECTION ClassLevelLockable<Host>::mtx_;
|
||
|
||
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!!!
|
||
// January 10, 2002: Fixed bug in AtomicDivide - credit due to Jordi Guerrero
|
||
// July 16, 2002: Ported by Terje Sletteb<65> to BCC 5.6
|
||
////////////////////////////////////////////////////////////////////////////////
|
||
|
||
#endif
|
||
@
|