1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef threading_LockGuard_h 8 #define threading_LockGuard_h 9 10 #include "mozilla/Attributes.h" 11 12 namespace js { 13 14 template <typename Mutex> 15 class MOZ_RAII UnlockGuard; 16 17 template <typename Mutex> 18 class MOZ_RAII LockGuard { 19 friend class UnlockGuard<Mutex>; 20 friend class ConditionVariable; 21 Mutex& lock; 22 23 public: LockGuard(Mutex & aLock)24 explicit LockGuard(Mutex& aLock) : lock(aLock) { lock.lock(); } 25 ~LockGuard()26 ~LockGuard() { lock.unlock(); } 27 }; 28 29 template <typename Mutex> 30 class MOZ_RAII UnlockGuard { 31 Mutex& lock; 32 33 public: UnlockGuard(LockGuard<Mutex> & aGuard)34 explicit UnlockGuard(LockGuard<Mutex>& aGuard) : lock(aGuard.lock) { 35 lock.unlock(); 36 } 37 ~UnlockGuard()38 ~UnlockGuard() { lock.lock(); } 39 }; 40 41 } // namespace js 42 43 #endif // threading_LockGuard_h 44