ReentrancyGuard.h 1.4 KB

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