renderer: ++wasm build portability

removed the direct thread inclusion
when those thread features are disabled
This commit is contained in:
Hermet Park 2025-03-12 12:37:33 +09:00 committed by Hermet Park
parent 6847d951d8
commit 36be459c80
3 changed files with 32 additions and 16 deletions

View file

@ -20,24 +20,17 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <thread>
#include <atomic>
#include "tvgArray.h" #include "tvgArray.h"
#include "tvgInlist.h" #include "tvgInlist.h"
#include "tvgTaskScheduler.h" #include "tvgTaskScheduler.h"
/************************************************************************/ /************************************************************************/
/* Internal Class Implementation */ /* Internal Class Implementation */
/************************************************************************/ /************************************************************************/
namespace tvg { namespace tvg {
struct TaskSchedulerImpl;
static TaskSchedulerImpl* _inst = nullptr;
static std::thread::id _tid; //dominant thread id
#ifdef THORVG_THREAD_SUPPORT #ifdef THORVG_THREAD_SUPPORT
static thread_local bool _async = true; static thread_local bool _async = true;
@ -196,11 +189,14 @@ struct TaskSchedulerImpl
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
static TaskSchedulerImpl* _inst = nullptr;
static ThreadID _tid; //dominant thread id
void TaskScheduler::init(uint32_t threads) void TaskScheduler::init(uint32_t threads)
{ {
if (_inst) return; if (_inst) return;
_inst = new TaskSchedulerImpl(threads); _inst = new TaskSchedulerImpl(threads);
_tid = std::this_thread::get_id(); _tid = tid();
} }
@ -219,8 +215,7 @@ void TaskScheduler::request(Task* task)
uint32_t TaskScheduler::threads() uint32_t TaskScheduler::threads()
{ {
if (_inst) return _inst->threadCnt(); return _inst ? _inst->threadCnt() : 0;
return 0;
} }
@ -230,7 +225,18 @@ void TaskScheduler::async(bool on)
_async = on; _async = on;
} }
bool TaskScheduler::onthread() bool TaskScheduler::onthread()
{ {
return _tid != std::this_thread::get_id(); return _tid != tid();
}
ThreadID TaskScheduler::tid()
{
#ifdef THORVG_THREAD_SUPPORT
return std::this_thread::get_id();
#else
return 0;
#endif
} }

View file

@ -23,16 +23,23 @@
#ifndef _TVG_TASK_SCHEDULER_H_ #ifndef _TVG_TASK_SCHEDULER_H_
#define _TVG_TASK_SCHEDULER_H_ #define _TVG_TASK_SCHEDULER_H_
#include <mutex>
#include <condition_variable>
#include "tvgCommon.h" #include "tvgCommon.h"
#include "tvgInlist.h" #include "tvgInlist.h"
#ifdef THORVG_THREAD_SUPPORT
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#endif
namespace tvg { namespace tvg {
#ifdef THORVG_THREAD_SUPPORT #ifdef THORVG_THREAD_SUPPORT
using ThreadID = std::thread::id;
struct Task struct Task
{ {
private: private:
@ -79,6 +86,8 @@ private:
#else //THORVG_THREAD_SUPPORT #else //THORVG_THREAD_SUPPORT
using ThreadID = uint8_t;
struct Task struct Task
{ {
public: public:
@ -105,6 +114,7 @@ struct TaskScheduler
static void request(Task* task); static void request(Task* task);
static void async(bool on); static void async(bool on);
static bool onthread(); //figure out whether on worker thread or not static bool onthread(); //figure out whether on worker thread or not
static ThreadID tid();
}; };
} //namespace } //namespace

View file

@ -21,7 +21,7 @@
*/ */
#include <cstring> #include <cstring>
#include <memory>
#include "tvgStr.h" #include "tvgStr.h"
#include "tvgGifEncoder.h" #include "tvgGifEncoder.h"
#include "tvgGifSaver.h" #include "tvgGifSaver.h"