#include "config.h"
#include "MainThread.h"
#include "Deque.h"
#include "MonotonicTime.h"
#include "StdLibExtras.h"
#include "Threading.h"
#include <mutex>
#include <wtf/Condition.h>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/ThreadSpecific.h>
namespace WTF {
static bool callbacksPaused; #if !PLATFORM(COCOA)
static Thread* mainThread { nullptr };
#endif
static Lock mainThreadFunctionQueueMutex;
static Deque<Function<void ()>>& functionQueue()
{
static NeverDestroyed<Deque<Function<void ()>>> functionQueue;
return functionQueue;
}
static std::once_flag initializeKey;
void initializeMainThread()
{
std::call_once(initializeKey, [] {
initializeThreading();
#if !PLATFORM(COCOA)
mainThread = &Thread::current();
#endif
initializeMainThreadPlatform();
initializeGCThreads();
});
}
#if !PLATFORM(COCOA)
bool isMainThread()
{
return mainThread == &Thread::current();
}
#endif
#if PLATFORM(COCOA)
#if !USE(WEB_THREAD)
void initializeMainThreadToProcessMainThread()
{
std::call_once(initializeKey, [] {
initializeThreading();
initializeMainThreadToProcessMainThreadPlatform();
initializeGCThreads();
});
}
#else
void initializeWebThread()
{
static std::once_flag initializeKey;
std::call_once(initializeKey, [] {
initializeWebThreadPlatform();
});
}
#endif // !USE(WEB_THREAD)
#endif // PLATFORM(COCOA)
#if !USE(WEB_THREAD)
bool canAccessThreadLocalDataForThread(Thread& thread)
{
return &thread == &Thread::current();
}
#endif
static const auto maxRunLoopSuspensionTime = 50_ms;
void dispatchFunctionsFromMainThread()
{
ASSERT(isMainThread());
if (callbacksPaused)
return;
auto startTime = MonotonicTime::now();
Function<void ()> function;
while (true) {
{
std::lock_guard<Lock> lock(mainThreadFunctionQueueMutex);
if (!functionQueue().size())
break;
function = functionQueue().takeFirst();
}
function();
function = nullptr;
if (MonotonicTime::now() - startTime > maxRunLoopSuspensionTime) {
scheduleDispatchFunctionsOnMainThread();
break;
}
}
}
void callOnMainThread(Function<void()>&& function)
{
ASSERT(function);
bool needToSchedule = false;
{
std::lock_guard<Lock> lock(mainThreadFunctionQueueMutex);
needToSchedule = functionQueue().size() == 0;
functionQueue().append(WTFMove(function));
}
if (needToSchedule)
scheduleDispatchFunctionsOnMainThread();
}
void setMainThreadCallbacksPaused(bool paused)
{
ASSERT(isMainThread());
if (callbacksPaused == paused)
return;
callbacksPaused = paused;
if (!callbacksPaused)
scheduleDispatchFunctionsOnMainThread();
}
static ThreadSpecific<std::optional<GCThreadType>, CanBeGCThread::True>* isGCThread;
void initializeGCThreads()
{
static std::once_flag flag;
std::call_once(
flag,
[] {
isGCThread = new ThreadSpecific<std::optional<GCThreadType>, CanBeGCThread::True>();
});
}
void registerGCThread(GCThreadType type)
{
if (!isGCThread) {
return;
}
**isGCThread = type;
}
bool isMainThreadOrGCThread()
{
if (mayBeGCThread())
return true;
return isMainThread();
}
std::optional<GCThreadType> mayBeGCThread()
{
if (!isGCThread)
return std::nullopt;
if (!isGCThread->isSet())
return std::nullopt;
return **isGCThread;
}
void callOnMainThreadAndWait(WTF::Function<void()>&& function)
{
if (isMainThread()) {
function();
return;
}
Lock mutex;
Condition conditionVariable;
bool isFinished = false;
callOnMainThread([&, function = WTFMove(function)] {
function();
std::lock_guard<Lock> lock(mutex);
isFinished = true;
conditionVariable.notifyOne();
});
std::unique_lock<Lock> lock(mutex);
conditionVariable.wait(lock, [&] {
return isFinished;
});
}
}