MongoDB  2.7.0
spin_lock.h
1 // spin_lock.h
2 
31 #pragma once
32 
33 #ifdef _WIN32
34 #include "mongo/platform/windows_basic.h"
35 #endif
36 
37 #include "mutex.h"
38 
39 namespace mongo {
40 
45  class SpinLock : boost::noncopyable {
46  public:
47  SpinLock();
48  ~SpinLock();
49 
50  static bool isfast(); // true if a real spinlock on this platform
51 
52  private:
53 #if defined(_WIN32)
54  CRITICAL_SECTION _cs;
55  public:
56  void lock() {EnterCriticalSection(&_cs); }
57  void unlock() { LeaveCriticalSection(&_cs); }
58 #elif defined(__USE_XOPEN2K)
59  pthread_spinlock_t _lock;
60  void _lk();
61  public:
62  void unlock() { pthread_spin_unlock(&_lock); }
63  void lock() {
64  if ( MONGO_likely( pthread_spin_trylock( &_lock ) == 0 ) )
65  return;
66  _lk();
67  }
68 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
69  volatile bool _locked;
70  public:
71  void unlock() {__sync_lock_release(&_locked); }
72  void lock();
73 #else
74  // default to a mutex if not implemented
75  SimpleMutex _mutex;
76  public:
77  void unlock() { _mutex.unlock(); }
78  void lock() { _mutex.lock(); }
79 #endif
80  };
81 
82  class scoped_spinlock : boost::noncopyable {
83  public:
84  scoped_spinlock( SpinLock& l ) : _l(l) {
85  _l.lock();
86  }
87  ~scoped_spinlock() {
88  _l.unlock();}
89  private:
90  SpinLock& _l;
91  };
92 
93 } // namespace mongo
Definition: spin_lock.h:82
The spinlock currently requires late GCC support routines to be efficient.
Definition: spin_lock.h:45
The concept with SimpleMutex is that it is a basic lock/unlock with no special functionality (such as...
Definition: mutex.h:158