1/*
2 * Copyright (C) 2007-2018 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood <jhaygood@reaktix.com>
4 * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#pragma once
32
33#include <mutex>
34#include <stdint.h>
35#include <wtf/Atomics.h>
36#include <wtf/Expected.h>
37#include <wtf/FastTLS.h>
38#include <wtf/Function.h>
39#include <wtf/HashSet.h>
40#include <wtf/PlatformRegisters.h>
41#include <wtf/Ref.h>
42#include <wtf/RefPtr.h>
43#include <wtf/StackBounds.h>
44#include <wtf/StackStats.h>
45#include <wtf/ThreadSafeRefCounted.h>
46#include <wtf/ThreadSpecific.h>
47#include <wtf/Vector.h>
48#include <wtf/WordLock.h>
49#include <wtf/text/AtomicStringTable.h>
50
51#if USE(PTHREADS) && !OS(DARWIN)
52#include <signal.h>
53#endif
54
55namespace WTF {
56
57class AbstractLocker;
58class ThreadMessageData;
59
60enum class ThreadGroupAddResult;
61
62class ThreadGroup;
63class PrintStream;
64
65// This function can be called from any threads.
66WTF_EXPORT_PRIVATE void initializeThreading();
67
68#if USE(PTHREADS)
69
70// We use SIGUSR1 to suspend and resume machine threads in JavaScriptCore.
71constexpr const int SigThreadSuspendResume = SIGUSR1;
72
73#endif
74
75// FIXME: The following functions remain because they are used from WebKit Windows support library,
76// WebKitQuartzCoreAdditions.dll. When updating the support library, we should use new API instead
77// and the following workaound should be removed. And new code should not use the following APIs.
78// Remove this workaround code when <rdar://problem/31793213> is fixed.
79#if OS(WINDOWS)
80WTF_EXPORT_PRIVATE ThreadIdentifier createThread(ThreadFunction, void*, const char* threadName);
81WTF_EXPORT_PRIVATE int waitForThreadCompletion(ThreadIdentifier);
82#endif
83
84class Thread : public ThreadSafeRefCounted<Thread> {
85public:
86 friend class ThreadGroup;
87 friend WTF_EXPORT_PRIVATE void initializeThreading();
88#if OS(WINDOWS)
89 friend WTF_EXPORT_PRIVATE int waitForThreadCompletion(ThreadIdentifier);
90#endif
91
92 WTF_EXPORT_PRIVATE ~Thread();
93
94 // Returns nullptr if thread creation failed.
95 // The thread name must be a literal since on some platforms it's passed in to the thread.
96 WTF_EXPORT_PRIVATE static Ref<Thread> create(const char* threadName, Function<void()>&&);
97
98 // Returns Thread object.
99 static Thread& current();
100
101 // Set of all WTF::Thread created threads.
102 WTF_EXPORT_PRIVATE static HashSet<Thread*>& allThreads(const LockHolder&);
103 WTF_EXPORT_PRIVATE static Lock& allThreadsMutex();
104
105#if OS(WINDOWS)
106 // Returns ThreadIdentifier directly. It is useful if the user only cares about identity
107 // of threads. At that time, users should know that holding this ThreadIdentifier does not ensure
108 // that the thread information is alive. While Thread::current() is not safe if it is called
109 // from the destructor of the other TLS data, currentID() always returns meaningful thread ID.
110 WTF_EXPORT_PRIVATE static ThreadIdentifier currentID();
111
112 ThreadIdentifier id() const { return m_id; }
113#endif
114
115 WTF_EXPORT_PRIVATE void changePriority(int);
116 WTF_EXPORT_PRIVATE int waitForCompletion();
117 WTF_EXPORT_PRIVATE void detach();
118
119#if OS(DARWIN)
120 using PlatformSuspendError = kern_return_t;
121#elif USE(PTHREADS)
122 using PlatformSuspendError = int;
123#elif OS(WINDOWS)
124 using PlatformSuspendError = DWORD;
125#endif
126
127 WTF_EXPORT_PRIVATE Expected<void, PlatformSuspendError> suspend();
128 WTF_EXPORT_PRIVATE void resume();
129 WTF_EXPORT_PRIVATE size_t getRegisters(PlatformRegisters&);
130
131#if USE(PTHREADS)
132 WTF_EXPORT_PRIVATE bool signal(int signalNumber);
133#endif
134
135 // Mark the current thread as requiring UI responsiveness.
136 // relativePriority is a value in the range [-15, 0] where a lower value indicates a lower priority.
137 WTF_EXPORT_PRIVATE static void setCurrentThreadIsUserInteractive(int relativePriority = 0);
138 WTF_EXPORT_PRIVATE static void setCurrentThreadIsUserInitiated(int relativePriority = 0);
139
140#if HAVE(QOS_CLASSES)
141 WTF_EXPORT_PRIVATE static void setGlobalMaxQOSClass(qos_class_t);
142 WTF_EXPORT_PRIVATE static qos_class_t adjustedQOSClass(qos_class_t);
143#endif
144
145 // Called in the thread during initialization.
146 // Helpful for platforms where the thread name must be set from within the thread.
147 static void initializeCurrentThreadInternal(const char* threadName);
148 static void initializeCurrentThreadEvenIfNonWTFCreated();
149
150 WTF_EXPORT_PRIVATE static const unsigned lockSpinLimit;
151 WTF_EXPORT_PRIVATE static void yield();
152
153 WTF_EXPORT_PRIVATE void dump(PrintStream& out) const;
154
155 static void initializePlatformThreading();
156
157 const StackBounds& stack() const
158 {
159 return m_stack;
160 }
161
162 AtomicStringTable* atomicStringTable()
163 {
164 return m_currentAtomicStringTable;
165 }
166
167 AtomicStringTable* setCurrentAtomicStringTable(AtomicStringTable* atomicStringTable)
168 {
169 AtomicStringTable* oldAtomicStringTable = m_currentAtomicStringTable;
170 m_currentAtomicStringTable = atomicStringTable;
171 return oldAtomicStringTable;
172 }
173
174#if ENABLE(STACK_STATS)
175 StackStats::PerThreadStats& stackStats()
176 {
177 return m_stackStats;
178 }
179#endif
180
181 void* savedStackPointerAtVMEntry()
182 {
183 return m_savedStackPointerAtVMEntry;
184 }
185
186 void setSavedStackPointerAtVMEntry(void* stackPointerAtVMEntry)
187 {
188 m_savedStackPointerAtVMEntry = stackPointerAtVMEntry;
189 }
190
191 void* savedLastStackTop()
192 {
193 return m_savedLastStackTop;
194 }
195
196 void setSavedLastStackTop(void* lastStackTop)
197 {
198 m_savedLastStackTop = lastStackTop;
199 }
200
201#if OS(DARWIN)
202 mach_port_t machThread() { return m_platformThread; }
203#endif
204
205 struct NewThreadContext;
206 static void entryPoint(NewThreadContext*);
207protected:
208 Thread() = default;
209
210 void initializeInThread();
211
212 // Internal platform-specific Thread establishment implementation.
213 bool establishHandle(NewThreadContext*);
214
215#if USE(PTHREADS)
216 void establishPlatformSpecificHandle(PlatformThreadHandle);
217#else
218 void establishPlatformSpecificHandle(PlatformThreadHandle, ThreadIdentifier);
219#endif
220
221#if USE(PTHREADS) && !OS(DARWIN)
222 static void signalHandlerSuspendResume(int, siginfo_t*, void* ucontext);
223#endif
224
225 static const char* normalizeThreadName(const char* threadName);
226
227 enum JoinableState : uint8_t {
228 // The default thread state. The thread can be joined on.
229 Joinable,
230
231 // Somebody waited on this thread to exit and this thread finally exited. This state is here because there can be a
232 // period of time between when the thread exits (which causes pthread_join to return and the remainder of waitOnThreadCompletion to run)
233 // and when threadDidExit is called. We need threadDidExit to take charge and delete the thread data since there's
234 // nobody else to pick up the slack in this case (since waitOnThreadCompletion has already returned).
235 Joined,
236
237 // The thread has been detached and can no longer be joined on. At this point, the thread must take care of cleaning up after itself.
238 Detached,
239 };
240
241 JoinableState joinableState() { return m_joinableState; }
242 void didBecomeDetached() { m_joinableState = Detached; }
243 void didExit();
244 void didJoin() { m_joinableState = Joined; }
245 bool hasExited() { return m_didExit; }
246
247 // These functions are only called from ThreadGroup.
248 ThreadGroupAddResult addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
249 void removeFromThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
250
251 // The Thread instance is ref'ed and held in thread-specific storage. It will be deref'ed by destructTLS at thread destruction time.
252 // For pthread, it employs pthreads-specific 2-pass destruction to reliably remove Thread.
253 // For Windows, we use thread_local to defer thread TLS destruction. It assumes regular ThreadSpecific
254 // types don't use multiple-pass destruction.
255
256#if !HAVE(FAST_TLS)
257 static WTF_EXPORT_PRIVATE ThreadSpecificKey s_key;
258 // One time initialization for this class as a whole.
259 // This method must be called before initializeTLS() and it is not thread-safe.
260 static void initializeTLSKey();
261#endif
262
263 // Creates and puts an instance of Thread into thread-specific storage.
264 static Thread& initializeTLS(Ref<Thread>&&);
265 WTF_EXPORT_PRIVATE static Thread& initializeCurrentTLS();
266
267 // Returns nullptr if thread-specific storage was not initialized.
268 static Thread* currentMayBeNull();
269
270#if OS(WINDOWS)
271 WTF_EXPORT_PRIVATE static Thread* currentDying();
272 static RefPtr<Thread> get(ThreadIdentifier);
273#endif
274
275 // This thread-specific destructor is called 2 times when thread terminates:
276 // - first, when all the other thread-specific destructors are called, it simply remembers it was 'destroyed once'
277 // and (1) re-sets itself into the thread-specific slot or (2) constructs thread local value to call it again later.
278 // - second, after all thread-specific destructors were invoked, it gets called again - this time, we remove the
279 // Thread from the threadMap, completing the cleanup.
280 static void THREAD_SPECIFIC_CALL destructTLS(void* data);
281
282 JoinableState m_joinableState { Joinable };
283 bool m_isShuttingDown { false };
284 bool m_didExit { false };
285 bool m_isDestroyedOnce { false };
286
287 // Lock & ParkingLot rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed.
288 // Use WordLock since WordLock does not depend on ThreadSpecific and this "Thread".
289 WordLock m_mutex;
290 StackBounds m_stack { StackBounds::emptyBounds() };
291 Vector<std::weak_ptr<ThreadGroup>> m_threadGroups;
292 PlatformThreadHandle m_handle;
293#if OS(WINDOWS)
294 ThreadIdentifier m_id { 0 };
295#elif OS(DARWIN)
296 mach_port_t m_platformThread { MACH_PORT_NULL };
297#elif USE(PTHREADS)
298 PlatformRegisters* m_platformRegisters { nullptr };
299 unsigned m_suspendCount { 0 };
300#endif
301
302 AtomicStringTable* m_currentAtomicStringTable { nullptr };
303 AtomicStringTable m_defaultAtomicStringTable;
304
305#if ENABLE(STACK_STATS)
306 StackStats::PerThreadStats m_stackStats;
307#endif
308 void* m_savedStackPointerAtVMEntry { nullptr };
309 void* m_savedLastStackTop;
310public:
311 void* m_apiData { nullptr };
312};
313
314inline Thread* Thread::currentMayBeNull()
315{
316#if !HAVE(FAST_TLS)
317 ASSERT(s_key != InvalidThreadSpecificKey);
318 return static_cast<Thread*>(threadSpecificGet(s_key));
319#else
320 return static_cast<Thread*>(_pthread_getspecific_direct(WTF_THREAD_DATA_KEY));
321#endif
322}
323
324inline Thread& Thread::current()
325{
326 // WRT WebCore:
327 // Thread::current() is used on main thread before it could possibly be used
328 // on secondary ones, so there is no need for synchronization here.
329 // WRT JavaScriptCore:
330 // Thread::initializeTLSKey() is initially called from initializeThreading(), ensuring
331 // this is initially called in a std::call_once locked context.
332#if !HAVE(FAST_TLS)
333 if (UNLIKELY(Thread::s_key == InvalidThreadSpecificKey))
334 WTF::initializeThreading();
335#endif
336 if (auto* thread = currentMayBeNull())
337 return *thread;
338#if OS(WINDOWS)
339 if (auto* thread = currentDying())
340 return *thread;
341#endif
342 return initializeCurrentTLS();
343}
344
345} // namespace WTF
346
347using WTF::Thread;
348
349#if OS(WINDOWS)
350using WTF::ThreadIdentifier;
351using WTF::createThread;
352using WTF::waitForThreadCompletion;
353#endif
354