QuantumLibrary
quantum_spinlock.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_SPINLOCK_H
17 #define BLOOMBERG_QUANTUM_SPINLOCK_H
18 
19 #include <atomic>
20 #include <mutex>
21 
22 namespace Bloomberg {
23 namespace quantum {
24 
25 //==============================================================================================
26 // class SpinLock
27 //==============================================================================================
31 class SpinLock
32 {
33 public:
34  using TryToLock = std::try_to_lock_t;
35 
37  SpinLock();
38 
40  SpinLock(const SpinLock&) = delete;
41 
43  SpinLock(SpinLock&&) = default;
44 
46  SpinLock& operator=(const SpinLock&) = delete;
47 
49  SpinLock& operator=(SpinLock&&) = default;
50 
54  void lock();
55 
59  bool tryLock();
60 
63  void unlock();
64 
65  //==============================================================================================
66  // class SpinLock::Guard
67  //==============================================================================================
71  class Guard
72  {
73  public:
77  explicit Guard(SpinLock& lock);
78 
83 
85  ~Guard();
86 
89  void lock();
90 
94  bool tryLock();
95 
98  bool ownsLock() const;
99  private:
100  SpinLock& _spinlock;
101  bool _ownsLock;
102  };
103 
104  //==============================================================================================
105  // class SpinLock::ReverseGuard
106  //==============================================================================================
111  {
112  public:
115  explicit ReverseGuard(SpinLock& lock);
116 
119  ~ReverseGuard();
120  private:
121  SpinLock& _spinlock;
122  };
123 
124 private:
125  std::atomic_flag _flag;
126 };
127 
128 }}
129 
130 #include <quantum/impl/quantum_spinlock_impl.h>
131 
132 #endif //BLOOMBERG_QUANTUM_SPINLOCK_H
Guard(SpinLock &lock)
Construct this object and lock the passed-in spinlock.
Definition: quantum_spinlock_impl.h:49
Definition: quantum_spinlock.h:71
void lock()
Locks this object.
Definition: quantum_buffer_impl.h:22
~Guard()
Destroy this object and unlock the underlying spinlock.
Definition: quantum_spinlock_impl.h:64
~ReverseGuard()
Acquire the underlying spinlock.
Definition: quantum_spinlock_impl.h:102
SpinLock()
Constructor. The object is in the unlocked state.
Definition: quantum_spinlock_impl.h:26
ReverseGuard(SpinLock &lock)
Release the passed-in spinlock.
Definition: quantum_spinlock_impl.h:95
bool tryLock()
Try to acquire the underlying spinlock.
Definition: quantum_spinlock_impl.h:72
Definition: quantum_spinlock.h:110
SpinLock & operator=(const SpinLock &)=delete
Copy assignment operator.
bool ownsLock() const
Indicates if this object owns the underlying spinlock.
Definition: quantum_spinlock_impl.h:90
std::try_to_lock_t TryToLock
Definition: quantum_spinlock.h:34
void lock()
Acquire the underlying spinlock.
Definition: quantum_spinlock_impl.h:81
void unlock()
Unlocks the current object.
Definition: quantum_spinlock_impl.h:43
bool tryLock()
Attempt to acquire the lock.
Definition: quantum_spinlock_impl.h:37