new SyncObject; refs #8

master
Marc Wäckerlin 14 years ago
parent 19dd88c325
commit 44468b3924
  1. 368
      openssl-act-engine/src/SyncObject.h

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

Loading…
Cancel
Save