#include "config.h"
#include "Threading.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <thread>
#include <wtf/DateMath.h>
#include <wtf/PrintStream.h>
#include <wtf/RandomNumberSeed.h>
#include <wtf/ThreadGroup.h>
#include <wtf/ThreadMessage.h>
#include <wtf/ThreadingPrimitives.h>
#include <wtf/text/AtomicStringTable.h>
#include <wtf/text/StringView.h>
#if HAVE(QOS_CLASSES)
#include <bmalloc/bmalloc.h>
#endif
namespace WTF {
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))
{
}
const char* name;
Function<void()> entryPoint;
Ref<Thread> thread;
Mutex mutex;
enum class Stage { Start, EstablishedHandle, Initialized };
Stage stage { Stage::Start };
#if !HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
ThreadCondition condition;
#endif
};
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_currentAtomicStringTable = &m_defaultAtomicStringTable;
#if USE(WEB_THREAD)
if (isWebThread() || isUIThread()) {
static NeverDestroyed<AtomicStringTable> sharedStringTable;
m_currentAtomicStringTable = &sharedStringTable.get();
}
#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)
{
WTF::initializeThreading();
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());
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
}
ASSERT(!thread->stack().isEmpty());
return thread;
}
static bool shouldRemoveThreadFromThreadGroup()
{
#if OS(WINDOWS)
if (WTF::isMainThread())
return false;
#endif
return true;
}
void Thread::didExit()
{
if (shouldRemoveThreadFromThreadGroup()) {
Vector<std::shared_ptr<ThreadGroup>> threadGroups;
{
auto locker = holdLock(m_mutex);
for (auto& threadGroup : m_threadGroups) {
if (auto retained = threadGroup.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_threadGroups.append(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_threadGroups.removeFirstMatching([&] (auto weakPtr) {
if (auto sharedPtr = weakPtr.lock())
return sharedPtr.get() == &threadGroup;
return false;
});
}
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)
ThreadSpecificKey Thread::s_key = InvalidThreadSpecificKey;
#endif
void initializeThreading()
{
static std::once_flag onceKey;
std::call_once(onceKey, [] {
initializeRandomNumberGenerator();
#if !HAVE(FAST_TLS)
Thread::initializeTLSKey();
#endif
initializeDates();
Thread::initializePlatformThreading();
});
}
}