// Copyright Epic Games, Inc. All Rights Reserved. #include "zenutil/workerpools.h" #include #include ZEN_THIRD_PARTY_INCLUDES_START #include ZEN_THIRD_PARTY_INCLUDES_END namespace zen { namespace { const int LargeWorkerThreadPoolTreadCount = gsl::narrow(std::thread::hardware_concurrency()); const int SmallWorkerThreadPoolTreadCount = gsl::narrow(Max((std::thread::hardware_concurrency() / 4u), 1u)); static bool IsShutDown = false; RwLock PoolLock; std::unique_ptr LargeWorkerPool; std::unique_ptr SmallWorkerPool; std::unique_ptr SyncWorkerPool; } // namespace WorkerThreadPool& GetLargeWorkerPool() { { RwLock::SharedLockScope _(PoolLock); if (LargeWorkerPool) { return *LargeWorkerPool; } } RwLock::ExclusiveLockScope _(PoolLock); ZEN_ASSERT(!IsShutDown); if (LargeWorkerPool) { return *LargeWorkerPool; } LargeWorkerPool.reset(new WorkerThreadPool(LargeWorkerThreadPoolTreadCount, "LargeThreadPool")); return *LargeWorkerPool; } WorkerThreadPool& GetSmallWorkerPool() { { RwLock::SharedLockScope _(PoolLock); if (SmallWorkerPool) { return *SmallWorkerPool; } } RwLock::ExclusiveLockScope _(PoolLock); ZEN_ASSERT(!IsShutDown); if (SmallWorkerPool) { return *SmallWorkerPool; } SmallWorkerPool.reset(new WorkerThreadPool(SmallWorkerThreadPoolTreadCount, "SmallThreadPool")); return *SmallWorkerPool; } WorkerThreadPool& GetSyncWorkerPool() { { RwLock::SharedLockScope _(PoolLock); if (SyncWorkerPool) { return *SyncWorkerPool; } } RwLock::ExclusiveLockScope _(PoolLock); ZEN_ASSERT(!IsShutDown); if (SyncWorkerPool) { return *SyncWorkerPool; } SyncWorkerPool.reset(new WorkerThreadPool(0, "SyncThreadPool")); return *SyncWorkerPool; } void ShutdownWorkerPools() { RwLock::ExclusiveLockScope _(PoolLock); IsShutDown = true; LargeWorkerPool.reset(); SmallWorkerPool.reset(); SyncWorkerPool.reset(); } } // namespace zen