fix crash for static ThreadPool
All checks were successful
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m57s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 2m20s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 2m35s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 2m48s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 3m41s
linux-arm-gcc / linux-gcc-armhf (push) Successful in 6m57s

This commit is contained in:
tqcq 2024-04-24 15:36:09 +08:00
parent 85852ee2e9
commit 0a2c5e3ca7

View File

@ -14,23 +14,30 @@ DecrementFuturesUsage()
}// namespace future_detail }// namespace future_detail
static ThreadPool g_default_thread_pool; static std::atomic<TaskQueueBase *> g_default_scheduler{nullptr};
TaskQueueBase *g_default_scheduler = &g_default_thread_pool; // static ThreadPool default_thread_pool;
static std::unique_ptr<Thread> g_default_thread;
void void
SetDefaultScheduler(TaskQueueBase *scheduler) noexcept SetDefaultScheduler(TaskQueueBase *scheduler) noexcept
{ {
if (scheduler == nullptr) { SLED_ASSERT(scheduler, "scheduler is nullptr");
g_default_scheduler = &g_default_thread_pool; g_default_scheduler.store(scheduler, std::memory_order_release);
} else {
g_default_scheduler = scheduler;
}
} }
TaskQueueBase * TaskQueueBase *
GetDefaultScheduler() noexcept GetDefaultScheduler() noexcept
{ {
return g_default_scheduler; static std::once_flag flag;
std::call_once(flag, [&] {
g_default_thread = sled::Thread::Create();
g_default_thread->Start();
TaskQueueBase *null_scheduler = nullptr;
while (g_default_scheduler.load() == nullptr) {
g_default_scheduler.compare_exchange_weak(null_scheduler, g_default_thread.get());
}
});
return g_default_scheduler.load(std::memory_order_acquire);
} }
}// namespace sled }// namespace sled