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 : /* 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 MOZ_RAII ReentrancyGuard
20 : {
21 : MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
22 : #ifdef DEBUG
23 : bool& mEntered;
24 : #endif
25 :
26 : public:
27 : template<class T>
28 : #ifdef DEBUG
29 8800032 : explicit ReentrancyGuard(T& aObj
30 : MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
31 8800032 : : mEntered(aObj.mEntered)
32 : #else
33 : explicit ReentrancyGuard(T&
34 : MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
35 : #endif
36 : {
37 8800039 : MOZ_GUARD_OBJECT_NOTIFIER_INIT;
38 : #ifdef DEBUG
39 8800079 : MOZ_ASSERT(!mEntered);
40 8800079 : mEntered = true;
41 : #endif
42 8800079 : }
43 8873051 : ~ReentrancyGuard()
44 8873051 : {
45 : #ifdef DEBUG
46 8873051 : mEntered = false;
47 : #endif
48 8871602 : }
49 :
50 : private:
51 : ReentrancyGuard(const ReentrancyGuard&) = delete;
52 : void operator=(const ReentrancyGuard&) = delete;
53 : };
54 :
55 : } // namespace mozilla
56 :
57 : #endif /* mozilla_ReentrancyGuard_h */
|