diff options
author | gatecat <gatecat@ds0.me> | 2021-03-02 08:27:12 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-02 08:27:12 +0000 |
commit | 27fbee523301be074abd06a3568dc9591d98e0fa (patch) | |
tree | 3ec0f59c35cb1107adc3a2f5043e19e4ffde3adc /common/scope_lock.h | |
parent | 6ff02248a3f625829c9bd041c369247ee926d8d0 (diff) | |
parent | 392156c25095aa93fdf847ef51dfa571b5d2ce88 (diff) | |
download | nextpnr-27fbee523301be074abd06a3568dc9591d98e0fa.tar.gz nextpnr-27fbee523301be074abd06a3568dc9591d98e0fa.tar.bz2 nextpnr-27fbee523301be074abd06a3568dc9591d98e0fa.zip |
Merge pull request #605 from litghost/add_placement_sanity_check
Add placement sanity check in placer_heap.
Diffstat (limited to 'common/scope_lock.h')
-rw-r--r-- | common/scope_lock.h | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/common/scope_lock.h b/common/scope_lock.h new file mode 100644 index 00000000..7b6c9dcd --- /dev/null +++ b/common/scope_lock.h @@ -0,0 +1,65 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2021 Symbiflow Authors. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#ifndef SCOPE_LOCK_H +#define SCOPE_LOCK_H + +#include <stdexcept> + +namespace nextpnr { + +// 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) + { + obj_->lock(); + locked_ = true; + } + ScopeLock(const ScopeLock &other) = delete; + ScopeLock(const ScopeLock &&other) = delete; + + ~ScopeLock() + { + if (locked_) { + obj_->unlock(); + } + } + void unlock_early() + { + if (!locked_) { + throw std::runtime_error("Lock already released?"); + } + locked_ = false; + obj_->unlock(); + } + + private: + LockingObject *obj_; + bool locked_; +}; + +}; // namespace nextpnr + +#endif /* SCOPE_LOCK_H */ |