]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSLock.h
2 * Copyright (C) 2005, 2008, 2009, 2014 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.
26 #include <wtf/Assertions.h>
27 #include <wtf/Noncopyable.h>
28 #include <wtf/RefPtr.h>
29 #include <wtf/ThreadSafeRefCounted.h>
30 #include <wtf/WTFThreadData.h>
34 // To make it safe to use JavaScript on multiple threads, it is
35 // important to lock before doing anything that allocates a
36 // JavaScript data structure or that interacts with shared state
37 // such as the protect count hash table. The simplest way to lock
38 // is to create a local JSLockHolder object in the scope where the lock
39 // must be held and pass it the context that requires protection.
40 // The lock is recursive so nesting is ok. The JSLock
41 // object also acts as a convenience short-hand for running important
42 // initialization routines.
44 // To avoid deadlock, sometimes it is necessary to temporarily
45 // release the lock. Since it is recursive you actually have to
46 // release all locks held by your thread. This is safe to do if
47 // you are executing code that doesn't require the lock, and you
48 // reacquire the right number of locks at the end. You can do this
49 // by constructing a locally scoped JSLock::DropAllLocks object. The
50 // DropAllLocks object takes care to release the JSLock only if your
51 // thread acquired it to begin with.
56 // This class is used to protect the initialization of the legacy single
59 WTF_MAKE_NONCOPYABLE(GlobalJSLock
);
61 JS_EXPORT_PRIVATE
GlobalJSLock();
62 JS_EXPORT_PRIVATE
~GlobalJSLock();
64 static void initialize();
66 static std::mutex
* s_sharedInstanceMutex
;
71 JS_EXPORT_PRIVATE
JSLockHolder(VM
*);
72 JS_EXPORT_PRIVATE
JSLockHolder(VM
&);
73 JS_EXPORT_PRIVATE
JSLockHolder(ExecState
*);
75 JS_EXPORT_PRIVATE
~JSLockHolder();
82 class JSLock
: public ThreadSafeRefCounted
<JSLock
> {
83 WTF_MAKE_NONCOPYABLE(JSLock
);
86 JS_EXPORT_PRIVATE
~JSLock();
88 JS_EXPORT_PRIVATE
void lock();
89 JS_EXPORT_PRIVATE
void unlock();
91 static void lock(ExecState
*);
92 static void unlock(ExecState
*);
93 static void lock(VM
&);
94 static void unlock(VM
&);
96 VM
* vm() { return m_vm
; }
98 bool hasExclusiveThread() const { return m_hasExclusiveThread
; }
99 std::thread::id
exclusiveThread() const
101 ASSERT(m_hasExclusiveThread
);
102 return m_ownerThreadID
;
104 JS_EXPORT_PRIVATE
void setExclusiveThread(std::thread::id
);
105 JS_EXPORT_PRIVATE
bool currentThreadIsHoldingLock();
107 void willDestroyVM(VM
*);
110 WTF_MAKE_NONCOPYABLE(DropAllLocks
);
112 JS_EXPORT_PRIVATE
DropAllLocks(ExecState
*);
113 JS_EXPORT_PRIVATE
DropAllLocks(VM
*);
114 JS_EXPORT_PRIVATE
DropAllLocks(VM
&);
115 JS_EXPORT_PRIVATE
~DropAllLocks();
117 void setDropDepth(unsigned depth
) { m_dropDepth
= depth
; }
118 unsigned dropDepth() const { return m_dropDepth
; }
121 intptr_t m_droppedLockCount
;
123 unsigned m_dropDepth
;
127 void lock(intptr_t lockCount
);
128 void unlock(intptr_t unlockCount
);
130 void didAcquireLock();
131 void willReleaseLock();
133 unsigned dropAllLocks(DropAllLocks
*);
134 void grabAllLocks(DropAllLocks
*, unsigned lockCount
);
137 std::thread::id m_ownerThreadID
;
138 intptr_t m_lockCount
;
139 unsigned m_lockDropDepth
;
140 bool m_hasExclusiveThread
;
142 AtomicStringTable
* m_entryAtomicStringTable
;