My Project
lightMutex.h
Go to the documentation of this file.
1 /* lightMutex.h
2  */
3 #ifndef OSL_LIGHT_MUTEX_H
4 #define OSL_LIGHT_MUTEX_H
5 
6 #include "osl/oslConfig.h"
7 #ifdef PROFILE_MUTEX
8 # include "osl/misc/perfmon.h"
9 #endif
10 #include <thread>
11 //#include <boost/utility.hpp>
12 
13 namespace osl
14 {
15  namespace misc
16  {
17 #if defined OSL_USE_RACE_DETECTOR || defined _MSC_VER
18  typedef std::mutex LightMutex;
19  typedef std::mutex LightMutexChar;
20 #else
21  template <class Mutex>
23  LightScopedLock(const LightScopedLock&) = delete;
25 
26  Mutex& m;
27  public:
28 #ifdef PROFILE_MUTEX
29  LightScopedLock(osl::misc::CounterPair &c,Mutex& m) :m(m){
30  c.count2();
31  while(!m.tryLock()){
32  for(int i=0;i<2;i++){
33  if(!m.waitLock(100)) break;
34  if(m.tryLock()) return;
35  }
36  c.count1();
37  std::this_thread::yield();
38  }
39  }
40 #else
41  LightScopedLock(Mutex& m) :m(m){
42  m.lock();
43  }
44 #endif
46  m.unlock();
47  }
48  };
49 
50 
51  class LightMutex {
52  LightMutex(const LightMutex&) = delete;
53  LightMutex& operator=(const LightMutex&) = delete;
54 
55  volatile int data;
56  public:
58  class unlockable_lock;
59  LightMutex() :data(0) {}
60  bool tryLock(){
61  if(data!=0) return false;
62  int dummy;
63 #ifdef __GNUC__
64  asm __volatile__(" movl $1,%0" "\n\t"
65  " xchgl (%1),%0" "\n\t"
66  : "=&q"(dummy)
67  : "q"(&data)
68  : "cc");
69 #else
70 # error "not supported"
71 #endif
72  return dummy==0;
73  }
74  bool waitLock(int counter){
75  for(int i=0;i<counter;i++){
76 #ifdef __GNUC__
77  asm __volatile__(" pause" "\n\t");
78 #endif
79  if(data==0)
80  return true;
81  }
82  return false;
83  }
84  void lock(){
85  while(!tryLock()){
86  for(int i=0;i<2;i++){
87  if(!waitLock(100)) break;
88  if(tryLock()) return;
89  }
90  std::this_thread::yield();
91  }
92  }
93  void unlock(){
94  data=0;
95  }
96  };
97 
102 
104  bool locked;
105  public:
107  m.lock();
108  }
110  unlock();
111  }
112  void unlock()
113  {
114  if (locked) {
115  locked = false;
116  m.unlock();
117  }
118  }
119  };
120 
122  LightMutexChar(const LightMutexChar&) = delete;
124 
125  volatile char data;
126  public:
129  bool tryLock(){
130  if(data!=0) return false;
131  char dummy;
132 #ifdef __GNUC__
133  asm __volatile__(" movb $1,%0" "\n\t"
134  " xchgb (%1),%0" "\n\t"
135  : "=&q"(dummy)
136  : "q"(&data)
137  : "cc");
138 #else
139 # error "not supported"
140 #endif
141  return dummy==0;
142  }
143  bool waitLock(int counter){
144  for(int i=0;i<counter;i++){
145 #ifdef __GNUC__
146  asm __volatile__(" pause" "\n\t");
147 #endif
148  if(data==0)
149  return true;
150  }
151  return false;
152  }
153  void lock(){
154  while(!tryLock()){
155  for(int i=0;i<2;i++){
156  if(!waitLock(100)) break;
157  if(tryLock()) return;
158  }
159  std::this_thread::yield();
160  }
161  }
162  void unlock(){
163  data=0;
164  }
165  };
166 #endif
167 
168 #ifdef PROFILE_MUTEX
169 # define SCOPED_LOCK(lock,m) \
170  static osl::misc::CounterPair c(__FILE__, __FUNCTION__, __LINE__); \
171  osl::misc::LightMutex::scoped_lock lock(c,m);
172 # define SCOPED_LOCK_CHAR(lock,m) \
173  static osl::misc::CounterPair c(__FILE__, __FUNCTION__, __LINE__); \
174  osl::misc::LightMutexChar::scoped_lock lock(c,m);
175 #else
176 # define SCOPED_LOCK(lock,m) \
177  osl::misc::LightMutex::scoped_lock lock(m);
178 # define SCOPED_LOCK_CHAR(lock,m) \
179  osl::misc::LightMutexChar::scoped_lock lock(m);
180 #endif
181  }
182  using misc::LightMutex;
183 }
184 #endif /* OSL_LIGHT_MUTEX_H */
185 // ;;; Local Variables:
186 // ;;; mode:c++
187 // ;;; c-basic-offset:2
188 // ;;; End:
189 
osl::misc::LightMutex::operator=
LightMutex & operator=(const LightMutex &)=delete
osl::misc::LightMutex::data
volatile int data
Definition: lightMutex.h:55
osl::misc::LightMutexChar::waitLock
bool waitLock(int counter)
Definition: lightMutex.h:143
osl::misc::LightMutex::unlockable_lock::unlockable_lock
unlockable_lock(const unlockable_lock &)
osl::misc::LightMutex::unlock
void unlock()
Definition: lightMutex.h:93
osl::misc::LightMutex::unlockable_lock
requirement: thread local
Definition: lightMutex.h:99
osl::misc::LightMutexChar::scoped_lock
LightScopedLock< LightMutexChar > scoped_lock
Definition: lightMutex.h:127
osl::misc::LightScopedLock::~LightScopedLock
~LightScopedLock()
Definition: lightMutex.h:45
osl::misc::LightMutex::LightMutex
LightMutex(const LightMutex &)=delete
osl::misc::LightMutexChar
Definition: lightMutex.h:121
osl::misc::LightMutex::unlockable_lock::unlock
void unlock()
Definition: lightMutex.h:112
osl::misc::LightMutexChar::data
volatile char data
Definition: lightMutex.h:125
osl::misc::LightScopedLock::LightScopedLock
LightScopedLock(const LightScopedLock &)=delete
osl::misc::LightMutex::unlockable_lock::~unlockable_lock
~unlockable_lock()
Definition: lightMutex.h:109
osl::misc::LightMutex
Definition: lightMutex.h:51
osl::misc::LightMutex::unlockable_lock::operator=
unlockable_lock & operator=(const unlockable_lock &)
osl::misc::LightScopedLock::m
Mutex & m
Definition: lightMutex.h:26
osl::misc::LightMutex::lock
void lock()
Definition: lightMutex.h:84
osl::misc::LightScopedLock::operator=
LightScopedLock & operator=(const LightScopedLock &)=delete
osl::misc::LightMutex::waitLock
bool waitLock(int counter)
Definition: lightMutex.h:74
oslConfig.h
osl::misc::LightMutex::tryLock
bool tryLock()
Definition: lightMutex.h:60
osl::misc::LightMutex::unlockable_lock::unlockable_lock
unlockable_lock(LightMutex &m)
Definition: lightMutex.h:106
osl::misc::LightMutexChar::tryLock
bool tryLock()
Definition: lightMutex.h:129
osl::misc::LightMutex::LightMutex
LightMutex()
Definition: lightMutex.h:59
osl::misc::LightScopedLock
Definition: lightMutex.h:22
osl::misc::LightMutexChar::unlock
void unlock()
Definition: lightMutex.h:162
misc
osl::misc::LightMutex::unlockable_lock::m
LightMutex & m
Definition: lightMutex.h:103
osl::misc::LightScopedLock::LightScopedLock
LightScopedLock(Mutex &m)
Definition: lightMutex.h:41
osl::misc::LightMutexChar::LightMutexChar
LightMutexChar(const LightMutexChar &)=delete
osl::misc::LightMutexChar::operator=
LightMutexChar & operator=(const LightMutexChar &)=delete
osl::misc::LightMutex::scoped_lock
LightScopedLock< LightMutex > scoped_lock
Definition: lightMutex.h:57
osl::misc::LightMutexChar::LightMutexChar
LightMutexChar()
Definition: lightMutex.h:128
osl::misc::LightMutexChar::lock
void lock()
Definition: lightMutex.h:153
osl::misc::LightMutex::unlockable_lock::locked
bool locked
Definition: lightMutex.h:104
osl
Definition: additionalEffect.h:6