BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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
#define define_self
void yield()
Definition: thread.cpp:365
bool try_lock_until(const time_point &abs_time)
Definition: api.hpp:15
static time_point now()
Definition: time.cpp:13
bool try_lock_for(const microseconds &rel_time)