BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
spin_yield_lock.cpp
Go to the documentation of this file.
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 
13  {
15  new (self) boost::atomic<int>();
16  static_assert( sizeof(boost::atomic<int>) == sizeof(_lock), "" );
17  self->store(unlocked);
18  }
19 
22  return self->exchange(locked, boost::memory_order_acquire)!=locked;
23  }
24 
26  return try_lock_until( fc::time_point::now() + us );
27  }
28 
30  while( abs_time > time_point::now() ) {
31  if( try_lock() )
32  return true;
33  yield();
34  }
35  return false;
36  }
37 
40  while( self->exchange(locked, boost::memory_order_acquire)==locked) {
41  yield();
42  }
43  }
44 
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