Line data Source code
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 32775 : explicit LockGuard(Mutex& aLock)
23 32775 : : lock(aLock)
24 : {
25 32775 : lock.lock();
26 32775 : }
27 :
28 32739 : ~LockGuard() {
29 32739 : lock.unlock();
30 32739 : }
31 : };
32 :
33 : template <typename Mutex>
34 : class MOZ_RAII UnlockGuard
35 : {
36 : Mutex& lock;
37 :
38 : public:
39 56 : explicit UnlockGuard(LockGuard<Mutex>& aGuard)
40 56 : : lock(aGuard.lock)
41 : {
42 56 : lock.unlock();
43 56 : }
44 :
45 55 : ~UnlockGuard() {
46 55 : lock.lock();
47 55 : }
48 : };
49 :
50 : } // namespace js
51 :
52 : #endif // threading_LockGuard_h
|