aboutsummaryrefslogtreecommitdiffstats
path: root/common/scope_lock.h
diff options
context:
space:
mode:
authorKeith Rothman <537074+litghost@users.noreply.github.com>2021-03-01 10:03:42 -0800
committerKeith Rothman <537074+litghost@users.noreply.github.com>2021-03-01 13:11:04 -0800
commit392156c25095aa93fdf847ef51dfa571b5d2ce88 (patch)
tree545f8c2f4ad52d376752b018b1680da80c3b9496 /common/scope_lock.h
parent99a2262d61c20019b2a4ce5321df48a9d5d43864 (diff)
downloadnextpnr-392156c25095aa93fdf847ef51dfa571b5d2ce88.tar.gz
nextpnr-392156c25095aa93fdf847ef51dfa571b5d2ce88.tar.bz2
nextpnr-392156c25095aa93fdf847ef51dfa571b5d2ce88.zip
Correct spelling of RAII and add missing check in unlock_early.
Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
Diffstat (limited to 'common/scope_lock.h')
-rw-r--r--common/scope_lock.h29
1 files changed, 19 insertions, 10 deletions
diff --git a/common/scope_lock.h b/common/scope_lock.h
index 35de6bc9..7b6c9dcd 100644
--- a/common/scope_lock.h
+++ b/common/scope_lock.h
@@ -20,37 +20,46 @@
#ifndef SCOPE_LOCK_H
#define SCOPE_LOCK_H
+#include <stdexcept>
+
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
// called.
//
// LockingObject must have a method "void lock(void)" and "void unlock(void)".
-template<typename LockingObject>
-class ScopeLock {
-public:
- ScopeLock(LockingObject * obj) : obj_(obj), locked_(false) {
+template <typename LockingObject> class ScopeLock
+{
+ public:
+ ScopeLock(LockingObject *obj) : obj_(obj), locked_(false)
+ {
obj_->lock();
locked_ = true;
}
ScopeLock(const ScopeLock &other) = delete;
ScopeLock(const ScopeLock &&other) = delete;
- ~ScopeLock() {
- if(locked_) {
+ ~ScopeLock()
+ {
+ if (locked_) {
obj_->unlock();
}
}
- void unlock_early() {
+ void unlock_early()
+ {
+ if (!locked_) {
+ throw std::runtime_error("Lock already released?");
+ }
locked_ = false;
obj_->unlock();
}
-private:
+
+ private:
LockingObject *obj_;
bool locked_;
};
-};
+}; // namespace nextpnr
#endif /* SCOPE_LOCK_H */