Debian Codegraph @

js/src/threading/LockGuard.h

raw
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 GuardT>
15 class MOZ_RAII UnlockGuard;
16
17 template <typename Mutex>
18 class MOZ_RAII LockGuard {
19 friend class UnlockGuard<LockGuard<Mutex>>;
20 friend class ConditionVariable;
21 Mutex& mutex;
22
23 public:
24 explicit LockGuard(Mutex& mutex) : mutex(mutex) { lock(); }
25 ~LockGuard() { unlock(); }
26
27 LockGuard(const LockGuard& other) = delete;
28
29 protected:
30 void lock() { mutex.lock(); }
31 void unlock() { mutex.unlock(); }
32 };
33
34 // RAII class to temporarily unlock a LockGuard.
35 template <typename GuardT>
36 class MOZ_RAII UnlockGuard {
37 GuardT& guard;
38
39 public:
40 explicit UnlockGuard(GuardT& guard) : guard(guard) { guard.unlock(); }
41 ~UnlockGuard() { guard.lock(); }
42
43 UnlockGuard(const UnlockGuard& other) = delete;
44 };
45
46 } // namespace js
47
48 #endif // threading_LockGuard_h