You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
3.9 KiB
185 lines
3.9 KiB
15 years ago
|
//////////////////////////////////////////////////////////////////////////
|
||
|
// Name: SyncObject.h
|
||
|
// Product: cv act library
|
||
|
// Purpose: Multithreading synchronization primitive for multiple Operating Systems.
|
||
|
//
|
||
|
// Copyright: (c) 2009 cv cryptovision GmbH
|
||
|
// all rights reserved
|
||
|
// Licence: The conditions for the use of this software are regulated
|
||
|
// in the cv act library licence agreement.
|
||
|
//
|
||
|
// Autor: Markus Tesche
|
||
|
// Date: 04/23/2009
|
||
|
//////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
#ifndef SyncObject_h
|
||
|
#define SyncObject_h
|
||
|
|
||
|
#include "actBasics.h"
|
||
|
|
||
|
#if defined(ACT_WIN32_WCE)
|
||
|
# include "SyncObjectWinCE.h"
|
||
|
|
||
|
#elif defined(ACT_WIN32)
|
||
|
# include "SyncObjectWin32.h"
|
||
|
|
||
|
#elif defined(ACT_SOLARIS)
|
||
|
# include "SyncObjectSloaris.h"
|
||
|
|
||
|
#elif defined(ACT_MACOSX)
|
||
|
# include "SyncObjectMacOS.h"
|
||
|
|
||
|
// posix has always to be last checked !
|
||
|
#elif defined(ACT_POSIX)
|
||
|
# include "SyncObjectPosix.h"
|
||
|
|
||
|
#else
|
||
|
# error SyncObject not implemented for this system
|
||
|
|
||
|
#endif
|
||
|
|
||
|
#include "actHandle.h"
|
||
|
#include "actISynchronize.h"
|
||
|
|
||
|
namespace act
|
||
|
{
|
||
|
//
|
||
|
// Synchronizeable<>
|
||
|
template
|
||
|
<
|
||
|
typename BaseT, /* ISynchronize or derived */
|
||
|
typename SyncObjectT = SyncObject
|
||
|
>
|
||
|
class Synchronizeable : public BaseT
|
||
|
{
|
||
|
public:
|
||
|
typedef SyncObjectT SyncObject;
|
||
|
typedef ValueHandle<SyncObjectT> SyncHandle;
|
||
|
|
||
|
protected:
|
||
|
//
|
||
|
// ISynchronize methods
|
||
|
virtual void Lock() { syncObject().lock(); }
|
||
|
virtual void Unlock() { syncObject().unlock(); }
|
||
|
virtual long LockCount() const { return syncObject().lockCount(); }
|
||
|
|
||
|
virtual const Handle& syncHandle() const { return m_sync_handle; }
|
||
|
|
||
|
protected:
|
||
|
SyncObjectT& syncObject() const { return m_sync_handle.valueRef(); }
|
||
|
|
||
|
protected:
|
||
|
ValueHandle<SyncObjectT> m_sync_handle;
|
||
|
};
|
||
|
|
||
|
//
|
||
|
// GuardT<>
|
||
|
template
|
||
|
<
|
||
|
class SyncObjectT,
|
||
|
class BaseT = void,
|
||
|
class TypeT = void
|
||
|
>
|
||
|
class GuardT;
|
||
|
|
||
|
//
|
||
|
// GuardT<>
|
||
|
template<class SyncObjectT>
|
||
|
class GuardT<SyncObjectT, void, void>
|
||
|
{
|
||
|
public:
|
||
|
typedef SyncObjectT SyncObject;
|
||
|
|
||
|
private:
|
||
|
GuardT(const GuardT&);
|
||
|
GuardT& operator=(const GuardT&);
|
||
|
|
||
|
public:
|
||
|
GuardT(SyncObjectT& sync_object)
|
||
|
: m_sync_object(sync_object)
|
||
|
{
|
||
|
m_sync_object.lock();
|
||
|
}
|
||
|
|
||
|
GuardT(const Handle& sync_handle)
|
||
|
: m_sync_object(sync_handle.requiredAs<SyncObjectT>())
|
||
|
{
|
||
|
m_sync_object.lock();
|
||
|
}
|
||
|
|
||
|
GuardT(const ValueHandle<SyncObjectT>& sync_handle)
|
||
|
: m_sync_object(sync_handle.valueRef())
|
||
|
{
|
||
|
m_sync_object.lock();
|
||
|
}
|
||
|
|
||
|
~GuardT()
|
||
|
{
|
||
|
m_sync_object.unlock();
|
||
|
}
|
||
|
|
||
|
long LockCount() const { return m_sync_object.lockCount(); }
|
||
|
|
||
|
protected:
|
||
|
SyncObjectT& m_sync_object;
|
||
|
};
|
||
|
|
||
|
//
|
||
|
// GuardT<>
|
||
|
template<class SyncObjectT>
|
||
|
class GuardT<SyncObjectT, ISynchronize, void>
|
||
|
{
|
||
|
public:
|
||
|
typedef SyncObjectT SyncObject;
|
||
|
|
||
|
private:
|
||
|
GuardT(const GuardT&);
|
||
|
GuardT& operator=(const GuardT&);
|
||
|
|
||
|
public:
|
||
|
GuardT(ISynchronize& synchronize)
|
||
|
: m_guard(synchronize.syncHandle())
|
||
|
{ }
|
||
|
|
||
|
GuardT(const ISynchronize& synchronize)
|
||
|
: m_guard(synchronize.syncHandle())
|
||
|
{ }
|
||
|
|
||
|
long LockCount() const { return m_guard.LockCount(); }
|
||
|
|
||
|
protected:
|
||
|
GuardT<SyncObjectT> m_guard;
|
||
|
};
|
||
|
|
||
|
//
|
||
|
// GuardT<>
|
||
|
template<class SyncObjectT, class TypeT>
|
||
|
class GuardT<SyncObjectT, ISynchronize, TypeT>
|
||
|
: public GuardT<SyncObjectT, ISynchronize, void>
|
||
|
{
|
||
|
public:
|
||
|
GuardT(TypeT& synchronize)
|
||
|
: GuardT<SyncObjectT, ISynchronize, void>(synchronize)
|
||
|
, m_synchronized(synchronize)
|
||
|
{ }
|
||
|
|
||
|
TypeT* operator->() const { return &m_synchronized; }
|
||
|
|
||
|
protected:
|
||
|
TypeT& m_synchronized;
|
||
|
};
|
||
|
|
||
|
|
||
|
typedef GuardT<SyncObject> Guard;
|
||
|
typedef GuardT<SyncObject, ISynchronize> Synchronize;
|
||
|
|
||
|
template<typename TypeT>
|
||
|
GuardT<SyncObject, ISynchronize, TypeT> Synchronized(TypeT& synchronize)
|
||
|
{
|
||
|
return GuardT<SyncObject, ISynchronize, TypeT>(synchronize);
|
||
|
}
|
||
|
|
||
|
} // namespace act
|
||
|
|
||
|
#endif // SyncObject_h
|