/* * Copyright (C) 2007-2020 Apple Inc. All rights reserved. * Copyright (C) 2007 Justin Haygood * Copyright (C) 2017 Yusuke Suzuki * Copyright (C) 2026 Igalia S.L. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if USE(PTHREADS) && !OS(DARWIN) #include #endif #if OS(WINDOWS) #include #endif #if HAVE(QOS_CLASSES) #include #endif // X11 headers define a bunch of macros with common terms, interfering with WebCore and WTF enum values. // As a workaround, we explicitly undef them here. #if defined(None) #undef None #endif namespace WTF { class AbstractLocker; class ThreadMessageData; enum class ThreadGroupAddResult; class ThreadGroup; class PrintStream; WTF_EXPORT_PRIVATE void initialize(); WTF_EXPORT_PRIVATE bool LEGACY_NODELETE processIsShuttingDown(); class ThreadSuspendLocker { WTF_MAKE_NONCOPYABLE(ThreadSuspendLocker); public: WTF_EXPORT_PRIVATE ThreadSuspendLocker(); explicit ThreadSuspendLocker(AdoptLockTag) : m_shouldUnlock(false) { } WTF_EXPORT_PRIVATE ~ThreadSuspendLocker(); private: bool m_shouldUnlock { true }; }; class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr, ThreadLike { public: friend class ThreadGroup; friend WTF_EXPORT_PRIVATE void initialize(); #if OS(LINUX) friend class HighPriorityThreads; #endif class ClientData : public ThreadSafeRefCounted { public: virtual ~ClientData() = default; enum class Type : uint8_t { WebCoreThreadGlobalData }; Type type() const { return Type::WebCoreThreadGlobalData; } protected: explicit ClientData(Type type) { ASSERT_UNUSED(type, type == Type::WebCoreThreadGlobalData); } }; WTF_EXPORT_PRIVATE ~Thread(); using QOS = ThreadQOS; using SchedulingPolicy = ThreadSchedulingPolicy; using SchedulingState = ThreadSchedulingState; static constexpr QOS defaultQOS = defaultThreadQOS; static constexpr SchedulingPolicy defaultSchedulingPolicy = defaultThreadSchedulingPolicy; #if HAVE(QOS_CLASSES) static dispatch_qos_class_t LEGACY_NODELETE dispatchQOSClass(QOS); #endif // Returns nullptr if thread creation failed. // The thread name must be a literal since on some platforms it's passed in to the thread. WTF_EXPORT_PRIVATE static Ref create(ASCIILiteral threadName, Function&&, ThreadType = ThreadType::Unknown, QOS = defaultQOS, SchedulingPolicy = defaultSchedulingPolicy, StackAllocationSpecification = { }); #if ENABLE(AOT) // A process that runs ahead-of-time compiled code starts its threads late, when a limit on threads may have been reached. There create() waits a little for room, // and crashes with a line that names the thread and the error; and a thread that the work gets done without (AutomaticThread::isOptional()) is left out. static bool survivesFailedCreation() { return s_survivesFailedCreation; } WTF_EXPORT_PRIVATE static void setSurvivesFailedCreation(); // Returns nullptr if thread creation failed: the limit on threads is reached, or there is no memory for a stack. errno says which. WTF_EXPORT_PRIVATE static RefPtr tryCreate(ASCIILiteral threadName, Function&&, ThreadType = ThreadType::Unknown, QOS = defaultQOS, SchedulingPolicy = defaultSchedulingPolicy, StackAllocationSpecification = { }); #endif // Returns Thread object. static Thread& currentSingleton(); // Set of all WTF::Thread created threads. WTF_EXPORT_PRIVATE static ThreadSafeWeakHashSet& LEGACY_NODELETE allThreads(); WTF_EXPORT_PRIVATE unsigned numberOfThreadGroups(); uint32_t uid() const { return m_uid; } #if OS(WINDOWS) // Returns ThreadIdentifier directly. It is useful if the user only cares about identity // of threads. At that time, users should know that holding this ThreadIdentifier does not ensure // that the thread information is alive. While Thread::currentSingleton() is not safe if it is called // from the destructor of the other TLS data, currentID() always returns meaningful thread ID. WTF_EXPORT_PRIVATE static ThreadIdentifier currentID(); ThreadIdentifier id() const { return m_id; } class SpecificStorage { public: using DestroyFunction = void (*)(void*); WTF_EXPORT_PRIVATE static bool allocateKey(int& key, DestroyFunction); WTF_EXPORT_PRIVATE void* get(int key); WTF_EXPORT_PRIVATE void set(int key, void* value); void destroySlots(); private: static constexpr size_t s_maxKeys = 32; static Atomic s_numberOfKeys; static std::array, s_maxKeys> s_destroyFunctions; std::array m_slots { }; }; SpecificStorage& specificStorage() LIFETIME_BOUND { return m_specificStorage; }; struct ThreadHolder; #endif WTF_EXPORT_PRIVATE void changePriority(int); WTF_EXPORT_PRIVATE int waitForCompletion(); WTF_EXPORT_PRIVATE void detach(); #if OS(DARWIN) using PlatformSuspendError = kern_return_t; #elif USE(PTHREADS) using PlatformSuspendError = int; #elif OS(WINDOWS) using PlatformSuspendError = DWORD; #endif WTF_EXPORT_PRIVATE std::expected suspend(const ThreadSuspendLocker&); WTF_EXPORT_PRIVATE void resume(const ThreadSuspendLocker&); WTF_EXPORT_PRIVATE size_t getRegisters(const ThreadSuspendLocker&, PlatformRegisters&); // Forces this thread through a context-synchronizing event so it re-fetches instructions that // were modified by another thread (the caller must have already flushed the modified code from // the instruction cache). This does not read or modify the thread's state; it only publishes the // change to it. WTF_EXPORT_PRIVATE void barrierInstructionCache(); #if USE(PTHREADS) #if OS(LINUX) WTF_EXPORT_PRIVATE static ThreadIdentifier currentID(); ThreadIdentifier id() const { return m_id; } #endif WTF_EXPORT_PRIVATE bool signal(int signalNumber); #endif // Mark the current thread as requiring UI responsiveness. // relativePriority is a value in the range [-15, 0] where a lower value indicates a lower priority. WTF_EXPORT_PRIVATE static void setCurrentThreadIsUserInteractive(int relativePriority = 0); WTF_EXPORT_PRIVATE static void setCurrentThreadIsUserInitiated(int relativePriority = 0); WTF_EXPORT_PRIVATE static QOS currentThreadQOS(); WTF_EXPORT_PRIVATE static bool currentThreadIsRealtime(); bool isRealtime() const { return m_schedulingPolicy == SchedulingPolicy::Realtime; } QOS qos() const { return m_qos; } #if HAVE(QOS_CLASSES) WTF_EXPORT_PRIVATE static void setGlobalMaxQOSClass(qos_class_t); #endif #if HAVE(THREAD_TIME_CONSTRAINTS) // Set thread timing constraints, which allows the scheduler to demote // threads which exceed their own reported constraints. WTF_EXPORT_PRIVATE void setThreadTimeConstraints(MonotonicTime period, MonotonicTime nominalComputation, MonotonicTime constraint, bool isPreemptable); #endif // Called in the thread during initialization. // Helpful for platforms where the thread name must be set from within the thread. static void initializeCurrentThreadInternal(const char* threadName); static void LEGACY_NODELETE initializeCurrentThreadEvenIfNonWTFCreated(); WTF_EXPORT_PRIVATE static void yield(); WTF_EXPORT_PRIVATE static bool exchangeIsCompilationThread(bool newValue); WTF_EXPORT_PRIVATE static void registerGCThread(GCThreadType); WTF_EXPORT_PRIVATE static bool mayBeGCThread(); WTF_EXPORT_PRIVATE static void LEGACY_NODELETE registerJSThread(Thread&); WTF_EXPORT_PRIVATE void dump(PrintStream& out) const; static void initializePlatformThreading(); const StackBounds& stack() const { return m_stack; } AtomStringTable* atomStringTable() { return m_currentAtomStringTable; } AtomStringTable* setCurrentAtomStringTable(AtomStringTable* atomStringTable) { AtomStringTable* oldAtomStringTable = m_currentAtomStringTable; m_currentAtomStringTable = atomStringTable; return oldAtomStringTable; } #if ENABLE(STACK_STATS) StackStats::PerThreadStats& stackStats() { return m_stackStats; } #endif void* savedStackPointerAtVMEntry() const { return m_savedStackPointerAtVMEntry; } void setSavedStackPointerAtVMEntry(void* stackPointerAtVMEntry) { m_savedStackPointerAtVMEntry = stackPointerAtVMEntry; } void* savedLastStackTop() const { return m_savedLastStackTop; } void setSavedLastStackTop(void* lastStackTop) { m_savedLastStackTop = lastStackTop; } #if OS(DARWIN) mach_port_t machThread() { return m_platformThread; } #endif bool isCompilationThread() const { return m_isCompilationThread; } bool isJSThread() const { return m_isJSThread; } GCThreadType gcThreadType() const { return static_cast(m_gcThreadType); } struct NewThreadContext; static void entryPoint(NewThreadContext*); ThreadLikeAssertion threadLikeAssertion() const { return createThreadLikeAssertion(m_uid); } // Returns nullptr if thread-specific storage was not initialized. #if OS(WINDOWS) WTF_EXPORT_PRIVATE static Thread* currentMayBeNull(); #else static Thread* currentMayBeNull(); #endif private: void LEGACY_NODELETE updateSchedulingAttributes(SchedulingState) const; // updateSchedulingAttributes for the first time + initialization bookkeeping void LEGACY_NODELETE initializeSchedulingAttributes(); static void setCurrentThreadQOS(QOS); protected: enum class IsMain : uint8_t { No, Yes, Unknown }; explicit Thread(QOS qos, SchedulingPolicy schedulingPolicy, IsMain isMain = IsMain::Unknown) : m_qos(qos) , m_schedulingPolicy(schedulingPolicy) , m_uid(isMain == IsMain::Yes ? 1 : ++s_uid) { } void initializeInThread(); // Internal platform-specific Thread establishment implementation. bool establishHandle(NewThreadContext&, StackAllocationSpecification, QOS, SchedulingPolicy); #if ENABLE(AOT) static RefPtr tryCreate(ASCIILiteral threadName, Function&&, ThreadType, QOS, SchedulingPolicy, StackAllocationSpecification, Seconds timeToWaitForRoom); WTF_EXPORT_PRIVATE static bool s_survivesFailedCreation; #endif #if USE(PTHREADS) void establishPlatformSpecificHandle(PlatformThreadHandle); #else void establishPlatformSpecificHandle(PlatformThreadHandle, ThreadIdentifier); #endif #if USE(PTHREADS) && !OS(DARWIN) static void signalHandlerSuspendResume(int, siginfo_t*, void* ucontext); #endif #if HAVE(QOS_CLASSES) static qos_class_t LEGACY_NODELETE adjustedQOSClass(qos_class_t); #endif static const char* LEGACY_NODELETE normalizeThreadName(const char* threadName); enum JoinableState : uint8_t { // The default thread state. The thread can be joined on. Joinable, // Somebody waited on this thread to exit and this thread finally exited. This state is here because there can be a // period of time between when the thread exits (which causes pthread_join to return and the remainder of waitOnThreadCompletion to run) // and when threadDidExit is called. We need threadDidExit to take charge and delete the thread data since there's // nobody else to pick up the slack in this case (since waitOnThreadCompletion has already returned). Joined, // 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. Detached, }; JoinableState joinableState() const { return m_joinableState; } void didBecomeDetached() { m_joinableState = Detached; } void didExit(); void didJoin() { m_joinableState = Joined; } bool hasExited() const { return m_didExit; } // These functions are only called from ThreadGroup. ThreadGroupAddResult addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&); // For pthread, the Thread instance is ref'ed and held in thread-specific storage. It will be deref'ed by destructTLS at thread destruction time. // It employs pthreads-specific 2-pass destruction to reliably remove Thread. #if !HAVE(FAST_TLS) && !OS(WINDOWS) static WTF_EXPORT_PRIVATE ThreadSpecificKey s_key; // One time initialization for this class as a whole. // This method must be called before initializeTLS() and it is not thread-safe. static void initializeTLSKey(); #endif // This thread-specific destructor is called 2 times when thread terminates: // - first, when all the other thread-specific destructors are called, it simply remembers it was 'destroyed once' // and (1) re-sets itself into the thread-specific slot or (2) constructs thread local value to call it again later. // - second, after all thread-specific destructors were invoked, it gets called again - this time, we deref the // Thread in the TLS, completing the cleanup. static void destructTLS(void* data); // Creates and puts an instance of Thread into thread-specific storage. static Thread& initializeTLS(Ref&&); WTF_EXPORT_PRIVATE static Thread& initializeCurrentTLS(); JoinableState m_joinableState { Joinable }; bool m_isShuttingDown : 1 { false }; bool m_didExit : 1 { false }; bool m_isDestroyedOnce : 1 { false }; bool m_isCompilationThread: 1 { false }; bool m_isJSThread : 1 { false }; unsigned m_gcThreadType : 2 { static_cast(GCThreadType::None) }; QOS m_qos { defaultQOS }; SchedulingPolicy m_schedulingPolicy { SchedulingPolicy::Other }; // Lock & ParkingLot rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed. // Use WordLock since WordLock does not depend on ThreadSpecific and this "Thread". WordLock m_mutex; StackBounds m_stack { StackBounds::emptyBounds() }; ThreadSafeWeakHashSet m_threadGroups; PlatformThreadHandle m_handle; const uint32_t m_uid; #if OS(WINDOWS) ThreadIdentifier m_id { 0 }; #elif OS(DARWIN) mach_port_t m_platformThread { MACH_PORT_NULL }; #elif USE(PTHREADS) #if OS(LINUX) ThreadIdentifier m_id { 0 }; #endif PlatformRegisters* m_platformRegisters { nullptr }; unsigned m_suspendCount { 0 }; #endif #if OS(WINDOWS) SpecificStorage m_specificStorage; #endif AtomStringTable* m_currentAtomStringTable { nullptr }; AtomStringTable m_defaultAtomStringTable; #if ENABLE(STACK_STATS) StackStats::PerThreadStats m_stackStats; #endif void* m_savedStackPointerAtVMEntry { nullptr }; void* m_savedLastStackTop; public: void* m_apiData { nullptr }; RefPtr m_clientData { nullptr }; }; #if !OS(WINDOWS) inline Thread* Thread::currentMayBeNull() { #if !HAVE(FAST_TLS) ASSERT(s_key != InvalidThreadSpecificKey); return static_cast(threadSpecificGet(s_key)); #else return static_cast(_pthread_getspecific_direct(WTF_THREAD_DATA_KEY)); #endif } #endif inline Thread& Thread::currentSingleton() { // WRT WebCore: // Thread::currentSingleton() is used on main thread before it could possibly be used // on secondary ones, so there is no need for synchronization here. // WRT JavaScriptCore: // Thread::initializeTLSKey() is initially called from initialize(), ensuring // this is initially called in a std::call_once locked context. #if !HAVE(FAST_TLS) && !OS(WINDOWS) if (Thread::s_key == InvalidThreadSpecificKey) [[unlikely]] WTF::initialize(); #endif if (SUPPRESS_UNCOUNTED_LOCAL auto* thread = currentMayBeNull(); thread) [[likely]] return *thread; return initializeCurrentTLS(); } inline void assertIsCurrent(const Thread& thread) WTF_ASSERTS_ACQUIRED_CAPABILITY(thread) { assertIsCurrent(thread.threadLikeAssertion()); } } // namespace WTF using WTF::ThreadSuspendLocker; using WTF::Thread; using WTF::ThreadType; using WTF::GCThreadType; using WTF::assertIsCurrent;