1/*
2 * Copyright (C) 2005, 2008, 2009, 2014 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 GNU
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#ifndef JSLock_h
22#define JSLock_h
23
24#include <mutex>
25#include <thread>
26#include <wtf/Assertions.h>
27#include <wtf/Lock.h>
28#include <wtf/Noncopyable.h>
29#include <wtf/RefPtr.h>
30#include <wtf/ThreadSafeRefCounted.h>
31#include <wtf/WTFThreadData.h>
32
33namespace JSC {
34
35// To make it safe to use JavaScript on multiple threads, it is
36// important to lock before doing anything that allocates a
37// JavaScript data structure or that interacts with shared state
38// such as the protect count hash table. The simplest way to lock
39// is to create a local JSLockHolder object in the scope where the lock
40// must be held and pass it the context that requires protection.
41// The lock is recursive so nesting is ok. The JSLock
42// object also acts as a convenience short-hand for running important
43// initialization routines.
44
45// To avoid deadlock, sometimes it is necessary to temporarily
46// release the lock. Since it is recursive you actually have to
47// release all locks held by your thread. This is safe to do if
48// you are executing code that doesn't require the lock, and you
49// reacquire the right number of locks at the end. You can do this
50// by constructing a locally scoped JSLock::DropAllLocks object. The
51// DropAllLocks object takes care to release the JSLock only if your
52// thread acquired it to begin with.
53
54class ExecState;
55class VM;
56
57// This class is used to protect the initialization of the legacy single
58// shared VM.
59class GlobalJSLock {
60 WTF_MAKE_NONCOPYABLE(GlobalJSLock);
61public:
62 JS_EXPORT_PRIVATE GlobalJSLock();
63 JS_EXPORT_PRIVATE ~GlobalJSLock();
64private:
65 static StaticLock s_sharedInstanceMutex;
66};
67
68class JSLockHolder {
69public:
70 JS_EXPORT_PRIVATE JSLockHolder(VM*);
71 JS_EXPORT_PRIVATE JSLockHolder(VM&);
72 JS_EXPORT_PRIVATE JSLockHolder(ExecState*);
73
74 JS_EXPORT_PRIVATE ~JSLockHolder();
75private:
76 void init();
77
78 RefPtr<VM> m_vm;
79};
80
81class JSLock : public ThreadSafeRefCounted<JSLock> {
82 WTF_MAKE_NONCOPYABLE(JSLock);
83public:
84 JSLock(VM*);
85 JS_EXPORT_PRIVATE ~JSLock();
86
87 JS_EXPORT_PRIVATE void lock();
88 JS_EXPORT_PRIVATE void unlock();
89
90 static void lock(ExecState*);
91 static void unlock(ExecState*);
92 static void lock(VM&);
93 static void unlock(VM&);
94
95 VM* vm() { return m_vm; }
96
97 bool hasExclusiveThread() const { return m_hasExclusiveThread; }
98 std::thread::id exclusiveThread() const
99 {
100 ASSERT(m_hasExclusiveThread);
101 return m_ownerThreadID;
102 }
103 JS_EXPORT_PRIVATE void setExclusiveThread(std::thread::id);
104 JS_EXPORT_PRIVATE bool currentThreadIsHoldingLock();
105
106 void willDestroyVM(VM*);
107
108 class DropAllLocks {
109 WTF_MAKE_NONCOPYABLE(DropAllLocks);
110 public:
111 JS_EXPORT_PRIVATE DropAllLocks(ExecState*);
112 JS_EXPORT_PRIVATE DropAllLocks(VM*);
113 JS_EXPORT_PRIVATE DropAllLocks(VM&);
114 JS_EXPORT_PRIVATE ~DropAllLocks();
115
116 void setDropDepth(unsigned depth) { m_dropDepth = depth; }
117 unsigned dropDepth() const { return m_dropDepth; }
118
119 private:
120 intptr_t m_droppedLockCount;
121 RefPtr<VM> m_vm;
122 unsigned m_dropDepth;
123 };
124
125private:
126 void lock(intptr_t lockCount);
127 void unlock(intptr_t unlockCount);
128
129 void didAcquireLock();
130 void willReleaseLock();
131
132 unsigned dropAllLocks(DropAllLocks*);
133 void grabAllLocks(DropAllLocks*, unsigned lockCount);
134
135 Lock m_lock;
136 std::thread::id m_ownerThreadID;
137 intptr_t m_lockCount;
138 unsigned m_lockDropDepth;
139 bool m_hasExclusiveThread;
140 VM* m_vm;
141 AtomicStringTable* m_entryAtomicStringTable;
142};
143
144} // namespace
145
146#endif // JSLock_h
147