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 namespace js { 11 12 template <typename Mutex> class MOZ_RAII UnlockGuard; 13 14 template <typename Mutex> 15 class MOZ_RAII LockGuard 16 { 17 friend class UnlockGuard<Mutex>; 18 friend class ConditionVariable; 19 Mutex& lock; 20 21 public: 22 explicit LockGuard(Mutex& aLock) 23 : lock(aLock) 24 { 25 lock.lock(); 26 } 27 28 ~LockGuard() { 29 lock.unlock(); 30 } 31 }; 32 33 template <typename Mutex> 34 class MOZ_RAII UnlockGuard 35 { 36 Mutex& lock; 37 38 public: 39 explicit UnlockGuard(LockGuard<Mutex>& aGuard) 40 : lock(aGuard.lock) 41 { 42 lock.unlock(); 43 } 44 45 ~UnlockGuard() { 46 lock.lock(); 47 } 48 }; 49 50 } // namespace js 51 52 #endif // threading_LockGuard_h 53