|
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 /* Small helper class for asserting uses of a class are non-reentrant. */ |
|
8 |
|
9 #ifndef mozilla_ReentrancyGuard_h |
|
10 #define mozilla_ReentrancyGuard_h |
|
11 |
|
12 #include "mozilla/Assertions.h" |
|
13 #include "mozilla/Attributes.h" |
|
14 #include "mozilla/GuardObjects.h" |
|
15 |
|
16 namespace mozilla { |
|
17 |
|
18 /* Useful for implementing containers that assert non-reentrancy */ |
|
19 class ReentrancyGuard |
|
20 { |
|
21 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
|
22 #ifdef DEBUG |
|
23 bool& entered; |
|
24 #endif |
|
25 |
|
26 public: |
|
27 template<class T> |
|
28 #ifdef DEBUG |
|
29 ReentrancyGuard(T& obj |
|
30 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
|
31 : entered(obj.entered) |
|
32 #else |
|
33 ReentrancyGuard(T& |
|
34 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
|
35 #endif |
|
36 { |
|
37 MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
|
38 #ifdef DEBUG |
|
39 MOZ_ASSERT(!entered); |
|
40 entered = true; |
|
41 #endif |
|
42 } |
|
43 ~ReentrancyGuard() |
|
44 { |
|
45 #ifdef DEBUG |
|
46 entered = false; |
|
47 #endif |
|
48 } |
|
49 |
|
50 private: |
|
51 ReentrancyGuard(const ReentrancyGuard&) MOZ_DELETE; |
|
52 void operator=(const ReentrancyGuard&) MOZ_DELETE; |
|
53 }; |
|
54 |
|
55 } // namespace mozilla |
|
56 |
|
57 #endif /* mozilla_ReentrancyGuard_h */ |