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
13namespace 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>
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;
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
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
LightMutexChar(const LightMutexChar &)=delete
LightMutexChar & operator=(const LightMutexChar &)=delete
LightScopedLock< LightMutexChar > scoped_lock
Definition lightMutex.h:127
bool waitLock(int counter)
Definition lightMutex.h:143
requirement: thread local
Definition lightMutex.h:99
unlockable_lock & operator=(const unlockable_lock &)
unlockable_lock(const unlockable_lock &)
LightMutex(const LightMutex &)=delete
bool waitLock(int counter)
Definition lightMutex.h:74
LightScopedLock< LightMutex > scoped_lock
Definition lightMutex.h:57
LightMutex & operator=(const LightMutex &)=delete
volatile int data
Definition lightMutex.h:55
LightScopedLock & operator=(const LightScopedLock &)=delete
LightScopedLock(const LightScopedLock &)=delete