Remove setExclusiveThread() and peers from the JSLock.
https://bugs.webkit.org/show_bug.cgi?id=168977

Reviewed by Filip Pizlo.

Source/JavaScriptCore:

JSLock::setExclusiveThread() was only used by WebCore.  Benchmarking with
Speedometer, we see that removal of exclusive thread status has no measurable
impact on performance.  So, let's remove the code for handling exclusive thread
status, and simplify the JSLock code.

For the records, exclusive thread status does improve JSLock locking/unlocking
time by up to 20%.  However, this difference is not measurable in the way WebCore
uses the JSLock as confirmed by Speedometer.

Also applied a minor optimization in JSLock::lock() to assume the initial lock
entry case (as opposed to the re-entry case).  This appears to shows a small
fractional improvement (about 5%) in JSLock cumulative locking and unlocking
time in a micro-benchmark.

* heap/Heap.cpp:
(JSC::Heap::Heap):
* heap/MachineStackMarker.cpp:
(JSC::MachineThreads::MachineThreads):
(JSC::MachineThreads::addCurrentThread):
* heap/MachineStackMarker.h:
* runtime/JSLock.cpp:
(JSC::JSLock::JSLock):
(JSC::JSLock::lock):
(JSC::JSLock::unlock):
(JSC::JSLock::currentThreadIsHoldingLock):
(JSC::JSLock::dropAllLocks):
(JSC::JSLock::grabAllLocks):
(JSC::JSLock::setExclusiveThread): Deleted.
* runtime/JSLock.h:
(JSC::JSLock::ownerThread):
(JSC::JSLock::hasExclusiveThread): Deleted.
(JSC::JSLock::exclusiveThread): Deleted.
* runtime/VM.h:
(JSC::VM::hasExclusiveThread): Deleted.
(JSC::VM::exclusiveThread): Deleted.
(JSC::VM::setExclusiveThread): Deleted.

Source/WebCore:

No new tests because this should already be covered by existing tests.

* bindings/js/CommonVM.cpp:
(WebCore::commonVMSlow):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@213175 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/runtime/JSLock.cpp b/Source/JavaScriptCore/runtime/JSLock.cpp
index 329d22d..72b21df 100644
--- a/Source/JavaScriptCore/runtime/JSLock.cpp
+++ b/Source/JavaScriptCore/runtime/JSLock.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005, 2008, 2012, 2014, 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2005-2017 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
@@ -78,7 +78,6 @@
     : m_ownerThreadID(std::thread::id())
     , m_lockCount(0)
     , m_lockDropDepth(0)
-    , m_hasExclusiveThread(false)
     , m_vm(vm)
     , m_entryAtomicStringTable(nullptr)
 {
@@ -94,13 +93,6 @@
     m_vm = nullptr;
 }
 
-void JSLock::setExclusiveThread(std::thread::id threadId)
-{
-    RELEASE_ASSERT(!m_lockCount && m_ownerThreadID == std::thread::id());
-    m_hasExclusiveThread = (threadId != std::thread::id());
-    m_ownerThreadID = threadId;
-}
-
 void JSLock::lock()
 {
     lock(1);
@@ -109,15 +101,16 @@
 void JSLock::lock(intptr_t lockCount)
 {
     ASSERT(lockCount > 0);
-    if (currentThreadIsHoldingLock()) {
-        m_lockCount += lockCount;
-        return;
+    bool success = m_lock.tryLock();
+    if (UNLIKELY(!success)) {
+        if (currentThreadIsHoldingLock()) {
+            m_lockCount += lockCount;
+            return;
+        }
+        m_lock.lock();
     }
 
-    if (!m_hasExclusiveThread) {
-        m_lock.lock();
-        m_ownerThreadID = std::this_thread::get_id();
-    }
+    m_ownerThreadID = std::this_thread::get_id();
     ASSERT(!m_lockCount);
     m_lockCount = lockCount;
 
@@ -175,11 +168,8 @@
     m_lockCount -= unlockCount;
 
     if (!m_lockCount) {
-        
-        if (!m_hasExclusiveThread) {
-            m_ownerThreadID = std::thread::id();
-            m_lock.unlock();
-        }
+        m_ownerThreadID = std::thread::id();
+        m_lock.unlock();
     }
 }
 
@@ -214,20 +204,12 @@
 
 bool JSLock::currentThreadIsHoldingLock()
 {
-    ASSERT(!m_hasExclusiveThread || (exclusiveThread() == std::this_thread::get_id()));
-    if (m_hasExclusiveThread)
-        return !!m_lockCount;
     return m_ownerThreadID == std::this_thread::get_id();
 }
 
 // This function returns the number of locks that were dropped.
 unsigned JSLock::dropAllLocks(DropAllLocks* dropper)
 {
-    if (m_hasExclusiveThread) {
-        ASSERT(exclusiveThread() == std::this_thread::get_id());
-        return 0;
-    }
-
     if (!currentThreadIsHoldingLock())
         return 0;
 
@@ -247,8 +229,6 @@
 
 void JSLock::grabAllLocks(DropAllLocks* dropper, unsigned droppedLockCount)
 {
-    ASSERT(!m_hasExclusiveThread || !droppedLockCount);
-
     // If no locks were dropped, nothing to do!
     if (!droppedLockCount)
         return;