source: webkit/trunk/JavaScriptCore/runtime/JSLock.cpp@ 67583

Last change on this file since 67583 was 64585, checked in by [email protected], 15 years ago

https://bugs.webkit.org/show_bug.cgi?id=41318
GC should reclaim garbage even when new objects are not being allocated rapidly

Patch by Nathan Lawrence <[email protected]> on 2010-08-03
Reviewed by Oliver Hunt.

Added a callback in JavaScriptCore that gets triggered after an
allocation causes the heap to reset. This is useful for adding a
timer that will trigger garbage collection after the "last" allocation.

Also needed was to add lock and unlock methods to JSLock that needed
only a JSGlobalData object versus an ExecState object.

(JSC::JIT::emit_op_put_by_val):

  • runtime/Collector.cpp:

(JSC::Heap::Heap):
(JSC::Heap::reset):
(JSC::Heap::setActivityCallback):

  • runtime/Collector.h:
  • runtime/GCActivityCallback.cpp: Added.

(JSC::DefaultGCActivityCallback::DefaultGCActivityCallback):
(JSC::DefaultGCActivityCallback::~DefaultGCActivityCallback):
(JSC::DefaultGCActivityCallback::operator()):

  • runtime/GCActivityCallback.h: Added.

(JSC::GCActivityCallback::~GCActivityCallback):
(JSC::GCActivityCallback::operator()):
(JSC::GCActivityCallback::GCActivityCallback):
(JSC::DefaultGCActivityCallback::create):

  • runtime/GCActivityCallbackCF.cpp: Added.

(JSC::DefaultGCActivityCallbackPlatformData::trigger):
(JSC::DefaultGCActivityCallback::DefaultGCActivityCallback):
(JSC::DefaultGCActivityCallback::~DefaultGCActivityCallback):
(JSC::DefaultGCActivityCallback::operator()):

  • runtime/JSLock.cpp:

