2 * Copyright (C) 2005, 2008 Apple Inc. All rights reserved.
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.
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.
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
25 #include "CallFrame.h"
27 #include "ScopeChain.h"
35 // JSLock is only needed to support an obsolete execution model where JavaScriptCore
36 // automatically protected against concurrent access from multiple threads.
37 // So it's safe to disable it on non-mac platforms where we don't have native pthreads.
38 #if (OS(DARWIN) || USE(PTHREADS))
40 // Acquire this mutex before accessing lock-related data.
41 static pthread_mutex_t JSMutex = PTHREAD_MUTEX_INITIALIZER;
43 // Thread-specific key that tells whether a thread holds the JSMutex, and how many times it was taken recursively.
44 pthread_key_t JSLockCount;
46 static void createJSLockCount()
48 pthread_key_create(&JSLockCount, 0);
51 pthread_once_t createJSLockCountOnce = PTHREAD_ONCE_INIT;
53 // Lock nesting count.
54 intptr_t JSLock::lockCount()
56 pthread_once(&createJSLockCountOnce, createJSLockCount);
58 return reinterpret_cast<intptr_t>(pthread_getspecific(JSLockCount));
61 static void setLockCount(intptr_t count)
64 pthread_setspecific(JSLockCount, reinterpret_cast<void*>(count));
67 JSLock::JSLock(ExecState* exec)
68 : m_lockBehavior(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly)
73 JSLock::JSLock(JSGlobalData* globalData)
74 : m_lockBehavior(globalData->isSharedInstance() ? LockForReal : SilenceAssertionsOnly)
79 void JSLock::lock(JSLockBehavior lockBehavior)
82 // Locking "not for real" is a debug-only feature.
83 if (lockBehavior == SilenceAssertionsOnly)
87 pthread_once(&createJSLockCountOnce, createJSLockCount);
89 intptr_t currentLockCount = lockCount();
90 if (!currentLockCount && lockBehavior == LockForReal) {
91 int result = pthread_mutex_lock(&JSMutex);
92 ASSERT_UNUSED(result, !result);
94 setLockCount(currentLockCount + 1);
97 void JSLock::unlock(JSLockBehavior lockBehavior)
102 // Locking "not for real" is a debug-only feature.
103 if (lockBehavior == SilenceAssertionsOnly)
107 intptr_t newLockCount = lockCount() - 1;
108 setLockCount(newLockCount);
109 if (!newLockCount && lockBehavior == LockForReal) {
110 int result = pthread_mutex_unlock(&JSMutex);
111 ASSERT_UNUSED(result, !result);
115 void JSLock::lock(ExecState* exec)
117 lock(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly);
120 void JSLock::unlock(ExecState* exec)
122 unlock(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly);
125 bool JSLock::currentThreadIsHoldingLock()
127 pthread_once(&createJSLockCountOnce, createJSLockCount);
128 return !!pthread_getspecific(JSLockCount);
131 // This is fairly nasty. We allow multiple threads to run on the same
132 // context, and we do not require any locking semantics in doing so -
133 // clients of the API may simply use the context from multiple threads
134 // concurently, and assume this will work. In order to make this work,
135 // We lock the context when a thread enters, and unlock it when it leaves.
136 // However we do not only unlock when the thread returns from its
137 // entry point (evaluate script or call function), we also unlock the
138 // context if the thread leaves JSC by making a call out to an external
139 // function through a callback.
141 // All threads using the context share the same JS stack (the RegisterFile).
142 // Whenever a thread calls into JSC it starts using the RegisterFile from the
143 // previous 'high water mark' - the maximum point the stack has ever grown to
144 // (returned by RegisterFile::end()). So if a first thread calls out to a
145 // callback, and a second thread enters JSC, then also exits by calling out
146 // to a callback, we can be left with stackframes from both threads in the
147 // RegisterFile. As such, a problem may occur should the first thread's
148 // callback complete first, and attempt to return to JSC. Were we to allow
149 // this to happen, and were its stack to grow further, then it may potentially
150 // write over the second thread's call frames.
152 // In avoid JS stack corruption we enforce a policy of only ever allowing two
153 // threads to use a JS context concurrently, and only allowing the second of
154 // these threads to execute until it has completed and fully returned from its
155 // outermost call into JSC. We enforce this policy using 'lockDropDepth'. The
156 // first time a thread exits it will call DropAllLocks - which will do as expected
157 // and drop locks allowing another thread to enter. Should another thread, or the
158 // same thread again, enter JSC (through evaluate script or call function), and exit
159 // again through a callback, then the locks will not be dropped when DropAllLocks
160 // is called (since lockDropDepth is non-zero). Since this thread is still holding
161 // the locks, only it will re able to re-enter JSC (either be returning from the
162 // callback, or by re-entering through another call to evaulate script or call
165 // This policy is slightly more restricive than it needs to be for correctness -
166 // we could validly allow futher entries into JSC from other threads, we only
167 // need ensure that callbacks return in the reverse chronological order of the
168 // order in which they were made - though implementing the less restrictive policy
169 // would likely increase complexity and overhead.
171 static unsigned lockDropDepth = 0;
173 JSLock::DropAllLocks::DropAllLocks(ExecState* exec)
174 : m_lockBehavior(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly)
176 pthread_once(&createJSLockCountOnce, createJSLockCount);
178 if (lockDropDepth++) {
183 m_lockCount = JSLock::lockCount();
184 for (intptr_t i = 0; i < m_lockCount; i++)
185 JSLock::unlock(m_lockBehavior);
188 JSLock::DropAllLocks::DropAllLocks(JSLockBehavior JSLockBehavior)
189 : m_lockBehavior(JSLockBehavior)
191 pthread_once(&createJSLockCountOnce, createJSLockCount);
193 if (lockDropDepth++) {
198 // It is necessary to drop even "unreal" locks, because having a non-zero lock count
199 // will prevent a real lock from being taken.
201 m_lockCount = JSLock::lockCount();
202 for (intptr_t i = 0; i < m_lockCount; i++)
203 JSLock::unlock(m_lockBehavior);
206 JSLock::DropAllLocks::~DropAllLocks()
208 for (intptr_t i = 0; i < m_lockCount; i++)
209 JSLock::lock(m_lockBehavior);
214 #else // (OS(DARWIN) || USE(PTHREADS))
216 JSLock::JSLock(ExecState*)
217 : m_lockBehavior(SilenceAssertionsOnly)
221 // If threading support is off, set the lock count to a constant value of 1 so ssertions
222 // that the lock is held don't fail
223 intptr_t JSLock::lockCount()
228 bool JSLock::currentThreadIsHoldingLock()
233 void JSLock::lock(JSLockBehavior)
237 void JSLock::unlock(JSLockBehavior)
241 void JSLock::lock(ExecState*)
245 void JSLock::unlock(ExecState*)
249 JSLock::DropAllLocks::DropAllLocks(ExecState*)
253 JSLock::DropAllLocks::DropAllLocks(JSLockBehavior)
257 JSLock::DropAllLocks::~DropAllLocks()
261 #endif // (OS(DARWIN) || USE(PTHREADS))