aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/workerpools.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-12-11 13:09:03 +0100
committerStefan Boberg <[email protected]>2023-12-11 13:09:03 +0100
commit93afeddbc7a5b5df390a29407f5515acd5a70fc1 (patch)
tree6f85ee551aabe20dece64a750c0b2d5d2c5d2d5d /src/zenutil/workerpools.cpp
parentremoved unnecessary SHA1 references (diff)
parentMake sure that PathFromHandle don't hide true error when throwing exceptions ... (diff)
downloadzen-93afeddbc7a5b5df390a29407f5515acd5a70fc1.tar.xz
zen-93afeddbc7a5b5df390a29407f5515acd5a70fc1.zip
Merge branch 'main' of https://github.com/EpicGames/zen
Diffstat (limited to 'src/zenutil/workerpools.cpp')
-rw-r--r--src/zenutil/workerpools.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/zenutil/workerpools.cpp b/src/zenutil/workerpools.cpp
new file mode 100644
index 000000000..3ae302064
--- /dev/null
+++ b/src/zenutil/workerpools.cpp
@@ -0,0 +1,95 @@
+// 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));
+
+ static bool IsShutDown = false;
+
+ 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);
+ 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