(JSC::JSLock::JSLock):

  • runtime/JSLock.h:
  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1/*
2 * Copyright (C) 2005, 2008 Apple Inc. All rights reserved.
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
24#include "Collector.h"
25#include "CallFrame.h"
26
27#if ENABLE(JSC_MULTIPLE_THREADS)
28#include <pthread.h>
29#endif
30
31namespace JSC {
32
33#if ENABLE(JSC_MULTIPLE_THREADS)
34
35// Acquire this mutex before accessing lock-related data.
36static pthread_mutex_t JSMutex = PTHREAD_MUTEX_INITIALIZER;
37
38// Thread-specific key that tells whether a thread holds the JSMutex, and how many times it was taken recursively.
39pthread_key_t JSLockCount;
40
41static void createJSLockCount()
42{
43 pthread_key_create(&JSLockCount, 0);
44}
45
46pthread_once_t createJSLockCountOnce = PTHREAD_ONCE_INIT;
47
48// Lock nesting count.
49intptr_t JSLock::lockCount()
50{
51 pthread_once(&createJSLockCountOnce, createJSLockCount);
52
53 return reinterpret_cast<intptr_t>(pthread_getspecific(JSLockCount));
54}
55
56static void setLockCount(intptr_t count)
57{
58 ASSERT(count >= 0);
59 pthread_setspecific(JSLockCount, reinterpret_cast<void*>(count));
60}
61
62JSLock::JSLock(ExecState* exec)
63 : m_lockBehavior(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly)
64{
65 lock(m_lockBehavior);
66}
67
68JSLock::JSLock(JSGlobalData* globalData)
69 : m_lockBehavior(globalData->isSharedInstance() ? LockForReal : SilenceAssertionsOnly)
70{
71 lock(m_lockBehavior);
72}
73
74void JSLock::lock(JSLockBehavior lockBehavior)
75{
76#ifdef NDEBUG
77 // Locking "not for real" is a debug-only feature.
78 if (lockBehavior == SilenceAssertionsOnly)
79 return;
80#endif
81
82 pthread_once(&createJSLockCountOnce, createJSLockCount);
83
84 intptr_t currentLockCount = lockCount();
85 if (!currentLockCount && lockBehavior == LockForReal) {
86 int result;
87 result = pthread_mutex_lock(&JSMutex);
88 ASSERT(!result);
89 }
90 setLockCount(currentLockCount + 1);
91}
92
93void JSLock::unlock(JSLockBehavior lockBehavior)
94{
95 ASSERT(lockCount());
96
97#ifdef NDEBUG
98 // Locking "not for real" is a debug-only feature.
99 if (lockBehavior == SilenceAssertionsOnly)
100 return;
101#endif
102
103 intptr_t newLockCount = lockCount() - 1;
104 setLockCount(newLockCount);
105 if (!newLockCount && lockBehavior == LockForReal) {
106 int result;
107 result = pthread_mutex_unlock(&JSMutex);
108 ASSERT(!result);
109 }
110}
111
112void JSLock::lock(ExecState* exec)
113{
114 lock(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly);
115}
116
117void JSLock::unlock(ExecState* exec)
118{
119 unlock(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly);
120}
121
122bool JSLock::currentThreadIsHoldingLock()
123{
124 pthread_once(&createJSLockCountOnce, createJSLockCount);
125 return !!pthread_getspecific(JSLockCount);
126}
127
128// This is fairly nasty. We allow multiple threads to run on the same
129// context, and we do not require any locking semantics in doing so -
130// clients of the API may simply use the context from multiple threads
131// concurently, and assume this will work. In order to make this work,
132// We lock the context when a thread enters, and unlock it when it leaves.
133// However we do not only unlock when the thread returns from its
134// entry point (evaluate script or call function), we also unlock the
135// context if the thread leaves JSC by making a call out to an external
136// function through a callback.
137//
138// All threads using the context share the same JS stack (the RegisterFile).
139// Whenever a thread calls into JSC it starts using the RegisterFile from the
140// previous 'high water mark' - the maximum point the stack has ever grown to
141// (returned by RegisterFile::end()). So if a first thread calls out to a
142// callback, and a second thread enters JSC, then also exits by calling out
143// to a callback, we can be left with stackframes from both threads in the
144// RegisterFile. As such, a problem may occur should the first thread's
145// callback complete first, and attempt to return to JSC. Were we to allow
146// this to happen, and were its stack to grow further, then it may potentially
147// write over the second thread's call frames.
148//
149// In avoid JS stack corruption we enforce a policy of only ever allowing two
150// threads to use a JS context concurrently, and only allowing the second of
151// these threads to execute until it has completed and fully returned from its
152// outermost call into JSC. We enforce this policy using 'lockDropDepth'. The
153// first time a thread exits it will call DropAllLocks - which will do as expected
154// and drop locks allowing another thread to enter. Should another thread, or the
155// same thread again, enter JSC (through evaluate script or call function), and exit
156// again through a callback, then the locks will not be dropped when DropAllLocks
157// is called (since lockDropDepth is non-zero). Since this thread is still holding
158// the locks, only it will re able to re-enter JSC (either be returning from the
159// callback, or by re-entering through another call to evaulate script or call
160// function).
161//
162// This policy is slightly more restricive than it needs to be for correctness -
163// we could validly allow futher entries into JSC from other threads, we only
164// need ensure that callbacks return in the reverse chronological order of the
165// order in which they were made - though implementing the less restrictive policy
166// would likely increase complexity and overhead.
167//
168static unsigned lockDropDepth = 0;
169
170JSLock::DropAllLocks::DropAllLocks(ExecState* exec)
171 : m_lockBehavior(exec->globalData().isSharedInstance() ? LockForReal : SilenceAssertionsOnly)
172{
173 pthread_once(&createJSLockCountOnce, createJSLockCount);
174
175 if (lockDropDepth++) {
176 m_lockCount = 0;
177 return;
178 }
179
180 m_lockCount = JSLock::lockCount();
181 for (intptr_t i = 0; i < m_lockCount; i++)
182 JSLock::unlock(m_lockBehavior);
183}
184
185JSLock::DropAllLocks::DropAllLocks(JSLockBehavior JSLockBehavior)
186 : m_lockBehavior(JSLockBehavior)
187{
188 pthread_once(&createJSLockCountOnce, createJSLockCount);
189
190 if (lockDropDepth++) {
191 m_lockCount = 0;
192 return;
193 }
194
195 // It is necessary to drop even "unreal" locks, because having a non-zero lock count
196 // will prevent a real lock from being taken.
197
198 m_lockCount = JSLock::lockCount();
199 for (intptr_t i = 0; i < m_lockCount; i++)
200 JSLock::unlock(m_lockBehavior);
201}
202
203JSLock::DropAllLocks::~DropAllLocks()
204{
205 for (intptr_t i = 0; i < m_lockCount; i++)
206 JSLock::lock(m_lockBehavior);
207
208 --lockDropDepth;
209}
210
211#else
212
213JSLock::JSLock(ExecState*)
214 : m_lockBehavior(SilenceAssertionsOnly)
215{
216}
217
218// If threading support is off, set the lock count to a constant value of 1 so ssertions
219// that the lock is held don't fail
220intptr_t JSLock::lockCount()
221{
222 return 1;
223}
224
225bool JSLock::currentThreadIsHoldingLock()
226{
227 return true;
228}
229
230void JSLock::lock(JSLockBehavior)
231{
232}
233
234void JSLock::unlock(JSLockBehavior)
235{
236}
237
238void JSLock::lock(ExecState*)
239{
240}
241
242void JSLock::unlock(ExecState*)
243{
244}
245
246JSLock::DropAllLocks::DropAllLocks(ExecState*)
247{
248}
249
250JSLock::DropAllLocks::DropAllLocks(JSLockBehavior)
251{
252}
253
254JSLock::DropAllLocks::~DropAllLocks()
255{
256}
257
258#endif // USE(MULTIPLE_THREADS)
259
260} // namespace JSC
Note: See TracBrowser for help on using the repository browser.