small changes for Threads; add compile test for Threads.h

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@321 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-10-24 20:35:12 +00:00
parent da979a7e0e
commit 23ade9b6f0
3 changed files with 128 additions and 19 deletions

View file

@ -53,6 +53,7 @@ namespace Loki
{ {
Lock() {} Lock() {}
explicit Lock(const SingleThreaded&) {} explicit Lock(const SingleThreaded&) {}
explicit Lock(const SingleThreaded*) {}
}; };
typedef Host VolatileType; typedef Host VolatileType;
@ -86,12 +87,12 @@ namespace Loki
#if defined(_WINDOWS_) || defined(_WINDOWS_H) #if defined(_WINDOWS_) || defined(_WINDOWS_H)
#define LOKI_THREADS_MUTEX_DECLARATION(x) CRITICAL_SECTION x #define LOKI_THREADS_MUTEX CRITICAL_SECTION
#define LOKI_THREADS_MUTEX_INIT(x) ::InitializeCriticalSection(x) #define LOKI_THREADS_MUTEX_INIT ::InitializeCriticalSection
#define LOKI_THREADS_MUTEX_DELETE(x) ::DeleteCriticalSection(x) #define LOKI_THREADS_MUTEX_DELETE ::DeleteCriticalSection
#define LOKI_THREADS_MUTEX_LOCK(x) ::EnterCriticalSection(x) #define LOKI_THREADS_MUTEX_LOCK ::EnterCriticalSection
#define LOKI_THREADS_MUTEX_UNLOCK(x) ::LeaveCriticalSection(x) #define LOKI_THREADS_MUTEX_UNLOCK ::LeaveCriticalSection
#define LOKI_THREADS_LONG LONG #define LOKI_THREADS_LONG LONG
#define LOKI_THREADS_ATOMIC_FUNCTIONS \ #define LOKI_THREADS_ATOMIC_FUNCTIONS \
static IntType AtomicIncrement(volatile IntType& lval) \ static IntType AtomicIncrement(volatile IntType& lval) \
@ -111,12 +112,12 @@ namespace Loki
#elif defined(_PTHREAD_H) //POSIX threads (pthread.h) #elif defined(_PTHREAD_H) //POSIX threads (pthread.h)
#define LOKI_THREADS_MUTEX_DECLARATION(x) pthread_mutex_t x #define LOKI_THREADS_MUTEX pthread_mutex_t
#define LOKI_THREADS_MUTEX_INIT(x) ::pthread_mutex_init(x,0) #define LOKI_THREADS_MUTEX_INIT(x) ::pthread_mutex_init(x,0)
#define LOKI_THREADS_MUTEX_DELETE(x) ::pthread_mutex_destroy(x) #define LOKI_THREADS_MUTEX_DELETE ::pthread_mutex_destroy
#define LOKI_THREADS_MUTEX_LOCK(x) ::pthread_mutex_lock(x) #define LOKI_THREADS_MUTEX_LOCK ::pthread_mutex_lock
#define LOKI_THREADS_MUTEX_UNLOCK(x) ::pthread_mutex_unlock(x) #define LOKI_THREADS_MUTEX_UNLOCK ::pthread_mutex_unlock
#define LOKI_THREADS_LONG long #define LOKI_THREADS_LONG long
#define LOKI_THREADS_ATOMIC(x) \ #define LOKI_THREADS_ATOMIC(x) \
pthread_mutex_lock(&atomic_mutex_); \ pthread_mutex_lock(&atomic_mutex_); \
@ -124,6 +125,9 @@ namespace Loki
pthread_mutex_unlock(&atomic_mutex_) pthread_mutex_unlock(&atomic_mutex_)
#define LOKI_THREADS_ATOMIC_FUNCTIONS \ #define LOKI_THREADS_ATOMIC_FUNCTIONS \
private: \
static pthread_mutex_t atomic_mutex_; \
public: \
static IntType AtomicIncrement(volatile IntType& lval) \ static IntType AtomicIncrement(volatile IntType& lval) \
{ LOKI_THREADS_ATOMIC( lval++ ); return lval; } \ { LOKI_THREADS_ATOMIC( lval++ ); return lval; } \
\ \
@ -135,9 +139,6 @@ namespace Loki
\ \
static void AtomicAssign(IntType& lval, volatile IntType& val) \ static void AtomicAssign(IntType& lval, volatile IntType& val) \
{ LOKI_THREADS_ATOMIC( lval = val ); } { LOKI_THREADS_ATOMIC( lval = val ); }
static pthread_mutex_t atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
@ -153,7 +154,7 @@ namespace Loki
template <class Host> template <class Host>
class ObjectLevelLockable class ObjectLevelLockable
{ {
LOKI_THREADS_MUTEX_DECLARATION(mtx_); mutable LOKI_THREADS_MUTEX mtx_;
public: public:
ObjectLevelLockable() ObjectLevelLockable()
@ -183,15 +184,21 @@ namespace Loki
LOKI_THREADS_MUTEX_LOCK(&host_.mtx_); LOKI_THREADS_MUTEX_LOCK(&host_.mtx_);
} }
explicit Lock(const ObjectLevelLockable* host) : host_(*host)
{
LOKI_THREADS_MUTEX_LOCK(&host_.mtx_);
}
~Lock() ~Lock()
{ {
LOKI_THREADS_MUTEX_UNLOCK(&host_.mtx_); LOKI_THREADS_MUTEX_UNLOCK(&host_.mtx_);
} }
private: private:
Lock();
Lock(const Lock&); Lock(const Lock&);
Lock& operator=(const Lock&); Lock& operator=(const Lock&);
ObjectLevelLockable const& host_; const ObjectLevelLockable& host_;
}; };
typedef volatile Host VolatileType; typedef volatile Host VolatileType;
@ -201,6 +208,11 @@ namespace Loki
LOKI_THREADS_ATOMIC_FUNCTIONS LOKI_THREADS_ATOMIC_FUNCTIONS
}; };
#if defined(_PTHREAD_H)
template <class Host>
pthread_mutex_t ObjectLevelLockable<Host>::atomic_mutex_(PTHREAD_MUTEX_INITIALIZER);
#endif
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -214,7 +226,7 @@ namespace Loki
{ {
struct Initializer struct Initializer
{ {
LOKI_THREADS_MUTEX_DECLARATION(mtx_); LOKI_THREADS_MUTEX mtx_;
bool init_; bool init_;
Initializer() : init_(false) Initializer() : init_(false)
@ -251,6 +263,12 @@ namespace Loki
assert(initializer_.init_); assert(initializer_.init_);
LOKI_THREADS_MUTEX_LOCK(&initializer_.mtx_); LOKI_THREADS_MUTEX_LOCK(&initializer_.mtx_);
} }
explicit Lock(const ClassLevelLockable*)
{
assert(initializer_.init_);
LOKI_THREADS_MUTEX_LOCK(&initializer_.mtx_);
}
~Lock() ~Lock()
{ {
@ -271,6 +289,11 @@ namespace Loki
}; };
#if defined(_PTHREAD_H)
template <class Host>
pthread_mutex_t ClassLevelLockable<Host>::atomic_mutex_(PTHREAD_MUTEX_INITIALIZER);
#endif
template <class Host> template <class Host>
typename ClassLevelLockable<Host>::Initializer typename ClassLevelLockable<Host>::Initializer
ClassLevelLockable<Host>::initializer_; ClassLevelLockable<Host>::initializer_;
@ -291,6 +314,9 @@ namespace Loki
#endif #endif
// $Log$ // $Log$
// Revision 1.15 2005/10/24 20:35:12 syntheticpp
// small changes for Threads; add compile test for Threads.h
//
// Revision 1.14 2005/10/24 15:05:24 syntheticpp // Revision 1.14 2005/10/24 15:05:24 syntheticpp
// adding support for POSIX threads (pthreads.h), Thanks to Ilya Volvovski // adding support for POSIX threads (pthreads.h), Thanks to Ilya Volvovski
// //

View file

@ -20,7 +20,7 @@
#endif #endif
//#define LOKI_CLASS_LEVEL_THREADING //#define LOKI_CLASS_LEVEL_THREADING
//#define LOKI_OBJECT_LEVEL_THREADING #define LOKI_OBJECT_LEVEL_THREADING
// Some platforms might have difficulty with this // Some platforms might have difficulty with this
// Need to ifdef around those cases. // Need to ifdef around those cases.
@ -41,6 +41,7 @@ Test::tests_type Test::tests;
// is the header inclusion to execute the correspond // is the header inclusion to execute the correspond
// unit test. // unit test.
#include "ThreadsTest.h"
#include "TypelistTest.h" #include "TypelistTest.h"
#include "SequenceTest.h" #include "SequenceTest.h"
#include "TypeManipTest.h" #include "TypeManipTest.h"
@ -112,6 +113,9 @@ return result;
// $Log$ // $Log$
// Revision 1.9 2005/10/24 20:35:12 syntheticpp
// small changes for Threads; add compile test for Threads.h
//
// Revision 1.8 2005/10/06 17:50:14 syntheticpp // Revision 1.8 2005/10/06 17:50:14 syntheticpp
// adding template based list/sequence implementation, should replace LOKI_TYPELIST_, update some files // adding template based list/sequence implementation, should replace LOKI_TYPELIST_, update some files
// //

View file

@ -0,0 +1,79 @@
///////////////////////////////////////////////////////////////////////////////
// Unit Test for Loki
//
// Copyright Peter Kümmel 2005
//
// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef THREADSTEST_H
#define THREADSTEST_H
#include <loki/Threads.h>
#include "UnitTest.h"
namespace ThreadsTestPrivate
{
class SingleLevel : public Loki::SingleThreaded<SingleLevel>
{
int i;
public:
void test()
{
Lock lock0;
Lock lock(*this);
Lock lockThis(this);
i++;
}
};
class ClassLevel : public Loki::ClassLevelLockable<ClassLevel>
{
int i;
public:
void test()
{
Lock lock0;
Lock lock(*this);
Lock lockThis(this);
i++;
}
};
class ObjectLevel : public Loki::ObjectLevelLockable<ObjectLevel>
{
int i;
public:
void test()
{
//Lock lock0_must_not_compile;
Lock lock(*this);
Lock lockThis(this);
i++;
}
};
}
class ThreadsTest : public Test
{
public:
ThreadsTest() : Test("Threads.h") {}
virtual void execute(TestResult &result)
{
printName(result);
bool r = true; // TODO some tests
testAssert("Threads",r,result);
std::cout << '\n';
}
} threadsTest;
#endif