You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reverselock.h 767B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (c) 2015-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_REVERSELOCK_H
  5. #define STARWELS_REVERSELOCK_H
  6. /**
  7. * An RAII-style reverse lock. Unlocks on construction and locks on destruction.
  8. */
  9. template<typename Lock>
  10. class reverse_lock
  11. {
  12. public:
  13. explicit reverse_lock(Lock& _lock) : lock(_lock) {
  14. _lock.unlock();
  15. _lock.swap(templock);
  16. }
  17. ~reverse_lock() {
  18. templock.lock();
  19. templock.swap(lock);
  20. }
  21. private:
  22. reverse_lock(reverse_lock const&);
  23. reverse_lock& operator=(reverse_lock const&);
  24. Lock& lock;
  25. Lock templock;
  26. };
  27. #endif // STARWELS_REVERSELOCK_H