2015-09-03 09:53:00 -07:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
2022-05-11 16:23:09 -07:00
|
|
|
// Copyright (c) 2016-2022 The Zcash developers
|
2015-09-03 09:53:00 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2015-09-03 09:53:00 -07:00
|
|
|
|
|
|
|
#ifndef BITCOIN_REVERSELOCK_H
|
|
|
|
#define BITCOIN_REVERSELOCK_H
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An RAII-style reverse lock. Unlocks on construction and locks on destruction.
|
|
|
|
*/
|
|
|
|
template<typename Lock>
|
|
|
|
class reverse_lock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-09-02 09:19:01 -07:00
|
|
|
explicit reverse_lock(Lock& _lock) : lock(_lock) {
|
|
|
|
_lock.unlock();
|
|
|
|
_lock.swap(templock);
|
2015-09-03 09:53:00 -07:00
|
|
|
}
|
|
|
|
|
2016-01-05 13:10:13 -08:00
|
|
|
~reverse_lock() {
|
|
|
|
templock.lock();
|
|
|
|
templock.swap(lock);
|
2015-09-03 09:53:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
reverse_lock(reverse_lock const&);
|
|
|
|
reverse_lock& operator=(reverse_lock const&);
|
|
|
|
|
|
|
|
Lock& lock;
|
2016-01-05 13:10:13 -08:00
|
|
|
Lock templock;
|
2015-09-03 09:53:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BITCOIN_REVERSELOCK_H
|