00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00032
00033 #ifndef GROOVX_RUTZ_SPIN_LOCK_H_UTC20070904211219_DEFINED
00034 #define GROOVX_RUTZ_SPIN_LOCK_H_UTC20070904211219_DEFINED
00035
00036 #if defined(HAVE_LIBKERN_OSATOMIC_H)
00037 # include "rutz/spin_lock_darwin.h"
00038 namespace rutz { typedef spin_lock_darwin spin_lock_t; }
00039
00040 #elif defined(HAVE_PTHREAD_SPINLOCK_T)
00041 # include "rutz/spin_lock_pthreads.h"
00042 namespace rutz { typedef spin_lock_pthreads spin_lock_t; }
00043
00044 #else
00045 # error no spinlock implementation available
00046 #endif
00047
00048 namespace rutz
00049 {
00050
00051 class spin_lock_locker
00052 {
00053 private:
00054 spin_lock_t* m_spin_lock;
00055
00056 spin_lock_locker(const spin_lock_locker&);
00057 spin_lock_locker& operator=(const spin_lock_locker&);
00058
00059 public:
00060 spin_lock_locker(spin_lock_t* lock) : m_spin_lock(lock)
00061 {
00062 if (m_spin_lock)
00063 m_spin_lock->lock();
00064 }
00065
00066 ~spin_lock_locker()
00067 {
00068 if (m_spin_lock)
00069 m_spin_lock->unlock();
00070 }
00071
00072 void unlock()
00073 {
00074 if (m_spin_lock)
00075 m_spin_lock->unlock();
00076 m_spin_lock = 0;
00077 }
00078 };
00079
00080 }
00081
00082 #define GVX_SPIN_CONCAT2(x,y) x##y
00083 #define GVX_SPIN_CONCAT(x,y) GVX_SPIN_CONCAT2(x,y)
00084
00085 #define GVX_SPIN_LOCK(x) \
00086 ::rutz::spin_lock_locker GVX_SPIN_CONCAT(anonymous_lock,__LINE__) (x)
00087
00088 #define GVX_SPIN_LOCK_IF(doit, x) \
00089 ::rutz::spin_lock_locker GVX_SPIN_CONCAT(anonymous_lock,__LINE__) ((doit) ? (x) : 0)
00090
00091 static const char vcid_groovx_rutz_spin_lock_h_utc20070904211219[] = "$Id: spin_lock.h 10134 2008-03-04 17:58:05Z rjpeters $ $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/rutz/spin_lock.h $";
00092 #endif // !GROOVX_RUTZ_SPIN_LOCK_H_UTC20070904211219DEFINED