ReentrancyGuard.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Small helper class for asserting uses of a class are non-reentrant. */
  6. #ifndef mozilla_ReentrancyGuard_h
  7. #define mozilla_ReentrancyGuard_h
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/GuardObjects.h"
  11. namespace mozilla {
  12. /* Useful for implementing containers that assert non-reentrancy */
  13. class MOZ_RAII ReentrancyGuard
  14. {
  15. MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  16. #ifdef DEBUG
  17. bool& mEntered;
  18. #endif
  19. public:
  20. template<class T>
  21. #ifdef DEBUG
  22. explicit ReentrancyGuard(T& aObj
  23. MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  24. : mEntered(aObj.mEntered)
  25. #else
  26. explicit ReentrancyGuard(T&
  27. MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  28. #endif
  29. {
  30. MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  31. #ifdef DEBUG
  32. MOZ_ASSERT(!mEntered);
  33. mEntered = true;
  34. #endif
  35. }
  36. ~ReentrancyGuard()
  37. {
  38. #ifdef DEBUG
  39. mEntered = false;
  40. #endif
  41. }
  42. private:
  43. ReentrancyGuard(const ReentrancyGuard&) = delete;
  44. void operator=(const ReentrancyGuard&) = delete;
  45. };
  46. } // namespace mozilla
  47. #endif /* mozilla_ReentrancyGuard_h */