Correct spelling of RAII and add missing check in unlock_early.
Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
This commit is contained in:
parent
99a2262d61
commit
392156c250
@ -20,37 +20,46 @@
|
|||||||
#ifndef SCOPE_LOCK_H
|
#ifndef SCOPE_LOCK_H
|
||||||
#define SCOPE_LOCK_H
|
#define SCOPE_LOCK_H
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace nextpnr {
|
namespace nextpnr {
|
||||||
|
|
||||||
// Provides a simple RIAA locking object. ScopeLock takes a lock when
|
// Provides a simple RAII locking object. ScopeLock takes a lock when
|
||||||
// constructed, and releases the lock on destruction or if "unlock_early" is
|
// constructed, and releases the lock on destruction or if "unlock_early" is
|
||||||
// called.
|
// called.
|
||||||
//
|
//
|
||||||
// LockingObject must have a method "void lock(void)" and "void unlock(void)".
|
// LockingObject must have a method "void lock(void)" and "void unlock(void)".
|
||||||
template<typename LockingObject>
|
template <typename LockingObject> class ScopeLock
|
||||||
class ScopeLock {
|
{
|
||||||
public:
|
public:
|
||||||
ScopeLock(LockingObject * obj) : obj_(obj), locked_(false) {
|
ScopeLock(LockingObject *obj) : obj_(obj), locked_(false)
|
||||||
|
{
|
||||||
obj_->lock();
|
obj_->lock();
|
||||||
locked_ = true;
|
locked_ = true;
|
||||||
}
|
}
|
||||||
ScopeLock(const ScopeLock &other) = delete;
|
ScopeLock(const ScopeLock &other) = delete;
|
||||||
ScopeLock(const ScopeLock &&other) = delete;
|
ScopeLock(const ScopeLock &&other) = delete;
|
||||||
|
|
||||||
~ScopeLock() {
|
~ScopeLock()
|
||||||
if(locked_) {
|
{
|
||||||
|
if (locked_) {
|
||||||
obj_->unlock();
|
obj_->unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void unlock_early() {
|
void unlock_early()
|
||||||
|
{
|
||||||
|
if (!locked_) {
|
||||||
|
throw std::runtime_error("Lock already released?");
|
||||||
|
}
|
||||||
locked_ = false;
|
locked_ = false;
|
||||||
obj_->unlock();
|
obj_->unlock();
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
|
private:
|
||||||
LockingObject *obj_;
|
LockingObject *obj_;
|
||||||
bool locked_;
|
bool locked_;
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}; // namespace nextpnr
|
||||||
|
|
||||||
#endif /* SCOPE_LOCK_H */
|
#endif /* SCOPE_LOCK_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user