|
MongoDB
2.0.3
|
00001 // spin_lock.h 00002 00019 #pragma once 00020 00021 #include "mutex.h" 00022 00023 namespace mongo { 00024 00029 class SpinLock : boost::noncopyable { 00030 public: 00031 SpinLock(); 00032 ~SpinLock(); 00033 00034 void lock(); 00035 void unlock(); 00036 00037 static bool isfast(); // true if a real spinlock on this platform 00038 00039 private: 00040 #if defined(_WIN32) 00041 CRITICAL_SECTION _cs; 00042 #elif defined(__USE_XOPEN2K) 00043 pthread_spinlock_t _lock; 00044 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) 00045 volatile bool _locked; 00046 #else 00047 // default to a mutex if not implemented 00048 SimpleMutex _mutex; 00049 #endif 00050 }; 00051 00052 class scoped_spinlock : boost::noncopyable { 00053 public: 00054 scoped_spinlock( SpinLock& l ) : _l(l) { 00055 _l.lock(); 00056 } 00057 ~scoped_spinlock() { 00058 _l.unlock();} 00059 private: 00060 SpinLock& _l; 00061 }; 00062 00063 } // namespace mongo
1.8.0