QuantumLibrary
quantum_mutex.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 #ifndef BLOOMBERG_QUANTUM_MUTEX_H
17 #define BLOOMBERG_QUANTUM_MUTEX_H
18 
19 #include <atomic>
20 #include <quantum/quantum_traits.h>
21 #include <quantum/quantum_spinlock.h>
22 #include <quantum/interface/quantum_icontext.h>
23 #include <quantum/quantum_yielding_thread.h>
24 
25 namespace Bloomberg {
26 namespace quantum {
27 
28 //==============================================================================================
29 // class Mutex
30 //==============================================================================================
36 class Mutex
37 {
38 public:
41  Mutex();
42 
43  Mutex(const Mutex& other) = delete;
44  Mutex& operator=(const Mutex& other) = delete;
45 
52  void lock();
53 
58  void lock(ICoroSync::Ptr sync);
59 
62  bool tryLock();
63 
65  void unlock();
66 
67  //==============================================================================================
68  // class Mutex::Guard
69  //==============================================================================================
73  class Guard
74  {
75  public:
83  explicit Guard(Mutex& mutex,
84  bool tryLock = false);
85 
92  Guard(ICoroSync::Ptr sync,
93  Mutex& mutex,
94  bool tryLock = false);
95 
97  ~Guard();
98 
101  bool ownsLock() const;
102 
103  private:
104  //Members
105  Mutex& _mutex;
106  bool _ownsLock;
107  };
108 
109  //==============================================================================================
110  // class Mutex::ReverseGuard
111  //==============================================================================================
116  {
117  public:
123  explicit ReverseGuard(Mutex& mutex);
124 
130  Mutex& mutex);
131 
133  ~ReverseGuard();
134 
135  private:
136  //Members
137  Mutex& _mutex;
138  ICoroSync::Ptr _sync;
139  };
140 
141 private:
142  template <class YIELDING>
143  void lockImpl(YIELDING&& yield);
144 
145  //Members
146  mutable SpinLock _spinlock;
147 };
148 
149 }}
150 
151 #include <quantum/impl/quantum_mutex_impl.h>
152 
153 #endif //BLOOMBERG_QUANTUM_MUTEX_H
Mutex & operator=(const Mutex &other)=delete
~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
Definition: quantum_mutex.h:115
ReverseGuard(Mutex &mutex)
Construct this object and unlock the passed-in mutex.
Definition: quantum_mutex_impl.h:117
Definition: quantum_mutex.h:73
bool tryLock()
Tries to lock the mutex object.
Definition: quantum_mutex_impl.h:54
void lock()
Locks this mutex.
Definition: quantum_mutex_impl.h:33