QuantumLibrary
quantum_mutex_impl.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 //NOTE: DO NOT INCLUDE DIRECTLY
17 
18 //##############################################################################################
19 //#################################### IMPLEMENTATIONS #########################################
20 //##############################################################################################
21 
22 namespace Bloomberg {
23 namespace quantum {
24 
25 //==============================================================================================
26 // class Mutex
27 //==============================================================================================
28 inline
30 {}
31 
32 inline
34 {
35  lockImpl(YieldingThread());
36 }
37 
38 inline
40 {
41  lockImpl(sync->getYieldHandle());
42 }
43 
44 template <class YIELDING>
45 void Mutex::lockImpl(YIELDING&& yield)
46 {
47  while (!tryLock())
48  {
49  yield();
50  }
51 }
52 
53 inline
55 {
56  return _spinlock.tryLock();
57 }
58 
59 inline
61 {
62  _spinlock.unlock();
63 }
64 
65 //==============================================================================================
66 // class Mutex::Guard
67 //==============================================================================================
68 inline
70  bool tryLock) :
71  _mutex(mutex)
72 {
73  if (tryLock)
74  {
75  _ownsLock = _mutex.tryLock();
76  }
77  else
78  {
79  _mutex.lock();
80  _ownsLock = true;
81  }
82 }
83 
84 inline
86  Mutex& mutex,
87  bool tryLock) :
88  _mutex(mutex)
89 {
90  if (tryLock)
91  {
92  _ownsLock = _mutex.tryLock();
93  }
94  else
95  {
96  _mutex.lock(sync);
97  _ownsLock = true;
98  }
99 }
100 
101 inline
103 {
104  return _ownsLock;
105 }
106 
107 inline
109 {
110  _mutex.unlock();
111 }
112 
113 //==============================================================================================
114 // class Mutex::ReverseGuard
115 //==============================================================================================
116 inline
118  _mutex(mutex)
119 {
120  _mutex.unlock();
121 }
122 
123 inline
125  Mutex& mutex) :
126  _mutex(mutex),
127  _sync(sync)
128 {
129  _mutex.unlock();
130 }
131 
132 inline
134 {
135  if (_sync)
136  {
137  _mutex.lock(_sync);
138  }
139  else
140  {
141  _mutex.lock();
142  }
143 }
144 
145 }}
~Guard()
Destructor. This will unlock the underlying mutex.
Definition: quantum_mutex_impl.h:108
Definition: quantum_buffer_impl.h:22
void unlock()
Unlock this mutex.
Definition: quantum_mutex_impl.h:60
Mutex()
Default constructor.
Definition: quantum_mutex_impl.h:29
std::shared_ptr< ICoroSync > Ptr
Definition: quantum_icoro_sync.h:36
~ReverseGuard()
Destroys this object and locks the underlying mutex.
Definition: quantum_mutex_impl.h:133
bool ownsLock() const
Determines if this object owns the underlying mutex.
Definition: quantum_mutex_impl.h:102
Guard(Mutex &mutex, bool tryLock=false)
Construct this object and lock the passed-in mutex.
Definition: quantum_mutex_impl.h:69
ReverseGuard(Mutex &mutex)
Construct this object and unlock the passed-in mutex.
Definition: quantum_mutex_impl.h:117
YieldingThreadDuration< std::chrono::microseconds > YieldingThread
Definition: quantum_yielding_thread.h:57
bool tryLock()
Tries to lock the mutex object.
Definition: quantum_mutex_impl.h:54
void unlock()
Unlocks the current object.
Definition: quantum_spinlock_impl.h:43
void lock()
Locks this mutex.
Definition: quantum_mutex_impl.h:33
bool tryLock()
Attempt to acquire the lock.
Definition: quantum_spinlock_impl.h:37