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"
26 #include "JSGlobalObject.h"
28 #include "ScopeChain.h"
36 // JSLock is only needed to support an obsolete execution model where JavaScriptCore
37 // automatically protected against concurrent access from multiple threads.
38 // So it's safe to disable it on non-mac platforms where we don't have native pthreads.
39 #if (OS(DARWIN) || USE(PTHREADS))
41 static pthread_mutex_t sharedInstanceLock
= PTHREAD_MUTEX_INITIALIZER
;
43 GlobalJSLock::GlobalJSLock()
45 pthread_mutex_lock(&sharedInstanceLock
);
48 GlobalJSLock::~GlobalJSLock()
50 pthread_mutex_unlock(&sharedInstanceLock
);
53 JSLockHolder::JSLockHolder(ExecState
* exec
)
54 : m_globalData(&exec
->globalData())
56 m_globalData
->apiLock().lock();
59 JSLockHolder::JSLockHolder(JSGlobalData
* globalData
)
60 : m_globalData(globalData
)
62 m_globalData
->apiLock().lock();
65 JSLockHolder::JSLockHolder(JSGlobalData
& globalData
)
66 : m_globalData(&globalData
)
68 m_globalData
->apiLock().lock();
71 JSLockHolder::~JSLockHolder()
73 m_globalData
->apiLock().unlock();
88 ThreadIdentifier currentThread
= WTF::currentThread();
90 SpinLockHolder
holder(&m_spinLock
);
91 if (m_ownerThread
== currentThread
&& m_lockCount
) {
100 SpinLockHolder
holder(&m_spinLock
);
101 m_ownerThread
= currentThread
;
102 ASSERT(!m_lockCount
);
107 void JSLock::unlock()
109 ASSERT(currentThreadIsHoldingLock());
111 SpinLockHolder
holder(&m_spinLock
);
118 void JSLock::lock(ExecState
* exec
)
120 exec
->globalData().apiLock().lock();
123 void JSLock::unlock(ExecState
* exec
)
125 exec
->globalData().apiLock().unlock();
128 bool JSLock::currentThreadIsHoldingLock()
130 return m_lockCount
&& m_ownerThread
== WTF::currentThread();
133 // This is fairly nasty. We allow multiple threads to run on the same
134 // context, and we do not require any locking semantics in doing so -
135 // clients of the API may simply use the context from multiple threads
136 // concurently, and assume this will work. In order to make this work,
137 // We lock the context when a thread enters, and unlock it when it leaves.
138 // However we do not only unlock when the thread returns from its
139 // entry point (evaluate script or call function), we also unlock the
140 // context if the thread leaves JSC by making a call out to an external
141 // function through a callback.
143 // All threads using the context share the same JS stack (the RegisterFile).
144 // Whenever a thread calls into JSC it starts using the RegisterFile from the
145 // previous 'high water mark' - the maximum point the stack has ever grown to
146 // (returned by RegisterFile::end()). So if a first thread calls out to a
147 // callback, and a second thread enters JSC, then also exits by calling out
148 // to a callback, we can be left with stackframes from both threads in the
149 // RegisterFile. As such, a problem may occur should the first thread's
150 // callback complete first, and attempt to return to JSC. Were we to allow
151 // this to happen, and were its stack to grow further, then it may potentially
152 // write over the second thread's call frames.
154 // To avoid JS stack corruption we enforce a policy of only ever allowing two
155 // threads to use a JS context concurrently, and only allowing the second of
156 // these threads to execute until it has completed and fully returned from its
157 // outermost call into JSC. We enforce this policy using 'lockDropDepth'. The
158 // first time a thread exits it will call DropAllLocks - which will do as expected
159 // and drop locks allowing another thread to enter. Should another thread, or the
160 // same thread again, enter JSC (through evaluate script or call function), and exit
161 // again through a callback, then the locks will not be dropped when DropAllLocks
162 // is called (since lockDropDepth is non-zero). Since this thread is still holding
163 // the locks, only it will be able to re-enter JSC (either be returning from the
164 // callback, or by re-entering through another call to evaulate script or call
167 // This policy is slightly more restricive than it needs to be for correctness -
168 // we could validly allow futher entries into JSC from other threads, we only
169 // need ensure that callbacks return in the reverse chronological order of the
170 // order in which they were made - though implementing the less restrictive policy
171 // would likely increase complexity and overhead.
174 // This function returns the number of locks that were dropped.
175 unsigned JSLock::dropAllLocks()
179 // Check if this thread is currently holding the lock.
180 // FIXME: Maybe we want to require this, guard with an ASSERT?
181 SpinLockHolder
holder(&m_spinLock
);
182 lockCount
= m_lockCount
;
183 if (!lockCount
|| m_ownerThread
!= WTF::currentThread())
187 // Don't drop the locks if they've already been dropped once.
188 // (If the prior drop came from another thread, and it resumed first,
189 // it could trash our register file).
193 // m_lockDropDepth is only incremented if any locks were dropped.
200 unsigned JSLock::dropAllLocksUnconditionally()
204 // Check if this thread is currently holding the lock.
205 // FIXME: Maybe we want to require this, guard with an ASSERT?
206 SpinLockHolder
holder(&m_spinLock
);
207 lockCount
= m_lockCount
;
208 if (!lockCount
|| m_ownerThread
!= WTF::currentThread())
212 // m_lockDropDepth is only incremented if any locks were dropped.
219 void JSLock::grabAllLocks(unsigned lockCount
)
221 // If no locks were dropped, nothing to do!
225 ThreadIdentifier currentThread
= WTF::currentThread();
227 // Check if this thread is currently holding the lock.
228 // FIXME: Maybe we want to prohibit this, guard against with an ASSERT?
229 SpinLockHolder
holder(&m_spinLock
);
230 if (m_ownerThread
== currentThread
&& m_lockCount
) {
231 m_lockCount
+= lockCount
;
240 SpinLockHolder
holder(&m_spinLock
);
241 m_ownerThread
= currentThread
;
242 ASSERT(!m_lockCount
);
243 m_lockCount
= lockCount
;
248 JSLock::DropAllLocks::DropAllLocks(ExecState
* exec
, AlwaysDropLocksTag alwaysDropLocks
)
250 , m_globalData(&exec
->globalData())
253 m_lockCount
= m_globalData
->apiLock().dropAllLocksUnconditionally();
255 m_lockCount
= m_globalData
->apiLock().dropAllLocks();
258 JSLock::DropAllLocks::DropAllLocks(JSGlobalData
* globalData
, AlwaysDropLocksTag alwaysDropLocks
)
260 , m_globalData(globalData
)
263 m_lockCount
= m_globalData
->apiLock().dropAllLocksUnconditionally();
265 m_lockCount
= m_globalData
->apiLock().dropAllLocks();
268 JSLock::DropAllLocks::~DropAllLocks()
270 m_globalData
->apiLock().grabAllLocks(m_lockCount
);
273 #else // (OS(DARWIN) || USE(PTHREADS))
275 GlobalJSLock::GlobalJSLock()
279 GlobalJSLock::~GlobalJSLock()
283 JSLockHolder::JSLockHolder(JSGlobalData
*)
287 JSLockHolder::JSLockHolder(JSGlobalData
&)
291 JSLockHolder::JSLockHolder(ExecState
*)
295 JSLockHolder::~JSLockHolder()
307 bool JSLock::currentThreadIsHoldingLock()
316 void JSLock::unlock()
320 void JSLock::lock(ExecState
*)
324 void JSLock::unlock(ExecState
*)
328 void JSLock::lock(JSGlobalData
&)
332 void JSLock::unlock(JSGlobalData
&)
336 unsigned JSLock::dropAllLocks()
341 unsigned JSLock::dropAllLocksUnconditionally()
346 void JSLock::grabAllLocks(unsigned)
350 JSLock::DropAllLocks::DropAllLocks(ExecState
*)
354 JSLock::DropAllLocks::DropAllLocks(JSGlobalData
*)
358 JSLock::DropAllLocks::~DropAllLocks()
362 #endif // (OS(DARWIN) || USE(PTHREADS))