BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
unique_lock.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <assert.h>
3 
4 namespace fc {
5  struct try_to_lock_t{};
6  class time_point;
7 
12  template<typename T>
13  class unique_lock {
14  public:
15  unique_lock( T& l, const fc::time_point& abs ):_lock(l) { _locked = _lock.try_lock_until(abs); }
16  unique_lock( T& l, try_to_lock_t ):_lock(l) { _locked = _lock.try_lock(); }
17  unique_lock( T& l ): _locked(false), _lock(l) { lock(); }
18  ~unique_lock() { if (_locked) unlock(); }
19  operator bool() const { return _locked; }
20  void unlock() { assert(_locked); if (_locked) { _lock.unlock(); _locked = false;} }
21  void lock() { assert(!_locked); if (!_locked) { _lock.lock(); _locked = true; } }
22  private:
23  unique_lock( const unique_lock& );
24  unique_lock& operator=( const unique_lock& );
25  bool _locked;
26  T& _lock;
27  };
28 
29 }
30 
41 #define synchronized(X) fc::unique_lock<decltype((X))> __lock(((X)));
42 
unique_lock(T &l, const fc::time_point &abs)
Definition: unique_lock.hpp:15
unique_lock(T &l, try_to_lock_t)
Definition: unique_lock.hpp:16
Definition: api.hpp:15