WorkerOrWorkletThread.h [plain text]
#pragma once
#include "WorkerRunLoop.h"
#include <wtf/Forward.h>
#include <wtf/Function.h>
#include <wtf/Lock.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/threads/BinarySemaphore.h>
namespace WTF {
class Thread;
}
namespace WebCore {
class WorkerDebuggerProxy;
class WorkerLoaderProxy;
class WorkerRunLoop;
class WorkerOrWorkletThread : public ThreadSafeRefCounted<WorkerOrWorkletThread> {
public:
virtual ~WorkerOrWorkletThread();
WTF::Thread* thread() const { return m_thread.get(); }
virtual WorkerDebuggerProxy* workerDebuggerProxy() const = 0;
virtual WorkerLoaderProxy& workerLoaderProxy() = 0;
WorkerOrWorkletGlobalScope* globalScope() const { return m_globalScope.get(); }
WorkerRunLoop& runLoop() { return m_runLoop; }
void start(Function<void(const String&)>&& evaluateCallback = { });
void stop(Function<void()>&& terminatedCallback = { });
void startRunningDebuggerTasks();
void stopRunningDebuggerTasks();
void suspend();
void resume();
const String& identifier() const { return m_identifier; }
static HashSet<WorkerOrWorkletThread*>& workerOrWorkletThreads();
static Lock& workerOrWorkletThreadsLock();
static void releaseFastMallocFreeMemoryInAllThreads();
protected:
explicit WorkerOrWorkletThread(const String& identifier);
void workerOrWorkletThread();
virtual void runEventLoop();
private:
virtual Ref<WTF::Thread> createThread() = 0;
virtual Ref<WorkerOrWorkletGlobalScope> createGlobalScope() = 0;
virtual void evaluateScriptIfNecessary(String&) { }
virtual bool shouldWaitForWebInspectorOnStartup() const { return false; }
String m_identifier;
Lock m_threadCreationAndGlobalScopeLock;
RefPtr<WorkerOrWorkletGlobalScope> m_globalScope;
RefPtr<WTF::Thread> m_thread;
WorkerRunLoop m_runLoop;
Function<void(const String&)> m_evaluateCallback;
Function<void()> m_stoppedCallback;
BinarySemaphore m_suspensionSemaphore;
bool m_isSuspended { false };
bool m_pausedForDebugger { false };
};
}