| 65 | ThreadPoolService ThreadPoolServiceInstance; |
| 66 | |
| 67 | bool ThreadPoolService::Init() |
| 68 | { |
| 69 | PROFILE_MEM(EngineThreading); |
| 70 | |
| 71 | // Spawn threads |
| 72 | const CPUInfo cpuInfo = Platform::GetCPUInfo(); |
| 73 | const int32 count = Math::Clamp<int32>(cpuInfo.ProcessorCoreCount - 1, 2, PLATFORM_THREADS_LIMIT / 2); |
| 74 | LOG(Info, "Spawning {0} Thread Pool workers", count); |
| 75 | ThreadPoolImpl::Threads.Resize(count); |
| 76 | for (int32 i = 0; i < count; i++) |
| 77 | { |
| 78 | auto runnable = New<SimpleRunnable>(true); |
| 79 | runnable->OnWork.Bind(ThreadPool::ThreadProc); |
| 80 | auto thread = Thread::Create(runnable, String::Format(TEXT("Thread Pool {0}"), i)); |
| 81 | if (thread == nullptr) |
| 82 | { |
| 83 | LOG(Error, "Failed to spawn {0} thread in the Thread Pool", i + 1); |
| 84 | return true; |
| 85 | } |
| 86 | ThreadPoolImpl::Threads[i] = thread; |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | void ThreadPoolService::BeforeExit() |
| 93 | { |