threadSafe.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 ** Copyright (c) 1992-2010 VT MAK
00003 ** All rights reserved.
00004 ******************************************************************************/
00005 
00009 
00010 
00011 #ifndef threadSafe_H_
00012 #define threadSafe_H_
00013 #include "lgrUtil.h"
00014 
00015 #include <vlutil/vlMutex.h>
00016 #include <vlutil/vlScopedLock.h>
00017 
00018 namespace MAKLogger
00019 {
00020 
00043 
00044    class DT_DLL_LGRUTIL DtScopedSequenceLock
00045    {
00046    public:
00048       typedef void (*LockFunction)(void*);
00049 
00052       DtScopedSequenceLock(const DtScopedSequenceLock& orig);
00053 
00055       virtual ~DtScopedSequenceLock();
00056 
00057    protected:
00058 
00060       DtScopedSequenceLock(void* object,LockFunction lockCB,LockFunction unlockCB);
00061 
00063       LockFunction myLockCallback;
00064 
00066       LockFunction myUnlockCallback;
00067 
00069       void* myObject;
00070    };
00071 
00072 
00099 
00100    template <typename T>
00101    class DtThreadSafe
00102    {
00103 #ifndef __solaris__
00104    protected:
00105 #else
00106    public:
00107 #endif
00108       class LockedProxy;
00109       class ConstLockedProxy;
00110       friend class SequenceLockImpl;
00111    public:
00112 
00113       DtThreadSafe(const T& value);
00114 
00116       operator T() const;
00117 
00120       LockedProxy operator->();
00121 
00124       ConstLockedProxy operator->() const;
00125 
00127       DtThreadSafe<T>& operator =(const DtThreadSafe<T>& orig);
00128 
00130       DtThreadSafe<T>& operator =(const T& orig);
00131 
00135       DtScopedSequenceLock sequenceLock();
00136 
00137 #ifndef __solaris__
00138    protected:
00139 #else
00140    public:
00141 #endif
00142 
00146       class LockedProxy
00147       {
00148       public:
00150          LockedProxy(T& value,DtMutex& mutex);
00152          LockedProxy(const LockedProxy& orig);
00154          ~LockedProxy();
00155 
00159          T* operator->();
00160 
00161       protected:
00162          T* myValue;
00163          DtMutex& myMutex;
00164          DtScopedLock myLock;
00165       };
00166 
00170       class ConstLockedProxy
00171       {
00172       public:
00174          ConstLockedProxy(const T& value,DtMutex& mutex);
00176          ConstLockedProxy(const ConstLockedProxy& orig);
00178          ~ConstLockedProxy();
00179 
00183          const  T* operator->();
00184 
00185       protected:
00186          const T*  myValue;
00187          DtMutex& myMutex;
00188          DtScopedLock myLock;
00189       };
00190 
00191 
00195       class SequenceLockImpl : public DtScopedSequenceLock
00196       {
00197       public:
00199          SequenceLockImpl(DtThreadSafe* object);
00200 
00202          virtual ~SequenceLockImpl();
00203 
00205          static void lock(void* object);
00206 
00208          static void unlock(void* object);
00209       };
00210 
00212       void lockObject();
00213 
00215       void unlockObject();
00216 
00218       T myValue;
00221       DtScopedLock* myExternalLock;
00222 
00224       DtMutex myLockMutex;
00225 
00227       mutable DtMutex myMutex;
00228    };
00229 
00230 #define threadSafe_inl_
00231 #include "threadSafe.inl"
00232 #undef threadSafe_inl_
00233 
00234 }
00235 
00236 #endif

Document ID: Generated on Tue Oct 11 19:41:03 EDT 2011 from SVN revision 107422
Copyright © 2005-2011 VT MÄK Inc. All Rights Reserved (www.mak.com)