ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 47d0cf7 | 2018-01-25 19:32:00 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005-2018 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" |
fpizlo@apple.com | bb9f63b | 2017-02-22 01:05:13 +0000 | [diff] [blame] | 29 | #include "MachineStackMarker.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 30 | #include "SamplingProfiler.h" |
ysuzuki@apple.com | 2f87539 | 2019-03-21 19:14:12 +0000 | [diff] [blame] | 31 | #include "WasmCapabilities.h" |
keith_miller@apple.com | 5fe2e70 | 2017-04-26 23:55:05 +0000 | [diff] [blame] | 32 | #include "WasmMachineThreads.h" |
mark.lam@apple.com | 49d3b25 | 2015-07-20 18:12:56 +0000 | [diff] [blame] | 33 | #include <thread> |
mark.lam@apple.com | 6c4490c | 2018-10-11 19:19:18 +0000 | [diff] [blame] | 34 | #include <wtf/StackPointer.h> |
keith_miller@apple.com | ea98d8d | 2017-06-01 18:28:48 +0000 | [diff] [blame] | 35 | #include <wtf/Threading.h> |
| 36 | #include <wtf/threads/Signals.h> |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 37 | |
ysuzuki@apple.com | 9f88061 | 2019-06-19 01:19:41 +0000 | [diff] [blame] | 38 | #if USE(WEB_THREAD) |
| 39 | #include <wtf/ios/WebCoreThread.h> |
| 40 | #endif |
| 41 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 42 | namespace JSC { |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 43 | |
utatane.tea@gmail.com | e71a872 | 2018-04-05 17:22:21 +0000 | [diff] [blame] | 44 | Lock GlobalJSLock::s_sharedInstanceMutex; |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 45 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 46 | GlobalJSLock::GlobalJSLock() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 47 | { |
fpizlo@apple.com | aa67129 | 2015-08-15 00:14:52 +0000 | [diff] [blame] | 48 | s_sharedInstanceMutex.lock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 49 | } |
| 50 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 51 | GlobalJSLock::~GlobalJSLock() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 52 | { |
fpizlo@apple.com | aa67129 | 2015-08-15 00:14:52 +0000 | [diff] [blame] | 53 | s_sharedInstanceMutex.unlock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 54 | } |
| 55 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 56 | JSLockHolder::JSLockHolder(JSGlobalObject* globalObject) |
| 57 | : JSLockHolder(globalObject->vm()) |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 58 | { |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 59 | } |
| 60 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 61 | JSLockHolder::JSLockHolder(VM* vm) |
ysuzuki@apple.com | 9f88061 | 2019-06-19 01:19:41 +0000 | [diff] [blame] | 62 | : JSLockHolder(*vm) |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 63 | { |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 64 | } |
| 65 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 66 | JSLockHolder::JSLockHolder(VM& vm) |
| 67 | : m_vm(&vm) |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 68 | { |
mark.lam@apple.com | afd0607 | 2014-02-26 01:03:37 +0000 | [diff] [blame] | 69 | m_vm->apiLock().lock(); |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 70 | } |
| 71 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 72 | JSLockHolder::~JSLockHolder() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 73 | { |
tsavell@apple.com | 899b2dd | 2019-10-04 15:35:03 +0000 | [diff] [blame] | 74 | RefPtr<JSLock> apiLock(&m_vm->apiLock()); |
| 75 | m_vm = nullptr; |
| 76 | apiLock->unlock(); |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 77 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 78 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 79 | JSLock::JSLock(VM* vm) |
mark.lam@apple.com | 8bae071 | 2017-03-01 20:15:08 +0000 | [diff] [blame] | 80 | : m_lockCount(0) |
commit-queue@webkit.org | e9ad976 | 2012-11-13 17:17:07 +0000 | [diff] [blame] | 81 | , m_lockDropDepth(0) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 82 | , m_vm(vm) |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 83 | , m_entryAtomStringTable(nullptr) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 84 | { |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 85 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 86 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 87 | JSLock::~JSLock() |
| 88 | { |
| 89 | } |
| 90 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 91 | void JSLock::willDestroyVM(VM* vm) |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 92 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 93 | ASSERT_UNUSED(vm, m_vm == vm); |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 94 | m_vm = nullptr; |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 95 | } |
| 96 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 97 | void JSLock::lock() |
| 98 | { |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 99 | lock(1); |
| 100 | } |
| 101 | |
| 102 | void JSLock::lock(intptr_t lockCount) |
| 103 | { |
| 104 | ASSERT(lockCount > 0); |
ysuzuki@apple.com | 9f88061 | 2019-06-19 01:19:41 +0000 | [diff] [blame] | 105 | #if USE(WEB_THREAD) |
| 106 | if (m_isWebThreadAware) { |
| 107 | ASSERT(WebCoreWebThreadIsEnabled && WebCoreWebThreadIsEnabled()); |
| 108 | WebCoreWebThreadLock(); |
| 109 | } |
| 110 | #endif |
| 111 | |
mark.lam@apple.com | 0a7ae13 | 2017-02-28 21:56:44 +0000 | [diff] [blame] | 112 | bool success = m_lock.tryLock(); |
| 113 | if (UNLIKELY(!success)) { |
| 114 | if (currentThreadIsHoldingLock()) { |
| 115 | m_lockCount += lockCount; |
| 116 | return; |
| 117 | } |
| 118 | m_lock.lock(); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 119 | } |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 120 | |
utatane.tea@gmail.com | 1e94a26 | 2017-04-12 12:08:29 +0000 | [diff] [blame] | 121 | m_ownerThread = &Thread::current(); |
mark.lam@apple.com | 8bae071 | 2017-03-01 20:15:08 +0000 | [diff] [blame] | 122 | WTF::storeStoreFence(); |
| 123 | m_hasOwnerThread = true; |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 124 | ASSERT(!m_lockCount); |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 125 | m_lockCount = lockCount; |
msaboff@apple.com | aa9f4ab | 2014-01-31 23:53:52 +0000 | [diff] [blame] | 126 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 127 | didAcquireLock(); |
| 128 | } |
| 129 | |
| 130 | void JSLock::didAcquireLock() |
commit-queue@webkit.org | 0b186d5 | 2018-08-07 12:50:23 +0000 | [diff] [blame] | 131 | { |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 132 | // 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] | 133 | if (!m_vm) |
| 134 | return; |
fpizlo@apple.com | 8b6ca58 | 2016-11-02 22:01:04 +0000 | [diff] [blame] | 135 | |
utatane.tea@gmail.com | 843c4ac | 2017-08-03 06:03:18 +0000 | [diff] [blame] | 136 | Thread& thread = Thread::current(); |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 137 | ASSERT(!m_entryAtomStringTable); |
| 138 | m_entryAtomStringTable = thread.setCurrentAtomStringTable(m_vm->atomStringTable()); |
| 139 | ASSERT(m_entryAtomStringTable); |
fpizlo@apple.com | 8b6ca58 | 2016-11-02 22:01:04 +0000 | [diff] [blame] | 140 | |
msaboff@apple.com | 94801be | 2018-06-20 00:45:06 +0000 | [diff] [blame] | 141 | m_vm->setLastStackTop(thread.savedLastStackTop()); |
| 142 | ASSERT(thread.stack().contains(m_vm->lastStackTop())); |
| 143 | |
fpizlo@apple.com | 8b6ca58 | 2016-11-02 22:01:04 +0000 | [diff] [blame] | 144 | if (m_vm->heap.hasAccess()) |
| 145 | m_shouldReleaseHeapAccess = false; |
| 146 | else { |
| 147 | m_vm->heap.acquireAccess(); |
| 148 | m_shouldReleaseHeapAccess = true; |
| 149 | } |
mhahnenberg@apple.com | ac9e2dc | 2014-02-15 01:27:53 +0000 | [diff] [blame] | 150 | |
mark.lam@apple.com | 5e1200e | 2014-02-13 06:50:09 +0000 | [diff] [blame] | 151 | RELEASE_ASSERT(!m_vm->stackPointerAtVMEntry()); |
mark.lam@apple.com | 6c4490c | 2018-10-11 19:19:18 +0000 | [diff] [blame] | 152 | void* p = currentStackPointer(); |
mark.lam@apple.com | 5e1200e | 2014-02-13 06:50:09 +0000 | [diff] [blame] | 153 | m_vm->setStackPointerAtVMEntry(p); |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 154 | |
sbarati@apple.com | 4811678 | 2019-04-12 23:26:43 +0000 | [diff] [blame] | 155 | if (m_vm->heap.machineThreads().addCurrentThread()) { |
| 156 | if (isKernTCSMAvailable()) |
| 157 | enableKernTCSM(); |
| 158 | } |
| 159 | |
keith_miller@apple.com | 5fe2e70 | 2017-04-26 23:55:05 +0000 | [diff] [blame] | 160 | #if ENABLE(WEBASSEMBLY) |
ysuzuki@apple.com | 2f87539 | 2019-03-21 19:14:12 +0000 | [diff] [blame] | 161 | if (Wasm::isSupported()) |
ysuzuki@apple.com | a0cd789 | 2019-02-16 00:18:54 +0000 | [diff] [blame] | 162 | Wasm::startTrackingCurrentThread(); |
keith_miller@apple.com | 5fe2e70 | 2017-04-26 23:55:05 +0000 | [diff] [blame] | 163 | #endif |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 164 | |
keith_miller@apple.com | ea98d8d | 2017-06-01 18:28:48 +0000 | [diff] [blame] | 165 | #if HAVE(MACH_EXCEPTIONS) |
utatane.tea@gmail.com | f872777 | 2017-07-20 18:32:13 +0000 | [diff] [blame] | 166 | registerThreadForMachExceptionHandling(Thread::current()); |
keith_miller@apple.com | ea98d8d | 2017-06-01 18:28:48 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
keith_miller@apple.com | c670c53 | 2017-06-29 17:34:57 +0000 | [diff] [blame] | 169 | // Note: everything below must come after addCurrentThread(). |
mark.lam@apple.com | f842cd8 | 2017-03-09 19:08:46 +0000 | [diff] [blame] | 170 | m_vm->traps().notifyGrabAllLocks(); |
fpizlo@apple.com | d2bbe27 | 2017-08-02 01:50:16 +0000 | [diff] [blame] | 171 | |
fpizlo@apple.com | c206913b | 2017-08-07 21:31:49 +0000 | [diff] [blame] | 172 | m_vm->firePrimitiveGigacageEnabledIfNecessary(); |
mark.lam@apple.com | f842cd8 | 2017-03-09 19:08:46 +0000 | [diff] [blame] | 173 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 174 | #if ENABLE(SAMPLING_PROFILER) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 175 | if (SamplingProfiler* samplingProfiler = m_vm->samplingProfiler()) |
| 176 | samplingProfiler->noticeJSLockAcquisition(); |
| 177 | #endif |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 178 | } |
| 179 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 180 | void JSLock::unlock() |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 181 | { |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 182 | unlock(1); |
| 183 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 184 | |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 185 | void JSLock::unlock(intptr_t unlockCount) |
| 186 | { |
| 187 | RELEASE_ASSERT(currentThreadIsHoldingLock()); |
| 188 | ASSERT(m_lockCount >= unlockCount); |
| 189 | |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 190 | // Maintain m_lockCount while calling willReleaseLock() so that its callees know that |
| 191 | // they still have the lock. |
| 192 | if (unlockCount == m_lockCount) |
| 193 | willReleaseLock(); |
| 194 | |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 195 | m_lockCount -= unlockCount; |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 196 | |
msaboff@apple.com | aa9f4ab | 2014-01-31 23:53:52 +0000 | [diff] [blame] | 197 | if (!m_lockCount) { |
mark.lam@apple.com | 8bae071 | 2017-03-01 20:15:08 +0000 | [diff] [blame] | 198 | m_hasOwnerThread = false; |
mark.lam@apple.com | 0a7ae13 | 2017-02-28 21:56:44 +0000 | [diff] [blame] | 199 | m_lock.unlock(); |
msaboff@apple.com | aa9f4ab | 2014-01-31 23:53:52 +0000 | [diff] [blame] | 200 | } |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 201 | } |
| 202 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 203 | void JSLock::willReleaseLock() |
commit-queue@webkit.org | 0b186d5 | 2018-08-07 12:50:23 +0000 | [diff] [blame] | 204 | { |
mark.lam@apple.com | 416a13e | 2016-05-19 21:02:44 +0000 | [diff] [blame] | 205 | RefPtr<VM> vm = m_vm; |
| 206 | if (vm) { |
keith_miller@apple.com | a1c17ed | 2020-01-17 04:09:32 +0000 | [diff] [blame] | 207 | RELEASE_ASSERT_WITH_MESSAGE(!vm->hasCheckpointOSRSideState(), "Releasing JSLock but pending checkpoint side state still available"); |
mark.lam@apple.com | 416a13e | 2016-05-19 21:02:44 +0000 | [diff] [blame] | 208 | vm->drainMicrotasks(); |
utatane.tea@gmail.com | ff51b69 | 2015-07-17 22:40:40 +0000 | [diff] [blame] | 209 | |
keith_miller@apple.com | efd353c | 2018-06-04 21:51:04 +0000 | [diff] [blame] | 210 | if (!vm->topCallFrame) |
| 211 | vm->clearLastException(); |
| 212 | |
mark.lam@apple.com | 416a13e | 2016-05-19 21:02:44 +0000 | [diff] [blame] | 213 | vm->heap.releaseDelayedReleasedObjects(); |
| 214 | vm->setStackPointerAtVMEntry(nullptr); |
fpizlo@apple.com | 8b6ca58 | 2016-11-02 22:01:04 +0000 | [diff] [blame] | 215 | |
| 216 | if (m_shouldReleaseHeapAccess) |
| 217 | vm->heap.releaseAccess(); |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 218 | } |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 219 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 220 | if (m_entryAtomStringTable) { |
| 221 | Thread::current().setCurrentAtomStringTable(m_entryAtomStringTable); |
| 222 | m_entryAtomStringTable = nullptr; |
joepeck@webkit.org | 6e82991 | 2014-07-24 23:56:34 +0000 | [diff] [blame] | 223 | } |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 224 | } |
| 225 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 226 | void JSLock::lock(JSGlobalObject* globalObject) |
tsavell@apple.com | 899b2dd | 2019-10-04 15:35:03 +0000 | [diff] [blame] | 227 | { |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 228 | globalObject->vm().apiLock().lock(); |
tsavell@apple.com | 899b2dd | 2019-10-04 15:35:03 +0000 | [diff] [blame] | 229 | } |
| 230 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 231 | void JSLock::unlock(JSGlobalObject* globalObject) |
tsavell@apple.com | 899b2dd | 2019-10-04 15:35:03 +0000 | [diff] [blame] | 232 | { |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 233 | globalObject->vm().apiLock().unlock(); |
tsavell@apple.com | 899b2dd | 2019-10-04 15:35:03 +0000 | [diff] [blame] | 234 | } |
| 235 | |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 236 | // This function returns the number of locks that were dropped. |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 237 | unsigned JSLock::dropAllLocks(DropAllLocks* dropper) |
mhahnenberg@apple.com | 6d9f86d | 2012-06-22 21:42:46 +0000 | [diff] [blame] | 238 | { |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 239 | if (!currentThreadIsHoldingLock()) |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 240 | return 0; |
| 241 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 242 | ++m_lockDropDepth; |
| 243 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 244 | dropper->setDropDepth(m_lockDropDepth); |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 245 | |
utatane.tea@gmail.com | 843c4ac | 2017-08-03 06:03:18 +0000 | [diff] [blame] | 246 | Thread& thread = Thread::current(); |
| 247 | thread.setSavedStackPointerAtVMEntry(m_vm->stackPointerAtVMEntry()); |
| 248 | thread.setSavedLastStackTop(m_vm->lastStackTop()); |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 249 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 250 | unsigned droppedLockCount = m_lockCount; |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 251 | unlock(droppedLockCount); |
| 252 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 253 | return droppedLockCount; |
mhahnenberg@apple.com | 6d9f86d | 2012-06-22 21:42:46 +0000 | [diff] [blame] | 254 | } |
| 255 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 256 | void JSLock::grabAllLocks(DropAllLocks* dropper, unsigned droppedLockCount) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 257 | { |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 258 | // If no locks were dropped, nothing to do! |
mark.lam@apple.com | cbebdc4 | 2014-02-09 00:46:17 +0000 | [diff] [blame] | 259 | if (!droppedLockCount) |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 260 | return; |
| 261 | |
mark.lam@apple.com | 7794f5e | 2014-02-10 19:39:30 +0000 | [diff] [blame] | 262 | ASSERT(!currentThreadIsHoldingLock()); |
mark.lam@apple.com | 6f2215d | 2014-02-10 22:27:10 +0000 | [diff] [blame] | 263 | lock(droppedLockCount); |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 264 | |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 265 | while (dropper->dropDepth() != m_lockDropDepth) { |
| 266 | unlock(droppedLockCount); |
fpizlo@apple.com | ac12a7e | 2017-07-22 14:36:18 +0000 | [diff] [blame] | 267 | Thread::yield(); |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 268 | lock(droppedLockCount); |
| 269 | } |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 270 | |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 271 | --m_lockDropDepth; |
mark.lam@apple.com | c6c3183 | 2014-02-08 08:27:13 +0000 | [diff] [blame] | 272 | |
utatane.tea@gmail.com | 843c4ac | 2017-08-03 06:03:18 +0000 | [diff] [blame] | 273 | Thread& thread = Thread::current(); |
| 274 | m_vm->setStackPointerAtVMEntry(thread.savedStackPointerAtVMEntry()); |
| 275 | m_vm->setLastStackTop(thread.savedLastStackTop()); |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 276 | } |
| 277 | |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 278 | JSLock::DropAllLocks::DropAllLocks(VM* vm) |
mark.lam@apple.com | cbebdc4 | 2014-02-09 00:46:17 +0000 | [diff] [blame] | 279 | : m_droppedLockCount(0) |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 280 | // If the VM is in the middle of being destroyed then we don't want to resurrect it |
| 281 | // by allowing DropAllLocks to ref it. By this point the JSLock has already been |
| 282 | // released anyways, so it doesn't matter that DropAllLocks is a no-op. |
rniwa@webkit.org | 954a92e | 2019-10-12 02:19:34 +0000 | [diff] [blame] | 283 | , m_vm(vm->heap.isShuttingDown() ? nullptr : vm) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 284 | { |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 285 | if (!m_vm) |
| 286 | return; |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 287 | RELEASE_ASSERT(!m_vm->apiLock().currentThreadIsHoldingLock() || !m_vm->isCollectorBusyOnCurrentThread()); |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 288 | m_droppedLockCount = m_vm->apiLock().dropAllLocks(this); |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 289 | } |
| 290 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 291 | JSLock::DropAllLocks::DropAllLocks(JSGlobalObject* globalObject) |
| 292 | : DropAllLocks(globalObject ? &globalObject->vm() : nullptr) |
mhahnenberg@apple.com | e16f809 | 2012-06-27 23:08:26 +0000 | [diff] [blame] | 293 | { |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 294 | } |
| 295 | |
akling@apple.com | 710fc6a | 2014-03-03 05:10:15 +0000 | [diff] [blame] | 296 | JSLock::DropAllLocks::DropAllLocks(VM& vm) |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 297 | : DropAllLocks(&vm) |
akling@apple.com | 710fc6a | 2014-03-03 05:10:15 +0000 | [diff] [blame] | 298 | { |
akling@apple.com | 710fc6a | 2014-03-03 05:10:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 301 | JSLock::DropAllLocks::~DropAllLocks() |
| 302 | { |
dbates@webkit.org | 96fcf22 | 2013-10-22 20:40:34 +0000 | [diff] [blame] | 303 | if (!m_vm) |
| 304 | return; |
mark.lam@apple.com | 2ff16d2 | 2014-02-11 04:48:01 +0000 | [diff] [blame] | 305 | m_vm->apiLock().grabAllLocks(this, m_droppedLockCount); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 306 | } |
| 307 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 308 | } // namespace JSC |