peerplays-fc/include/fc/unique_lock.hpp

42 lines
1.1 KiB
C++
Raw Normal View History

2012-09-08 02:50:37 +00:00
#ifndef _FC_UNIQUE_LOCK_HPP_
#define _FC_UNIQUE_LOCK_HPP_
namespace fc {
2012-09-08 21:37:25 +00:00
struct try_to_lock_t{};
class time_point;
2012-09-08 02:50:37 +00:00
/**
* Including Boost's unique lock drastically increases compile times
* for something that is this trivial!
*/
template<typename T>
class unique_lock {
public:
2012-09-08 21:37:25 +00:00
unique_lock( T& l, const fc::time_point& abs ):_lock(l) { _locked = _lock.try_lock_until(abs); }
unique_lock( T& l, try_to_lock_t ):_lock(l) { _locked = _lock.try_lock(); }
unique_lock( T& l ):_lock(l) { _lock.lock(); _locked = true; }
~unique_lock() { _lock.unlock(); _locked = false; }
operator bool()const { return _locked; }
2012-09-08 02:50:37 +00:00
private:
unique_lock( const unique_lock& );
unique_lock& operator=( const unique_lock& );
2012-09-08 21:37:25 +00:00
bool _locked;
2012-09-08 02:50:37 +00:00
T& _lock;
};
}
/**
* Emulate java with the one quirk that the open bracket must come before
* the synchronized 'keyword'.
*
* <code>
{ synchronized( lock_type )
}
* </code>
*/
#define synchronized(X) fc::unique_lock<decltype((X))> __lock(((X)));
#endif