// Copyright (c) Wojciech Figat. All rights reserved. #pragma once #if PLATFORM_WEB #include "../Base/ThreadBase.h" #ifdef __EMSCRIPTEN_PTHREADS__ #include #include #endif /// /// Thread object for Web platform. /// class FLAXENGINE_API WebThread : public ThreadBase { public: /// /// Initializes a new instance of the class. /// /// The runnable. /// The thread name. /// The thread priority. WebThread(IRunnable* runnable, const String& name, ThreadPriority priority) : ThreadBase(runnable, name, priority) #ifdef __EMSCRIPTEN_PTHREADS__ , _thread(0) #endif { } public: /// /// Factory method to create a thread with the specified stack size and thread priority /// /// The runnable object to execute /// Name of the thread /// Tells the thread whether it needs to adjust its priority or not. Defaults to normal priority /// The size of the stack to create. 0 means use the current thread's stack size /// Pointer to the new thread or null if cannot create it static WebThread* Create(IRunnable* runnable, const String& name, ThreadPriority priority = ThreadPriority::Normal, uint32 stackSize = 0); #ifdef __EMSCRIPTEN_PTHREADS__ // [ThreadBase] void Join() override { pthread_join(_thread, nullptr); ClearHandleInternal(); } protected: // [ThreadBase] void ClearHandleInternal() override { _thread = 0; } void SetPriorityInternal(ThreadPriority priority) override { // Not supported } void KillInternal(bool waitForJoin) override { if (waitForJoin) pthread_join(_thread, nullptr); pthread_kill(_thread, SIGKILL); } private: pthread_t _thread; static void* ThreadProc(void* pThis) { auto thread = (WebThread*)pThis; const int32 exitCode = thread->Run(); return (void*)(uintptr)exitCode; } #else // [ThreadBase] void Join() override { } protected: // [ThreadBase] void ClearHandleInternal() override { } void SetPriorityInternal(ThreadPriority priority) override { } void KillInternal(bool waitForJoin) override { } #endif }; #endif