All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SpinLock.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include <pthread.h>
7 
8 namespace Eegeo
9 {
10  class SpinLock
11  {
12  pthread_mutex_t m_lock;
13 
14  public:
15  SpinLock () { pthread_mutex_init(&m_lock, 0); }
16  ~SpinLock () { pthread_mutex_destroy(&m_lock); }
17  void Lock ();
18  void Unlock ();
19  };
20 
22  {
23  SpinLock& m_spinLock;
24  public:
25  SpinLockAutoLock( SpinLock& lock ) : m_spinLock(lock)
26  {
27  m_spinLock.Lock();
28  }
29 
31  {
32  m_spinLock.Unlock();
33  }
34  };
35 }
36 
37 inline void Eegeo::SpinLock::Lock()
38 {
39  pthread_mutex_lock(&m_lock);
40 }
41 
42 inline void Eegeo::SpinLock::Unlock()
43 {
44  pthread_mutex_unlock(&m_lock);
45 }