]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSLock.h
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / JSLock.h
1 /*
2 * Copyright (C) 2005, 2008, 2009 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 <wtf/Assertions.h>
25 #include <wtf/Noncopyable.h>
26 #include <wtf/RefPtr.h>
27 #include <wtf/TCSpinLock.h>
28 #include <wtf/Threading.h>
29
30 namespace JSC {
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
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.
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
51 class ExecState;
52 class JSGlobalData;
53
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 };
73
74 class JSLock {
75 WTF_MAKE_NONCOPYABLE(JSLock);
76 public:
77 JSLock();
78 JS_EXPORT_PRIVATE ~JSLock();
79
80 JS_EXPORT_PRIVATE void lock();
81 JS_EXPORT_PRIVATE void unlock();
82
83 static void lock(ExecState*);
84 static void unlock(ExecState*);
85 static void lock(JSGlobalData&);
86 static void unlock(JSGlobalData&);
87
88 JS_EXPORT_PRIVATE bool currentThreadIsHoldingLock();
89
90 unsigned dropAllLocks();
91 unsigned dropAllLocksUnconditionally();
92 void grabAllLocks(unsigned lockCount);
93
94 SpinLock m_spinLock;
95 Mutex m_lock;
96 ThreadIdentifier m_ownerThread;
97 intptr_t m_lockCount;
98 unsigned m_lockDropDepth;
99
100 class DropAllLocks {
101 WTF_MAKE_NONCOPYABLE(DropAllLocks);
102 public:
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();
110
111 private:
112 intptr_t m_lockCount;
113 RefPtr<JSGlobalData> m_globalData;
114 };
115 };
116
117 } // namespace
118
119 #endif // JSLock_h