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

View file

@ -20,7 +20,7 @@
#endif
//#define LOKI_CLASS_LEVEL_THREADING
//#define LOKI_OBJECT_LEVEL_THREADING
#define LOKI_OBJECT_LEVEL_THREADING
// Some platforms might have difficulty with this
// Need to ifdef around those cases.
@ -41,6 +41,7 @@ Test::tests_type Test::tests;
// is the header inclusion to execute the correspond
// unit test.
#include "ThreadsTest.h"
#include "TypelistTest.h"
#include "SequenceTest.h"
#include "TypeManipTest.h"
@ -112,6 +113,9 @@ return result;
// $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
// 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