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