diff options
| author | Dan Engelbrecht <[email protected]> | 2023-11-29 09:14:57 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-29 15:14:57 +0100 |
| commit | 68b3382ef7e0f7795b9a601aae73adc2f8ef9873 (patch) | |
| tree | a8460fa497195575505e14f7beef069ecee31ef7 /src/zenutil/workerpools.cpp | |
| parent | fixed file logger pattern (#579) (diff) | |
| download | zen-68b3382ef7e0f7795b9a601aae73adc2f8ef9873.tar.xz zen-68b3382ef7e0f7795b9a601aae73adc2f8ef9873.zip | |
global thread worker pools (#577)
- Improvement: Use two global worker thread pools instead of ad-hoc creation of worker pools
Diffstat (limited to 'src/zenutil/workerpools.cpp')
| -rw-r--r-- | src/zenutil/workerpools.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/zenutil/workerpools.cpp b/src/zenutil/workerpools.cpp new file mode 100644 index 000000000..b511b0c5c --- /dev/null +++ b/src/zenutil/workerpools.cpp @@ -0,0 +1,89 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "zenutil/workerpools.h" + +#include <zencore/intmath.h> +#include <zencore/thread.h> + +ZEN_THIRD_PARTY_INCLUDES_START +#include <gsl/gsl-lite.hpp> +ZEN_THIRD_PARTY_INCLUDES_END + +namespace zen { +namespace { + const int LargeWorkerThreadPoolTreadCount = gsl::narrow<int>(std::thread::hardware_concurrency()); + const int SmallWorkerThreadPoolTreadCount = gsl::narrow<int>(Max((std::thread::hardware_concurrency() / 4u), 1u)); + + RwLock PoolLock; + + std::unique_ptr<WorkerThreadPool> LargeWorkerPool; + std::unique_ptr<WorkerThreadPool> SmallWorkerPool; + std::unique_ptr<WorkerThreadPool> SyncWorkerPool; +} // namespace + +WorkerThreadPool& +GetLargeWorkerPool() +{ + { + RwLock::SharedLockScope _(PoolLock); + if (LargeWorkerPool) + { + return *LargeWorkerPool; + } + } + RwLock::ExclusiveLockScope _(PoolLock); + if (LargeWorkerPool) + { + return *LargeWorkerPool; + } + LargeWorkerPool.reset(new WorkerThreadPool(LargeWorkerThreadPoolTreadCount, "LargeThreadPool")); + return *LargeWorkerPool; +} + +WorkerThreadPool& +GetSmallWorkerPool() +{ + { + RwLock::SharedLockScope _(PoolLock); + if (SmallWorkerPool) + { + return *SmallWorkerPool; + } + } + RwLock::ExclusiveLockScope _(PoolLock); + if (SmallWorkerPool) + { + return *SmallWorkerPool; + } + SmallWorkerPool.reset(new WorkerThreadPool(SmallWorkerThreadPoolTreadCount, "SmallThreadPool")); + return *SmallWorkerPool; +} + +WorkerThreadPool& +GetSyncWorkerPool() +{ + { + RwLock::SharedLockScope _(PoolLock); + if (SyncWorkerPool) + { + return *SyncWorkerPool; + } + } + RwLock::ExclusiveLockScope _(PoolLock); + if (SyncWorkerPool) + { + return *SyncWorkerPool; + } + SyncWorkerPool.reset(new WorkerThreadPool(0, "SyncThreadPool")); + return *SyncWorkerPool; +} + +void +ShutdownWorkerPools() +{ + RwLock::ExclusiveLockScope _(PoolLock); + LargeWorkerPool.reset(); + SmallWorkerPool.reset(); + SyncWorkerPool.reset(); +} +} // namespace zen |