BitShares-Core
7.0.2
BitShares blockchain node software and command-line wallet software
libraries
fc
src
thread
spin_lock.cpp
Go to the documentation of this file.
1
#include <
fc/thread/spin_lock.hpp
>
2
#include <
fc/time.hpp
>
3
#include <boost/atomic.hpp>
4
#include <boost/memory_order.hpp>
5
#include <new>
6
7
namespace
fc
{
8
#define define_self boost::atomic<int>* self = (boost::atomic<int>*)&_lock
9
spin_lock::spin_lock
()
10
{
11
define_self
;
12
new
(
self
) boost::atomic<int>();
13
static_assert(
sizeof
(boost::atomic<int>) ==
sizeof
(_lock),
""
);
14
self
->store(unlocked);
15
}
16
17
bool
spin_lock::try_lock
() {
18
define_self
;
19
return
self
->exchange(locked, boost::memory_order_acquire)!=locked;
20
}
21
22
bool
spin_lock::try_lock_for
(
const
fc::microseconds
& us ) {
23
return
try_lock_until
(
fc::time_point::now
() + us );
24
}
25
26
bool
spin_lock::try_lock_until
(
const
fc::time_point
& abs_time ) {
27
while
( abs_time >
time_point::now
() ) {
28
if
(
try_lock
() )
29
return
true
;
30
}
31
return
false
;
32
}
33
34
void
spin_lock::lock
() {
35
define_self
;
36
while
( self->exchange(locked, boost::memory_order_acquire)==locked) { }
37
}
38
39
void
spin_lock::unlock
() {
40
define_self
;
41
self
->store(unlocked, boost::memory_order_release);
42
}
43
44
#undef define_self
45
46
}
// namespace fc
fc::spin_lock::try_lock_until
bool try_lock_until(const time_point &abs_time)
Definition:
spin_lock.cpp:26
fc
Definition:
api.hpp:15
fc::spin_lock::try_lock_for
bool try_lock_for(const microseconds &rel_time)
Definition:
spin_lock.cpp:22
spin_lock.hpp
fc::spin_lock::try_lock
bool try_lock()
Definition:
spin_lock.cpp:17
fc::spin_lock::unlock
void unlock()
Definition:
spin_lock.cpp:39
fc::spin_lock::spin_lock
spin_lock()
Definition:
spin_lock.cpp:9
define_self
#define define_self
Definition:
spin_lock.cpp:8
fc::spin_lock::lock
void lock()
Definition:
spin_lock.cpp:34
fc::microseconds
Definition:
time.hpp:12
fc::time_point::now
static time_point now()
Definition:
time.cpp:13
fc::time_point
Definition:
time.hpp:44
time.hpp
Generated on Fri Dec 15 2023 06:12:43 for BitShares-Core by
1.8.17