#pragma once
#include <mutex>
#include <wtf/Assertions.h>
#include <wtf/ForbidHeapAllocation.h>
#include <wtf/Lock.h>
#include <wtf/Noncopyable.h>
#include <wtf/RefPtr.h>
#include <wtf/Threading.h>
#include <wtf/text/AtomStringTable.h>
namespace JSC {
class CallFrame;
class VM;
class JSGlobalObject;
class JSLock;
class GlobalJSLock {
WTF_MAKE_NONCOPYABLE(GlobalJSLock);
public:
JS_EXPORT_PRIVATE GlobalJSLock();
JS_EXPORT_PRIVATE ~GlobalJSLock();
private:
static Lock s_sharedInstanceMutex;
};
class JSLockHolder {
public:
JS_EXPORT_PRIVATE JSLockHolder(VM*);
JS_EXPORT_PRIVATE JSLockHolder(VM&);
JS_EXPORT_PRIVATE JSLockHolder(JSGlobalObject*);
JS_EXPORT_PRIVATE ~JSLockHolder();
private:
RefPtr<VM> m_vm;
};
class JSLock : public ThreadSafeRefCounted<JSLock> {
WTF_MAKE_NONCOPYABLE(JSLock);
public:
JSLock(VM*);
JS_EXPORT_PRIVATE ~JSLock();
JS_EXPORT_PRIVATE void lock();
JS_EXPORT_PRIVATE void unlock();
static void lock(JSGlobalObject*);
static void unlock(JSGlobalObject*);
static void lock(VM&);
static void unlock(VM&);
VM* vm() { return m_vm; }
Optional<RefPtr<Thread>> ownerThread() const
{
if (m_hasOwnerThread)
return m_ownerThread;
return WTF::nullopt;
}
bool currentThreadIsHoldingLock() { return m_hasOwnerThread && m_ownerThread.get() == &Thread::current(); }
void willDestroyVM(VM*);
class DropAllLocks {
WTF_MAKE_NONCOPYABLE(DropAllLocks);
public:
JS_EXPORT_PRIVATE DropAllLocks(JSGlobalObject*);
JS_EXPORT_PRIVATE DropAllLocks(VM*);
JS_EXPORT_PRIVATE DropAllLocks(VM&);
JS_EXPORT_PRIVATE ~DropAllLocks();
void setDropDepth(unsigned depth) { m_dropDepth = depth; }
unsigned dropDepth() const { return m_dropDepth; }
private:
intptr_t m_droppedLockCount;
RefPtr<VM> m_vm;
unsigned m_dropDepth;
};
void makeWebThreadAware()
{
m_isWebThreadAware = true;
}
bool isWebThreadAware() const { return m_isWebThreadAware; }
private:
void lock(intptr_t lockCount);
void unlock(intptr_t unlockCount);
void didAcquireLock();
void willReleaseLock();
unsigned dropAllLocks(DropAllLocks*);
void grabAllLocks(DropAllLocks*, unsigned lockCount);
Lock m_lock;
bool m_isWebThreadAware { false };
bool m_hasOwnerThread { false };
bool m_shouldReleaseHeapAccess;
RefPtr<Thread> m_ownerThread;
intptr_t m_lockCount;
unsigned m_lockDropDepth;
uint32_t m_lastOwnerThread { 0 };
VM* m_vm;
AtomStringTable* m_entryAtomStringTable;
};
}