#pragma once
#include "DisallowScope.h"
#include "Heap.h"
#include <wtf/ThreadSpecific.h>
namespace JSC {
class DeferGC {
WTF_MAKE_NONCOPYABLE(DeferGC);
public:
DeferGC(Heap& heap)
: m_heap(heap)
{
m_heap.incrementDeferralDepth();
}
~DeferGC()
{
m_heap.decrementDeferralDepthAndGCIfNeeded();
}
private:
Heap& m_heap;
};
class DeferGCForAWhile {
WTF_MAKE_NONCOPYABLE(DeferGCForAWhile);
public:
DeferGCForAWhile(Heap& heap)
: m_heap(heap)
{
m_heap.incrementDeferralDepth();
}
~DeferGCForAWhile()
{
m_heap.decrementDeferralDepth();
}
private:
Heap& m_heap;
};
class DisallowGC : public DisallowScope<DisallowGC> {
WTF_MAKE_NONCOPYABLE(DisallowGC);
typedef DisallowScope<DisallowGC> Base;
public:
#ifdef NDEBUG
ALWAYS_INLINE DisallowGC(bool = false) { }
ALWAYS_INLINE static void initialize() { }
#else // not NDEBUG
DisallowGC(bool enabled = true)
: Base(enabled)
{ }
static void initialize()
{
WTF::threadSpecificKeyCreate(&s_scopeReentryCount, 0);
}
private:
static uintptr_t scopeReentryCount()
{
return reinterpret_cast<uintptr_t>(WTF::threadSpecificGet(s_scopeReentryCount));
}
static void setScopeReentryCount(uintptr_t value)
{
WTF::threadSpecificSet(s_scopeReentryCount, reinterpret_cast<void*>(value));
}
JS_EXPORT_PRIVATE static WTF::ThreadSpecificKey s_scopeReentryCount;
#endif // NDEBUG
friend class DisallowScope<DisallowGC>;
};
}