1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#ifndef WTFThreadData_h
28#define WTFThreadData_h
29
30#include <wtf/HashMap.h>
31#include <wtf/HashSet.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/StackBounds.h>
34#include <wtf/StackStats.h>
35#include <wtf/text/StringHash.h>
36
37#if USE(APPLE_INTERNAL_SDK)
38#include <System/pthread_machdep.h>
39#endif
40
41#if defined(__PTK_FRAMEWORK_JAVASCRIPTCORE_KEY1)
42#define USE_PTHREAD_GETSPECIFIC_DIRECT 1
43#endif
44
45#if !USE(PTHREAD_GETSPECIFIC_DIRECT)
46#include <wtf/ThreadSpecific.h>
47#include <wtf/Threading.h>
48#endif
49
50namespace WTF {
51
52class AtomicStringTable;
53
54typedef void (*AtomicStringTableDestructor)(AtomicStringTable*);
55
56class WTFThreadData {
57 WTF_MAKE_NONCOPYABLE(WTFThreadData);
58public:
59 WTF_EXPORT_PRIVATE WTFThreadData();
60 WTF_EXPORT_PRIVATE ~WTFThreadData();
61
62 AtomicStringTable* atomicStringTable()
63 {
64 return m_currentAtomicStringTable;
65 }
66
67 AtomicStringTable* setCurrentAtomicStringTable(AtomicStringTable* atomicStringTable)
68 {
69 AtomicStringTable* oldAtomicStringTable = m_currentAtomicStringTable;
70 m_currentAtomicStringTable = atomicStringTable;
71 return oldAtomicStringTable;
72 }
73
74 void resetCurrentAtomicStringTable()
75 {
76 m_currentAtomicStringTable = m_defaultAtomicStringTable;
77 }
78
79 const StackBounds& stack()
80 {
81 // We need to always get a fresh StackBounds from the OS due to how fibers work.
82 // See https://bugs.webkit.org/show_bug.cgi?id=102411
83#if OS(WINDOWS)
84 m_stackBounds = StackBounds::currentThreadStackBounds();
85#endif
86 return m_stackBounds;
87 }
88
89#if ENABLE(STACK_STATS)
90 StackStats::PerThreadStats& stackStats()
91 {
92 return m_stackStats;
93 }
94#endif
95
96 void* savedStackPointerAtVMEntry()
97 {
98 return m_savedStackPointerAtVMEntry;
99 }
100
101 void setSavedStackPointerAtVMEntry(void* stackPointerAtVMEntry)
102 {
103 m_savedStackPointerAtVMEntry = stackPointerAtVMEntry;
104 }
105
106 void* savedLastStackTop()
107 {
108 return m_savedLastStackTop;
109 }
110
111 void setSavedLastStackTop(void* lastStackTop)
112 {
113 m_savedLastStackTop = lastStackTop;
114 }
115
116 void* m_apiData;
117
118private:
119 AtomicStringTable* m_currentAtomicStringTable;
120 AtomicStringTable* m_defaultAtomicStringTable;
121 AtomicStringTableDestructor m_atomicStringTableDestructor;
122
123 StackBounds m_stackBounds;
124#if ENABLE(STACK_STATS)
125 StackStats::PerThreadStats m_stackStats;
126#endif
127 void* m_savedStackPointerAtVMEntry;
128 void* m_savedLastStackTop;
129
130#if USE(PTHREAD_GETSPECIFIC_DIRECT)
131 static const pthread_key_t directKey = __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY1;
132 WTF_EXPORT_PRIVATE static WTFThreadData& createAndRegisterForGetspecificDirect();
133#else
134 static WTF_EXPORTDATA ThreadSpecific<WTFThreadData>* staticData;
135#endif
136
137 friend WTFThreadData& wtfThreadData();
138 friend class AtomicStringTable;
139};
140
141inline WTFThreadData& wtfThreadData()
142{
143 // WRT WebCore:
144 // WTFThreadData is used on main thread before it could possibly be used
145 // on secondary ones, so there is no need for synchronization here.
146 // WRT JavaScriptCore:
147 // wtfThreadData() is initially called from initializeThreading(), ensuring
148 // this is initially called in a pthread_once locked context.
149#if !USE(PTHREAD_GETSPECIFIC_DIRECT)
150 if (!WTFThreadData::staticData)
151 WTFThreadData::staticData = new ThreadSpecific<WTFThreadData>;
152 return **WTFThreadData::staticData;
153#else
154 if (WTFThreadData* data = static_cast<WTFThreadData*>(_pthread_getspecific_direct(WTFThreadData::directKey)))
155 return *data;
156 return WTFThreadData::createAndRegisterForGetspecificDirect();
157#endif
158}
159
160} // namespace WTF
161
162using WTF::WTFThreadData;
163using WTF::wtfThreadData;
164using WTF::AtomicStringTable;
165
166#endif // WTFThreadData_h
167