/*
- * Copyright (C) 2005, 2008, 2009 Apple Inc. All rights reserved.
+ * Copyright (C) 2005, 2008, 2009, 2014 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
#ifndef JSLock_h
#define JSLock_h
+#include <mutex>
+#include <thread>
#include <wtf/Assertions.h>
#include <wtf/Noncopyable.h>
+#include <wtf/RefPtr.h>
+#include <wtf/ThreadSafeRefCounted.h>
+#include <wtf/WTFThreadData.h>
namespace JSC {
// important to lock before doing anything that allocates a
// JavaScript data structure or that interacts with shared state
// such as the protect count hash table. The simplest way to lock
- // is to create a local JSLock object in the scope where the lock
- // must be held. The lock is recursive so nesting is ok. The JSLock
+ // is to create a local JSLockHolder object in the scope where the lock
+ // must be held and pass it the context that requires protection.
+ // The lock is recursive so nesting is ok. The JSLock
// object also acts as a convenience short-hand for running important
// initialization routines.
// DropAllLocks object takes care to release the JSLock only if your
// thread acquired it to begin with.
- // For contexts other than the single shared one, implicit locking is not done,
- // but we still need to perform all the counting in order to keep debug
- // assertions working, so that clients that use the shared context don't break.
-
class ExecState;
+ class VM;
- enum JSLockBehavior { SilenceAssertionsOnly, LockForReal };
+ // This class is used to protect the initialization of the legacy single
+ // shared VM.
+ class GlobalJSLock {
+ WTF_MAKE_NONCOPYABLE(GlobalJSLock);
+ public:
+ JS_EXPORT_PRIVATE GlobalJSLock();
+ JS_EXPORT_PRIVATE ~GlobalJSLock();
- class JSLock : public Noncopyable {
+ static void initialize();
+ private:
+ static std::mutex* s_sharedInstanceMutex;
+ };
+
+ class JSLockHolder {
public:
- JSLock(ExecState*);
+ JS_EXPORT_PRIVATE JSLockHolder(VM*);
+ JS_EXPORT_PRIVATE JSLockHolder(VM&);
+ JS_EXPORT_PRIVATE JSLockHolder(ExecState*);
- JSLock(JSLockBehavior lockBehavior)
- : m_lockBehavior(lockBehavior)
- {
-#ifdef NDEBUG
- // Locking "not for real" is a debug-only feature.
- if (lockBehavior == SilenceAssertionsOnly)
- return;
-#endif
- lock(lockBehavior);
- }
+ JS_EXPORT_PRIVATE ~JSLockHolder();
+ private:
+ void init();
+
+ 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();
- ~JSLock()
- {
-#ifdef NDEBUG
- // Locking "not for real" is a debug-only feature.
- if (m_lockBehavior == SilenceAssertionsOnly)
- return;
-#endif
- unlock(m_lockBehavior);
- }
-
- static void lock(JSLockBehavior);
- static void unlock(JSLockBehavior);
static void lock(ExecState*);
static void unlock(ExecState*);
+ static void lock(VM&);
+ static void unlock(VM&);
+
+ VM* vm() { return m_vm; }
- static intptr_t lockCount();
- static bool currentThreadIsHoldingLock();
+ bool hasExclusiveThread() const { return m_hasExclusiveThread; }
+ std::thread::id exclusiveThread() const
+ {
+ ASSERT(m_hasExclusiveThread);
+ return m_ownerThreadID;
+ }
+ JS_EXPORT_PRIVATE void setExclusiveThread(std::thread::id);
+ JS_EXPORT_PRIVATE bool currentThreadIsHoldingLock();
- JSLockBehavior m_lockBehavior;
+ void willDestroyVM(VM*);
- class DropAllLocks : public Noncopyable {
+ class DropAllLocks {
+ WTF_MAKE_NONCOPYABLE(DropAllLocks);
public:
- DropAllLocks(ExecState* exec);
- DropAllLocks(JSLockBehavior);
- ~DropAllLocks();
+ JS_EXPORT_PRIVATE DropAllLocks(ExecState*);
+ 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_lockCount;
- JSLockBehavior m_lockBehavior;
+ intptr_t m_droppedLockCount;
+ RefPtr<VM> m_vm;
+ unsigned m_dropDepth;
};
+
+ private:
+ void lock(intptr_t lockCount);
+ void unlock(intptr_t unlockCount);
+
+ void didAcquireLock();
+ void willReleaseLock();
+
+ unsigned dropAllLocks(DropAllLocks*);
+ void grabAllLocks(DropAllLocks*, unsigned lockCount);
+
+ std::mutex m_lock;
+ std::thread::id m_ownerThreadID;
+ intptr_t m_lockCount;
+ unsigned m_lockDropDepth;
+ bool m_hasExclusiveThread;
+ VM* m_vm;
+ AtomicStringTable* m_entryAtomicStringTable;
};
} // namespace