ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | fa09f02 | 2014-02-08 00:55:15 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005, 2008, 2012, 2014 Apple Inc. All rights reserved. |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the NU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "JSLock.h" |
| 23 | |
ggaren@apple.com | a8b3854 | 2011-01-10 23:43:56 +0000 | [diff] [blame] | 24 | #include "Heap.h" |
ggaren@apple.com | 5169fc9 | 2008-11-17 22:11:26 +0000 | [diff] [blame] | 25 | #include "CallFrame.h" |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 26 | #include "JSGlobalObject.h" |
oliver@apple.com | 9d4f0ec | 2011-03-14 18:16:36 +0000 | [diff] [blame] | 27 | #include "JSObject.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 28 | #include "JSCInlines.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 29 | #include "SamplingProfiler.h" |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 30 | #include <thread> |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 31 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 32 | namespace JSC { |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 33 | |
fpizlo@apple.com | aa67129 | 2015-08-15 00:14:52 +0000 | [diff] [blame] | 34 | StaticLock GlobalJSLock::s_sharedInstanceMutex; |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 35 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 36 | GlobalJSLock::GlobalJSLock() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 37 | { |
fpizlo@apple.com | aa67129 | 2015-08-15 00:14:52 +0000 | [diff] [blame] | 38 | s_sharedInstanceMutex.lock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 39 | } |
| 40 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 41 | GlobalJSLock::~GlobalJSLock() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 42 | { |
fpizlo@apple.com | aa67129 | 2015-08-15 00:14:52 +0000 | [diff] [blame] | 43 | s_sharedInstanceMutex.unlock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 44 | } |
| 45 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 46 | JSLockHolder::JSLockHolder(ExecState* exec) |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 47 | : m_vm(&exec->vm()) |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 48 | { |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 49 | init(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 50 | } |
| 51 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 52 | JSLockHolder::JSLockHolder(VM* vm) |
| 53 | : m_vm(vm) |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 54 | { |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 55 | init(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 56 | } |
| 57 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 58 | JSLockHolder::JSLockHolder(VM& vm) |
| 59 | : m_vm(&vm) |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 60 | { |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 61 | init(); |
| 62 | } |
| 63 | |
| 64 | void JSLockHolder::init() |
| 65 | { |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 66 | m_vm->apiLock().lock(); |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 67 | } |
| 68 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 69 | JSLockHolder::~JSLockHolder() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 70 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 71 | RefPtr<JSLock> apiLock(&m_vm->apiLock()); |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 72 | m_vm = nullptr; |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 73 | apiLock->unlock(); |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 74 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 75 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 76 | JSLock::JSLock(VM* vm) |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 77 | : m_ownerThreadID(std::thread::id()) |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 78 | , m_lockCount(0) |
commit-queue@webkit.org | e9ad976 | 2012-11-13 17:17:07 +0000 | [diff] [blame] | 79 | , m_lockDropDepth(0) |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 80 | , m_hasExclusiveThread(false) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 81 | , m_vm(vm) |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 82 | , m_entryAtomicStringTable(nullptr) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 83 | { |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 84 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 85 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 86 | JSLock::~JSLock() |
| 87 | { |
| 88 | } |
| 89 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 90 | void JSLock::willDestroyVM(VM* vm) |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 91 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 92 | ASSERT_UNUSED(vm, m_vm == vm); |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 93 | m_vm = nullptr; |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 96 | void JSLock::setExclusiveThread(std::thread::id threadId) |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 97 | { |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 98 | RELEASE_ASSERT(!m_lockCount && m_ownerThreadID == std::thread::id()); |
| 99 | m_hasExclusiveThread = (threadId != std::thread::id()); |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 100 | m_ownerThreadID = threadId; |
| 101 | } |
| 102 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 103 | void JSLock::lock() |
| 104 | { |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 105 | lock(1); |
| 106 | } |
| 107 | |
| 108 | void JSLock::lock(intptr_t lockCount) |
| 109 | { |
| 110 | ASSERT(lockCount > 0); |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 111 | if (currentThreadIsHoldingLock()) { |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 112 | m_lockCount += lockCount; |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 113 | return; |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 114 | } |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 115 | |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 116 | if (!m_hasExclusiveThread) { |
| 117 | m_lock.lock(); |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 118 | m_ownerThreadID = std::this_thread::get_id(); |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 119 | } |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 120 | ASSERT(!m_lockCount); |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 121 | m_lockCount = lockCount; |
msaboff@apple.com | aa9f4ab | 2014-01-31 23:53:52 +0000 | [diff] [blame] | 122 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 123 | didAcquireLock(); |
| 124 | } |
| 125 | |
| 126 | void JSLock::didAcquireLock() |
| 127 | { |
| 128 | // FIXME: What should happen to the per-thread identifier table if we don't have a VM? |
mhahnenberg@apple.com | ac9e2dc | 2014-02-15 01:27:53 +0000 | [diff] [blame] | 129 | if (!m_vm) |
| 130 | return; |
| 131 | |
mark.lam@apple.com | 5e1200e | 2014-02-13 06:50:09 +0000 | [diff] [blame] | 132 | RELEASE_ASSERT(!m_vm->stackPointerAtVMEntry()); |
| 133 | void* p = &p; // A proxy for the current stack pointer. |
| 134 | m_vm->setStackPointerAtVMEntry(p); |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 135 | |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 136 | WTFThreadData& threadData = wtfThreadData(); |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 137 | m_vm->setLastStackTop(threadData.savedLastStackTop()); |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 138 | |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 139 | ASSERT(!m_entryAtomicStringTable); |
barraclough@apple.com | 0cde90d | 2014-03-20 21:05:49 +0000 | [diff] [blame] | 140 | m_entryAtomicStringTable = threadData.setCurrentAtomicStringTable(m_vm->atomicStringTable()); |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 141 | ASSERT(m_entryAtomicStringTable); |
| 142 | |
mark.lam@apple.com | 6aba436 | 2015-02-26 19:44:08 +0000 | [diff] [blame] | 143 | m_vm->heap.machineThreads().addCurrentThread(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 144 | |
| 145 | #if ENABLE(SAMPLING_PROFILER) |
| 146 | // Note: this must come after addCurrentThread(). |
| 147 | if (SamplingProfiler* samplingProfiler = m_vm->samplingProfiler()) |
| 148 | samplingProfiler->noticeJSLockAcquisition(); |
| 149 | #endif |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 150 | } |
| 151 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 152 | void JSLock::unlock() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 153 | { |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 154 | unlock(1); |
| 155 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 156 | |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 157 | void JSLock::unlock(intptr_t unlockCount) |
| 158 | { |
| 159 | RELEASE_ASSERT(currentThreadIsHoldingLock()); |
| 160 | ASSERT(m_lockCount >= unlockCount); |
| 161 | |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 162 | // Maintain m_lockCount while calling willReleaseLock() so that its callees know that |
| 163 | // they still have the lock. |
| 164 | if (unlockCount == m_lockCount) |
| 165 | willReleaseLock(); |
| 166 | |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 167 | m_lockCount -= unlockCount; |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 168 | |
msaboff@apple.com | aa9f4ab | 2014-01-31 23:53:52 +0000 | [diff] [blame] | 169 | if (!m_lockCount) { |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 170 | |
| 171 | if (!m_hasExclusiveThread) { |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 172 | m_ownerThreadID = std::thread::id(); |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 173 | m_lock.unlock(); |
| 174 | } |
msaboff@apple.com | aa9f4ab | 2014-01-31 23:53:52 +0000 | [diff] [blame] | 175 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 176 | } |
| 177 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 178 | void JSLock::willReleaseLock() |
| 179 | { |
mark.lam@apple.com | 416a13e | 2016-05-19 21:02:44 +0000 | [diff] [blame] | 180 | RefPtr<VM> vm = m_vm; |
| 181 | if (vm) { |
| 182 | vm->drainMicrotasks(); |
utatane.tea@gmail.com | ff51b69 | 2015-07-17 22:40:40 +0000 | [diff] [blame] | 183 | |
mark.lam@apple.com | 416a13e | 2016-05-19 21:02:44 +0000 | [diff] [blame] | 184 | vm->heap.releaseDelayedReleasedObjects(); |
| 185 | vm->setStackPointerAtVMEntry(nullptr); |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 186 | } |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 187 | |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 188 | if (m_entryAtomicStringTable) { |
| 189 | wtfThreadData().setCurrentAtomicStringTable(m_entryAtomicStringTable); |
| 190 | m_entryAtomicStringTable = nullptr; |
| 191 | } |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 192 | } |
| 193 | |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 194 | void JSLock::lock(ExecState* exec) |
| 195 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 196 | exec->vm().apiLock().lock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void JSLock::unlock(ExecState* exec) |
| 200 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 201 | exec->vm().apiLock().unlock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | bool JSLock::currentThreadIsHoldingLock() |
| 205 | { |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 206 | ASSERT(!m_hasExclusiveThread || (exclusiveThread() == std::this_thread::get_id())); |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 207 | if (m_hasExclusiveThread) |
| 208 | return !!m_lockCount; |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 209 | return m_ownerThreadID == std::this_thread::get_id(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 210 | } |
| 211 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 212 | // This function returns the number of locks that were dropped. |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 213 | unsigned JSLock::dropAllLocks(DropAllLocks* dropper) |
mhahnenberg@apple.com | 6d9f86d | 2012-06-22 21:42:46 +0000 | [diff] [blame] | 214 | { |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 215 | if (m_hasExclusiveThread) { |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 216 | ASSERT(exclusiveThread() == std::this_thread::get_id()); |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 220 | if (!currentThreadIsHoldingLock()) |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 221 | return 0; |
| 222 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 223 | ++m_lockDropDepth; |
| 224 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 225 | dropper->setDropDepth(m_lockDropDepth); |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 226 | |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 227 | WTFThreadData& threadData = wtfThreadData(); |
mark.lam@apple.com | 5e1200e | 2014-02-13 06:50:09 +0000 | [diff] [blame] | 228 | threadData.setSavedStackPointerAtVMEntry(m_vm->stackPointerAtVMEntry()); |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 229 | threadData.setSavedLastStackTop(m_vm->lastStackTop()); |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 230 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 231 | unsigned droppedLockCount = m_lockCount; |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 232 | unlock(droppedLockCount); |
| 233 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 234 | return droppedLockCount; |
mhahnenberg@apple.com | 6d9f86d | 2012-06-22 21:42:46 +0000 | [diff] [blame] | 235 | } |
| 236 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 237 | void JSLock::grabAllLocks(DropAllLocks* dropper, unsigned droppedLockCount) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 238 | { |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 239 | ASSERT(!m_hasExclusiveThread || !droppedLockCount); |
| 240 | |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 241 | // If no locks were dropped, nothing to do! |
mark.lam@apple.com | cbebdc4 | 2014-02-09 00:46:17 +0000 | [diff] [blame] | 242 | if (!droppedLockCount) |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 243 | return; |
| 244 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 245 | ASSERT(!currentThreadIsHoldingLock()); |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 246 | lock(droppedLockCount); |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 247 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 248 | while (dropper->dropDepth() != m_lockDropDepth) { |
| 249 | unlock(droppedLockCount); |
| 250 | std::this_thread::yield(); |
| 251 | lock(droppedLockCount); |
| 252 | } |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 253 | |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 254 | --m_lockDropDepth; |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 255 | |
| 256 | WTFThreadData& threadData = wtfThreadData(); |
mark.lam@apple.com | 5e1200e | 2014-02-13 06:50:09 +0000 | [diff] [blame] | 257 | m_vm->setStackPointerAtVMEntry(threadData.savedStackPointerAtVMEntry()); |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 258 | m_vm->setLastStackTop(threadData.savedLastStackTop()); |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 259 | } |
| 260 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 261 | JSLock::DropAllLocks::DropAllLocks(VM* vm) |
mark.lam@apple.com | cbebdc4 | 2014-02-09 00:46:17 +0000 | [diff] [blame] | 262 | : m_droppedLockCount(0) |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 263 | // If the VM is in the middle of being destroyed then we don't want to resurrect it |
| 264 | // by allowing DropAllLocks to ref it. By this point the JSLock has already been |
| 265 | // released anyways, so it doesn't matter that DropAllLocks is a no-op. |
| 266 | , m_vm(vm->refCount() ? vm : nullptr) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 267 | { |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 268 | if (!m_vm) |
| 269 | return; |
barraclough@apple.com | 0cde90d | 2014-03-20 21:05:49 +0000 | [diff] [blame] | 270 | wtfThreadData().resetCurrentAtomicStringTable(); |
ggaren@apple.com | 1af6f96 | 2015-06-05 22:03:50 +0000 | [diff] [blame] | 271 | RELEASE_ASSERT(!m_vm->apiLock().currentThreadIsHoldingLock() || !m_vm->isCollectorBusy()); |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 272 | m_droppedLockCount = m_vm->apiLock().dropAllLocks(this); |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 273 | } |
| 274 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 275 | JSLock::DropAllLocks::DropAllLocks(ExecState* exec) |
| 276 | : DropAllLocks(exec ? &exec->vm() : nullptr) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 277 | { |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 278 | } |
| 279 | |
akling@apple.com | 710fc6a | 2014-03-03 05:10:15 +0000 | [diff] [blame] | 280 | JSLock::DropAllLocks::DropAllLocks(VM& vm) |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 281 | : DropAllLocks(&vm) |
akling@apple.com | 710fc6a | 2014-03-03 05:10:15 +0000 | [diff] [blame] | 282 | { |
akling@apple.com | 710fc6a | 2014-03-03 05:10:15 +0000 | [diff] [blame] | 283 | } |
| 284 | |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 285 | JSLock::DropAllLocks::~DropAllLocks() |
| 286 | { |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 287 | if (!m_vm) |
| 288 | return; |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 289 | m_vm->apiLock().grabAllLocks(this, m_droppedLockCount); |
barraclough@apple.com | 0cde90d | 2014-03-20 21:05:49 +0000 | [diff] [blame] | 290 | wtfThreadData().setCurrentAtomicStringTable(m_vm->atomicStringTable()); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 291 | } |
| 292 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 293 | } // namespace JSC |