mfbt/ReentrancyGuard.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/ReentrancyGuard.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* Small helper class for asserting uses of a class are non-reentrant. */
    1.11 +
    1.12 +#ifndef mozilla_ReentrancyGuard_h
    1.13 +#define mozilla_ReentrancyGuard_h
    1.14 +
    1.15 +#include "mozilla/Assertions.h"
    1.16 +#include "mozilla/Attributes.h"
    1.17 +#include "mozilla/GuardObjects.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +
    1.21 +/* Useful for implementing containers that assert non-reentrancy */
    1.22 +class ReentrancyGuard
    1.23 +{
    1.24 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
    1.25 +#ifdef DEBUG
    1.26 +    bool& entered;
    1.27 +#endif
    1.28 +
    1.29 +  public:
    1.30 +    template<class T>
    1.31 +#ifdef DEBUG
    1.32 +    ReentrancyGuard(T& obj
    1.33 +                    MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
    1.34 +      : entered(obj.entered)
    1.35 +#else
    1.36 +    ReentrancyGuard(T&
    1.37 +                    MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
    1.38 +#endif
    1.39 +    {
    1.40 +      MOZ_GUARD_OBJECT_NOTIFIER_INIT;
    1.41 +#ifdef DEBUG
    1.42 +      MOZ_ASSERT(!entered);
    1.43 +      entered = true;
    1.44 +#endif
    1.45 +    }
    1.46 +    ~ReentrancyGuard()
    1.47 +    {
    1.48 +#ifdef DEBUG
    1.49 +      entered = false;
    1.50 +#endif
    1.51 +    }
    1.52 +
    1.53 +  private:
    1.54 +    ReentrancyGuard(const ReentrancyGuard&) MOZ_DELETE;
    1.55 +    void operator=(const ReentrancyGuard&) MOZ_DELETE;
    1.56 +};
    1.57 +
    1.58 +} // namespace mozilla
    1.59 +
    1.60 +#endif /* mozilla_ReentrancyGuard_h */

mercurial