#include "config.h"
#include <wtf/Threading.h>
#include <cstring>
#include <wtf/DateMath.h>
#include <wtf/PrintStream.h>
#include <wtf/RandomNumberSeed.h>
#include <wtf/ThreadGroup.h>
#include <wtf/ThreadingPrimitives.h>
#include <wtf/WTFConfig.h>
#include <wtf/threads/Signals.h>
#if HAVE(QOS_CLASSES)
#include <bmalloc/bmalloc.h>
#endif
namespace WTF {
static Optional<size_t> stackSize(ThreadType threadType)
{
#if PLATFORM(PLAYSTATION)
if (threadType == ThreadType::JavaScript)
return 512 * KB;
#elif OS(DARWIN) && ASAN_ENABLED
if (threadType == ThreadType::Compiler)
return 1 * MB; #else
UNUSED_PARAM(threadType);
#endif
#if defined(DEFAULT_THREAD_STACK_SIZE_IN_KB) && DEFAULT_THREAD_STACK_SIZE_IN_KB > 0
return DEFAULT_THREAD_STACK_SIZE_IN_KB * 1024;
#else
return WTF::nullopt;
#endif
}
std::atomic<uint32_t> Thread::s_uid { 0 };
struct Thread::NewThreadContext : public ThreadSafeRefCounted<NewThreadContext> {
public:
NewThreadContext(const char* name, Function<void()>&& entryPoint, Ref<Thread>&& thread)
: name(name)
, entryPoint(WTFMove(entryPoint))
, thread(WTFMove(thread))
{
}
enum class Stage { Start, EstablishedHandle, Initialized };
Stage stage { Stage::Start };
const char* name;
Function<void()> entryPoint;
Ref<Thread> thread;
Mutex mutex;
#if !HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
ThreadCondition condition;
#endif
};
HashSet<Thread*>& Thread::allThreads(const LockHolder&)
{
static LazyNeverDestroyed<HashSet<Thread*>> allThreads;
static std::once_flag onceKey;
std::call_once(onceKey, [&] {
allThreads.construct();
});
return allThreads;
}
Lock& Thread::allThreadsMutex()
{
static Lock mutex;
return mutex;
}
const char* Thread::normalizeThreadName(const char* threadName)
{
#if HAVE(PTHREAD_SETNAME_NP)
return threadName;
#else
StringView result(threadName);
size_t size = result.reverseFind('.');
if (size != notFound)
result = result.substring(size + 1);
#if OS(WINDOWS)
constexpr const size_t kVisualStudioThreadNameLimit = 32 - 1;
if (result.length() > kVisualStudioThreadNameLimit)
result = result.right(kVisualStudioThreadNameLimit);
#elif OS(LINUX)
constexpr const size_t kLinuxThreadNameLimit = 16 - 1;
if (result.length() > kLinuxThreadNameLimit)
result = result.right(kLinuxThreadNameLimit);
#endif
ASSERT(result.characters8()[result.length()] == '\0');
return reinterpret_cast<const char*>(result.characters8());
#endif
}
void Thread::initializeInThread()
{
if (m_stack.isEmpty())
m_stack = StackBounds::currentThreadStackBounds();
m_savedLastStackTop = stack().origin();
m_currentAtomStringTable = &m_defaultAtomStringTable;
#if USE(WEB_THREAD)
if (isWebThread() || isUIThread()) {
static LazyNeverDestroyed<AtomStringTable> sharedStringTable;
static std::once_flag onceKey;
std::call_once(onceKey, [&] {
sharedStringTable.construct();
});
m_currentAtomStringTable = &sharedStringTable.get();
}
#endif
#if OS(LINUX)
m_id = currentID();
#endif
}
void Thread::entryPoint(NewThreadContext* newThreadContext)
{
Function<void()> function;
{
Ref<NewThreadContext> context = adoptRef(*newThreadContext);
MutexLocker locker(context->mutex);
ASSERT(context->stage == NewThreadContext::Stage::EstablishedHandle);
Thread::initializeCurrentThreadInternal(context->name);
function = WTFMove(context->entryPoint);
context->thread->initializeInThread();
Thread::initializeTLS(WTFMove(context->thread));
#if !HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
context->stage = NewThreadContext::Stage::Initialized;
context->condition.signal();
#endif
}
ASSERT(!Thread::current().stack().isEmpty());
function();
}
Ref<Thread> Thread::create(const char* name, Function<void()>&& entryPoint, ThreadType threadType, QOS qos)
{
WTF::initialize();
Ref<Thread> thread = adoptRef(*new Thread());
Ref<NewThreadContext> context = adoptRef(*new NewThreadContext { name, WTFMove(entryPoint), thread.copyRef() });
context->ref();
{
MutexLocker locker(context->mutex);
bool success = thread->establishHandle(context.ptr(), stackSize(threadType), qos);
RELEASE_ASSERT(success);
context->stage = NewThreadContext::Stage::EstablishedHandle;
#if HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
thread->m_stack = StackBounds::newThreadStackBounds(thread->m_handle);
#else
while (context->stage != NewThreadContext::Stage::Initialized)
context->condition.wait(context->mutex);
#endif
}
{
auto locker = holdLock(allThreadsMutex());
if (!thread->m_didUnregisterFromAllThreads)
allThreads(locker).add(thread.ptr());
}
ASSERT(!thread->stack().isEmpty());
return thread;
}
static bool shouldRemoveThreadFromThreadGroup()
{
#if OS(WINDOWS)
if (WTF::isMainThread())
return false;
#endif
return true;
}
void Thread::didExit()
{
{
auto locker = holdLock(allThreadsMutex());
allThreads(locker).remove(this);
m_didUnregisterFromAllThreads = true;
}
if (shouldRemoveThreadFromThreadGroup()) {
{
Vector<std::shared_ptr<ThreadGroup>> threadGroups;
{
auto locker = holdLock(m_mutex);
for (auto& threadGroupPointerPair : m_threadGroupMap) {
if (auto retained = threadGroupPointerPair.value.lock())
threadGroups.append(WTFMove(retained));
}
m_isShuttingDown = true;
}
for (auto& threadGroup : threadGroups) {
auto threadGroupLocker = holdLock(threadGroup->getLock());
auto locker = holdLock(m_mutex);
threadGroup->m_threads.remove(*this);
}
}
auto locker = holdLock(m_mutex);
m_didExit = true;
}
}
ThreadGroupAddResult Thread::addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup& threadGroup)
{
UNUSED_PARAM(threadGroupLocker);
auto locker = holdLock(m_mutex);
if (m_isShuttingDown)
return ThreadGroupAddResult::NotAdded;
if (threadGroup.m_threads.add(*this).isNewEntry) {
m_threadGroupMap.add(&threadGroup, threadGroup.weakFromThis());
return ThreadGroupAddResult::NewlyAdded;
}
return ThreadGroupAddResult::AlreadyAdded;
}
void Thread::removeFromThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup& threadGroup)
{
UNUSED_PARAM(threadGroupLocker);
auto locker = holdLock(m_mutex);
if (m_isShuttingDown)
return;
m_threadGroupMap.remove(&threadGroup);
}
unsigned Thread::numberOfThreadGroups()
{
auto locker = holdLock(m_mutex);
return m_threadGroupMap.size();
}
bool Thread::exchangeIsCompilationThread(bool newValue)
{
auto& thread = Thread::current();
bool oldValue = thread.m_isCompilationThread;
thread.m_isCompilationThread = newValue;
return oldValue;
}
void Thread::registerGCThread(GCThreadType gcThreadType)
{
Thread::current().m_gcThreadType = static_cast<unsigned>(gcThreadType);
}
bool Thread::mayBeGCThread()
{
return Thread::current().gcThreadType() != GCThreadType::None;
}
void Thread::setCurrentThreadIsUserInteractive(int relativePriority)
{
#if HAVE(QOS_CLASSES)
ASSERT(relativePriority <= 0);
ASSERT(relativePriority >= QOS_MIN_RELATIVE_PRIORITY);
pthread_set_qos_class_self_np(adjustedQOSClass(QOS_CLASS_USER_INTERACTIVE), relativePriority);
#else
UNUSED_PARAM(relativePriority);
#endif
}
void Thread::setCurrentThreadIsUserInitiated(int relativePriority)
{
#if HAVE(QOS_CLASSES)
ASSERT(relativePriority <= 0);
ASSERT(relativePriority >= QOS_MIN_RELATIVE_PRIORITY);
pthread_set_qos_class_self_np(adjustedQOSClass(QOS_CLASS_USER_INITIATED), relativePriority);
#else
UNUSED_PARAM(relativePriority);
#endif
}
#if HAVE(QOS_CLASSES)
static qos_class_t globalMaxQOSclass { QOS_CLASS_UNSPECIFIED };
void Thread::setGlobalMaxQOSClass(qos_class_t maxClass)
{
bmalloc::api::setScavengerThreadQOSClass(maxClass);
globalMaxQOSclass = maxClass;
}
qos_class_t Thread::adjustedQOSClass(qos_class_t originalClass)
{
if (globalMaxQOSclass != QOS_CLASS_UNSPECIFIED)
return std::min(originalClass, globalMaxQOSclass);
return originalClass;
}
#endif
void Thread::dump(PrintStream& out) const
{
out.print("Thread:", RawPointer(this));
}
#if !HAVE(FAST_TLS) && !OS(WINDOWS)
ThreadSpecificKey Thread::s_key = InvalidThreadSpecificKey;
#endif
void initialize()
{
static std::once_flag onceKey;
std::call_once(onceKey, [] {
setPermissionsOfConfigPage();
Config::AssertNotFrozenScope assertScope;
initializeRandomNumberGenerator();
#if !HAVE(FAST_TLS) && !OS(WINDOWS)
Thread::initializeTLSKey();
#endif
initializeDates();
Thread::initializePlatformThreading();
#if USE(PTHREADS) && HAVE(MACHINE_CONTEXT)
SignalHandlers::initialize();
#endif
});
}
WTF_EXPORT_PRIVATE void initializeThreading();
void initializeThreading()
{
initialize();
}
}