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