QuantumLibrary
quantum_spinlock_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 inline
27  _flag ATOMIC_FLAG_INIT
28 {}
29 
30 inline
31 void SpinLock::lock()
32 {
33  while (_flag.test_and_set(std::memory_order_acquire)); //spin
34 }
35 
36 inline
38 {
39  return !_flag.test_and_set(std::memory_order_acquire);
40 }
41 
42 inline
44 {
45  _flag.clear(std::memory_order_release);
46 }
47 
48 inline
50  _spinlock(lock),
51  _ownsLock(true)
52 {
53  _spinlock.lock();
54 }
55 
56 inline
58  _spinlock(lock),
59  _ownsLock(_spinlock.tryLock())
60 {
61 }
62 
63 inline
65 {
66  if (_ownsLock) {
67  _spinlock.unlock();
68  }
69 }
70 
71 inline
73 {
74  if (!_ownsLock) {
75  _ownsLock = _spinlock.tryLock();
76  }
77  return _ownsLock;
78 }
79 
80 inline
82 {
83  if (!_ownsLock) {
84  _spinlock.lock();
85  _ownsLock = true;
86  }
87 }
88 
89 inline
91  return _ownsLock;
92 }
93 
94 inline
96  _spinlock(lock)
97 {
98  _spinlock.unlock();
99 }
100 
101 inline
103 {
104  _spinlock.lock();
105 }
106 
107 }}
Guard(SpinLock &lock)
Construct this object and lock the passed-in spinlock.
Definition: quantum_spinlock_impl.h:49
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
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