#include "config.h"
#include "MainThread.h"
#include "CurrentTime.h"
#include "Deque.h"
#include "StdLibExtras.h"
#include "Threading.h"
#include <mutex>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/ThreadSpecific.h>
namespace WTF {
static bool callbacksPaused; #if !PLATFORM(COCOA)
static ThreadIdentifier mainThreadIdentifier;
#endif
static StaticLock 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)
mainThreadIdentifier = currentThread();
#endif
initializeMainThreadPlatform();
initializeGCThreads();
});
}
#if !PLATFORM(COCOA)
bool isMainThread()
{
return currentThread() == mainThreadIdentifier;
}
#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(ThreadIdentifier threadId)
{
return threadId == currentThread();
}
#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<StaticLock> 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<StaticLock> 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;
}
}