Locks.h

Go to the documentation of this file.
00001 
00002 //Luke Lenhart, 2007
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #include "Types.h"
00006 
00007 #ifndef LOCKS_H_INCLUDED
00008 #define LOCKS_H_INCLUDED
00009 
00010 namespace MPMA
00011 {
00012 
00013     //  -- Atomic Operations safe for multiprocessor operations
00014 
00016     void AtomicIntInc(volatile uint *pint);
00017 
00019     void AtomicIntDec(volatile uint *pint);
00020 
00022     int AtomicIntAdd(volatile int *pint, int addValue);
00023 
00025     bool AtomicCompareExchange(volatile uint *pInt, uint expectedValue, uint newValue, volatile uint &outResultValue);
00026 
00027 
00028     // -- Locking constructs
00029 
00032     class RWSleepLock
00033     {
00034         friend class TakeRWSleepLock;
00035 
00036     public:
00037         RWSleepLock();
00038         ~RWSleepLock();
00039 
00040     protected:
00041         uint cntRead, cntWrite; //lock count for read/write access
00042         uint cntWritePend, cntReadPend; //pending access counter
00043 
00044         //you cannot duplicate this
00045         RWSleepLock(const RWSleepLock&);
00046         const RWSleepLock& operator=(const RWSleepLock&);
00047     };
00048 
00050     class TakeRWSleepLock
00051     {
00052     public:
00054         inline TakeRWSleepLock(RWSleepLock &rwsSection, bool writeAccess=true, bool takeNow=true): crit((RWSleepLock&)rwsSection), meIn(false)
00055             {if (takeNow) Take(writeAccess); }
00056         inline ~TakeRWSleepLock()
00057             {  if (meIn) Leave(); }
00058 
00060         void Take(bool writeAccess);
00062         void Leave();
00063 
00064     private:
00065         mutable RWSleepLock &crit;
00066 
00067         bool meIn;
00068         bool meWrite;
00069 
00070         //you cannot duplicate this
00071         TakeRWSleepLock(const TakeRWSleepLock&);
00072         const TakeRWSleepLock& operator=(const TakeRWSleepLock&);
00073     };
00074 
00075 
00077     class MutexLock
00078     {
00079         friend class TakeMutexLock;
00080 
00081     public:
00082         MutexLock();
00083         ~MutexLock();
00084 
00085     protected:
00086         void *crit;
00087         bool isMemManaged; //special case, since memory manager uses us
00088 
00089         //you cannot duplicate this
00090         MutexLock(const MutexLock&);
00091         const MutexLock& operator=(const MutexLock&);
00092     };
00093 
00095     class TakeMutexLock
00096     {
00097     public:
00099         TakeMutexLock(MutexLock &critSection);
00100         ~TakeMutexLock();
00101 
00102     private:
00103         MutexLock &crit;
00104 
00105         //you cannot duplicate this
00106         TakeMutexLock(const TakeMutexLock&);
00107         const TakeMutexLock& operator=(const TakeMutexLock&);
00108     };
00109 
00110 
00112     class SpinLock
00113     {
00114         friend class TakeSpinLock;
00115     protected:
00116         volatile uint taken;
00117         bool collision;
00118     public:
00119         inline SpinLock(): taken(0), collision(false) {}
00120 
00121         //you cannot duplicate this
00122         SpinLock(const SpinLock&);
00123         const SpinLock& operator=(const SpinLock&);
00124     };
00125 
00127     class TakeSpinLock
00128     {
00129     public:
00131         inline TakeSpinLock(SpinLock &slock, bool takeNow=true): locker(slock), taken(false)
00132             { if (takeNow) Take(); }
00133         inline ~TakeSpinLock()
00134             { if (taken) Leave(); }
00135 
00137         inline void Take();
00139         inline void Leave();
00140 
00141     private:
00142         SpinLock &locker;
00143         bool taken;
00144 
00145         //you cannot duplicate this
00146         TakeSpinLock(const TakeSpinLock&);
00147         const TakeSpinLock& operator=(const TakeSpinLock&);
00148     };
00149 
00150 
00151     // -- Signal-based blocking constructs
00152 
00154     class BlockingObject
00155     {
00156     public:
00157         BlockingObject();
00158         ~BlockingObject();
00159 
00161         void Clear();
00162 
00164         void Set();
00165 
00167         bool WaitUntilClear(bool setOnReturn=false, uint timeToWait=0xffffffff);
00168 
00169     private:
00170         volatile int clearCount;
00171         volatile bool isSet;
00172         void *pdata; //platform-specific container
00173 
00174         //you cannot duplicate this
00175         BlockingObject(const BlockingObject&);
00176         const BlockingObject& operator=(const BlockingObject&);
00177     };
00178 
00179 } //namespace MPMA
00180 
00181 //include the templated and inline code
00182 #ifndef LOCKS_INCLUDE_INLINE
00183     #define LOCKS_INCLUDE_INLINE
00184     #include "Locks.cpp"
00185 #endif
00186 
00187 //include the platform-specific headers
00188 #ifdef WIN32
00189     #include "win32/LocksWin32.h"
00190 #else
00191     #include "linux/LocksLin32.h"
00192 #endif
00193 
00194 #endif //LOCKS_H_INCLUDED

Generated on Sat Aug 9 15:05:05 2008 for MPMA Framework by  doxygen 1.5.6