]>
Commit | Line | Data |
---|---|---|
b37bf2e1 | 1 | /* |
9dae56ea | 2 | * Copyright (C) 2005, 2008, 2009 Apple Inc. All rights reserved. |
b37bf2e1 A |
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 | ||
9dae56ea A |
21 | #ifndef JSLock_h |
22 | #define JSLock_h | |
b37bf2e1 A |
23 | |
24 | #include <wtf/Assertions.h> | |
25 | #include <wtf/Noncopyable.h> | |
6fe7ccc8 A |
26 | #include <wtf/RefPtr.h> |
27 | #include <wtf/TCSpinLock.h> | |
28 | #include <wtf/Threading.h> | |
b37bf2e1 | 29 | |
9dae56ea | 30 | namespace JSC { |
b37bf2e1 A |
31 | |
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 | |
6fe7ccc8 A |
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 | |
b37bf2e1 A |
39 | // object also acts as a convenience short-hand for running important |
40 | // initialization routines. | |
41 | ||
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. | |
50 | ||
9dae56ea | 51 | class ExecState; |
14957cd0 | 52 | class JSGlobalData; |
9dae56ea | 53 | |
6fe7ccc8 A |
54 | // This class is used to protect the initialization of the legacy single |
55 | // shared JSGlobalData. | |
56 | class GlobalJSLock { | |
57 | WTF_MAKE_NONCOPYABLE(GlobalJSLock); | |
58 | public: | |
59 | JS_EXPORT_PRIVATE GlobalJSLock(); | |
60 | JS_EXPORT_PRIVATE ~GlobalJSLock(); | |
61 | }; | |
62 | ||
63 | class JSLockHolder { | |
64 | public: | |
65 | JS_EXPORT_PRIVATE JSLockHolder(JSGlobalData*); | |
66 | JS_EXPORT_PRIVATE JSLockHolder(JSGlobalData&); | |
67 | JS_EXPORT_PRIVATE JSLockHolder(ExecState*); | |
68 | ||
69 | JS_EXPORT_PRIVATE ~JSLockHolder(); | |
70 | private: | |
71 | RefPtr<JSGlobalData> m_globalData; | |
72 | }; | |
f9bf01c6 | 73 | |
14957cd0 A |
74 | class JSLock { |
75 | WTF_MAKE_NONCOPYABLE(JSLock); | |
b37bf2e1 | 76 | public: |
6fe7ccc8 A |
77 | JSLock(); |
78 | JS_EXPORT_PRIVATE ~JSLock(); | |
79 | ||
80 | JS_EXPORT_PRIVATE void lock(); | |
81 | JS_EXPORT_PRIVATE void unlock(); | |
82 | ||
9dae56ea A |
83 | static void lock(ExecState*); |
84 | static void unlock(ExecState*); | |
6fe7ccc8 A |
85 | static void lock(JSGlobalData&); |
86 | static void unlock(JSGlobalData&); | |
87 | ||
88 | JS_EXPORT_PRIVATE bool currentThreadIsHoldingLock(); | |
9dae56ea | 89 | |
6fe7ccc8 A |
90 | unsigned dropAllLocks(); |
91 | unsigned dropAllLocksUnconditionally(); | |
92 | void grabAllLocks(unsigned lockCount); | |
b37bf2e1 | 93 | |
6fe7ccc8 A |
94 | SpinLock m_spinLock; |
95 | Mutex m_lock; | |
96 | ThreadIdentifier m_ownerThread; | |
97 | intptr_t m_lockCount; | |
98 | unsigned m_lockDropDepth; | |
b37bf2e1 | 99 | |
14957cd0 A |
100 | class DropAllLocks { |
101 | WTF_MAKE_NONCOPYABLE(DropAllLocks); | |
b37bf2e1 | 102 | public: |
6fe7ccc8 A |
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(); | |
b37bf2e1 A |
110 | |
111 | private: | |
9dae56ea | 112 | intptr_t m_lockCount; |
6fe7ccc8 | 113 | RefPtr<JSGlobalData> m_globalData; |
b37bf2e1 A |
114 | }; |
115 | }; | |
116 | ||
117 | } // namespace | |
118 | ||
9dae56ea | 119 | #endif // JSLock_h |