2 * Copyright (C) 2005, 2008, 2009 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 GNU
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.
24 #include <wtf/Assertions.h>
25 #include <wtf/Noncopyable.h>
26 #include <wtf/RefPtr.h>
27 #include <wtf/TCSpinLock.h>
28 #include <wtf/Threading.h>
32 // To make it safe to use JavaScript on multiple threads, it is
33 // important to lock before doing anything that allocates a
34 // JavaScript data structure or that interacts with shared state
35 // such as the protect count hash table. The simplest way to lock
36 // is to create a local JSLockHolder object in the scope where the lock
37 // must be held and pass it the context that requires protection.
38 // The lock is recursive so nesting is ok. The JSLock
39 // object also acts as a convenience short-hand for running important
40 // initialization routines.
42 // To avoid deadlock, sometimes it is necessary to temporarily
43 // release the lock. Since it is recursive you actually have to
44 // release all locks held by your thread. This is safe to do if
45 // you are executing code that doesn't require the lock, and you
46 // reacquire the right number of locks at the end. You can do this
47 // by constructing a locally scoped JSLock::DropAllLocks object. The
48 // DropAllLocks object takes care to release the JSLock only if your
49 // thread acquired it to begin with.
54 // This class is used to protect the initialization of the legacy single
55 // shared JSGlobalData.
57 WTF_MAKE_NONCOPYABLE(GlobalJSLock
);
59 JS_EXPORT_PRIVATE
GlobalJSLock();
60 JS_EXPORT_PRIVATE
~GlobalJSLock();
65 JS_EXPORT_PRIVATE
JSLockHolder(JSGlobalData
*);
66 JS_EXPORT_PRIVATE
JSLockHolder(JSGlobalData
&);
67 JS_EXPORT_PRIVATE
JSLockHolder(ExecState
*);
69 JS_EXPORT_PRIVATE
~JSLockHolder();
71 RefPtr
<JSGlobalData
> m_globalData
;
75 WTF_MAKE_NONCOPYABLE(JSLock
);
78 JS_EXPORT_PRIVATE
~JSLock();
80 JS_EXPORT_PRIVATE
void lock();
81 JS_EXPORT_PRIVATE
void unlock();
83 static void lock(ExecState
*);
84 static void unlock(ExecState
*);
85 static void lock(JSGlobalData
&);
86 static void unlock(JSGlobalData
&);
88 JS_EXPORT_PRIVATE
bool currentThreadIsHoldingLock();
90 unsigned dropAllLocks();
91 unsigned dropAllLocksUnconditionally();
92 void grabAllLocks(unsigned lockCount
);
96 ThreadIdentifier m_ownerThread
;
98 unsigned m_lockDropDepth
;
101 WTF_MAKE_NONCOPYABLE(DropAllLocks
);
103 // This is a hack to allow Mobile Safari to always release the locks since
104 // hey depend on the behavior that DropAllLocks does indeed always drop all
105 // locks, which isn't always the case with the default behavior.
106 enum AlwaysDropLocksTag
{ DontAlwaysDropLocks
= 0, AlwaysDropLocks
};
107 JS_EXPORT_PRIVATE
DropAllLocks(ExecState
* exec
, AlwaysDropLocksTag alwaysDropLocks
= DontAlwaysDropLocks
);
108 JS_EXPORT_PRIVATE
DropAllLocks(JSGlobalData
*, AlwaysDropLocksTag alwaysDropLocks
= DontAlwaysDropLocks
);
109 JS_EXPORT_PRIVATE
~DropAllLocks();
112 intptr_t m_lockCount
;
113 RefPtr
<JSGlobalData
> m_globalData
;