MongoDB  1.8.5
spin_lock.h
00001 // spin_lock.h
00002 
00019 #pragma once
00020 
00021 #include "pch.h"
00022 #include "rwlock.h"
00023 
00024 namespace mongo {
00025 
00030     class SpinLock {
00031     public:
00032         SpinLock();
00033         ~SpinLock();
00034 
00035         void lock();
00036         void unlock();
00037 
00038     private:
00039 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
00040         volatile bool _locked;
00041 #elif defined(_WIN32)
00042         CRITICAL_SECTION _cs;
00043 #else
00044         // default to a scoped mutex if not implemented
00045         RWLock _mutex;
00046 #endif
00047 
00048         // Non-copyable, non-assignable
00049         SpinLock(SpinLock&);
00050         SpinLock& operator=(SpinLock&);
00051     };
00052     
00053     struct scoped_spinlock {
00054         scoped_spinlock( SpinLock& l ) : _l(l){
00055             _l.lock();
00056         }
00057         ~scoped_spinlock() {
00058             _l.unlock();
00059         }
00060         SpinLock& _l;
00061     };
00062 
00063 }  // namespace mongo
00064