aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/workerpools.cpp
blob: 1bab39b2ac08cf47413700004931f229c5714d52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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 {

	struct WorkerPools
	{
		const int LargeWorkerThreadPoolTreadCount  = gsl::narrow<int>(Max(GetHardwareConcurrency() - 1u, 2u));
		const int MediumWorkerThreadPoolTreadCount = gsl::narrow<int>(Max((GetHardwareConcurrency() / 4u), 2u));
		const int SmallWorkerThreadPoolTreadCount  = gsl::narrow<int>(Max((GetHardwareConcurrency() / 8u), 1u));
		const int TinyWorkerThreadPoolTreadCount   = 1;

		bool IsShutDown = false;

		RwLock PoolLock;

		struct WorkerPool
		{
			std::unique_ptr<WorkerThreadPool> Pool;
			const int						  TreadCount;
			const std::string_view			  Name;
		};

		WorkerPool BurstLargeWorkerPool		 = {.TreadCount = LargeWorkerThreadPoolTreadCount, .Name = "large"};
		WorkerPool BackgroundLargeWorkerPool = {.TreadCount = LargeWorkerThreadPoolTreadCount, .Name = "large_bg"};

		WorkerPool BurstMediumWorkerPool	  = {.TreadCount = MediumWorkerThreadPoolTreadCount, .Name = "medium"};
		WorkerPool BackgroundMediumWorkerPool = {.TreadCount = MediumWorkerThreadPoolTreadCount, .Name = "medium_bg"};

		WorkerPool BurstSmallWorkerPool		 = {.TreadCount = SmallWorkerThreadPoolTreadCount, .Name = "small"};
		WorkerPool BackgroundSmallWorkerPool = {.TreadCount = SmallWorkerThreadPoolTreadCount, .Name = "small_bg"};

		WorkerPool BurstTinyWorkerPool		= {.TreadCount = TinyWorkerThreadPoolTreadCount, .Name = "tiny"};
		WorkerPool BackgroundTinyWorkerPool = {.TreadCount = TinyWorkerThreadPoolTreadCount, .Name = "tiny_bg"};

		WorkerPool SyncWorkerPool = {.TreadCount = 0, .Name = "synctp"};

		WorkerThreadPool& EnsurePoolPtr(WorkerPool& Pool)
		{
			{
				RwLock::SharedLockScope _(PoolLock);
				if (Pool.Pool)
				{
					return *Pool.Pool;
				}
			}
			RwLock::ExclusiveLockScope _(PoolLock);
			ZEN_ASSERT(!IsShutDown);
			if (!Pool.Pool)
			{
				Pool.Pool.reset(new WorkerThreadPool(Pool.TreadCount, Pool.Name));
			}
			return *Pool.Pool;
		}

		void Shutdown()
		{
			RwLock::ExclusiveLockScope _(PoolLock);
			IsShutDown = true;
			BurstLargeWorkerPool.Pool.reset();
			BackgroundLargeWorkerPool.Pool.reset();
			BurstMediumWorkerPool.Pool.reset();
			BackgroundMediumWorkerPool.Pool.reset();
			BurstSmallWorkerPool.Pool.reset();
			BackgroundSmallWorkerPool.Pool.reset();
			BurstTinyWorkerPool.Pool.reset();
			BackgroundTinyWorkerPool.Pool.reset();
			SyncWorkerPool.Pool.reset();
		}
	};

	WorkerPools& Pools()
	{
		static WorkerPools _;
		return _;
	}

}  // namespace

WorkerThreadPool&
GetLargeWorkerPool(EWorkloadType WorkloadType)
{
	auto& p = Pools();
	return p.EnsurePoolPtr(WorkloadType == EWorkloadType::Burst ? p.BurstLargeWorkerPool : p.BackgroundLargeWorkerPool);
}

WorkerThreadPool&
GetMediumWorkerPool(EWorkloadType WorkloadType)
{
	auto& p = Pools();
	return p.EnsurePoolPtr(WorkloadType == EWorkloadType::Burst ? p.BurstMediumWorkerPool : p.BackgroundMediumWorkerPool);
}

WorkerThreadPool&
GetSmallWorkerPool(EWorkloadType WorkloadType)
{
	auto& p = Pools();
	return p.EnsurePoolPtr(WorkloadType == EWorkloadType::Burst ? p.BurstSmallWorkerPool : p.BackgroundSmallWorkerPool);
}

WorkerThreadPool&
GetTinyWorkerPool(EWorkloadType WorkloadType)
{
	auto& p = Pools();
	return p.EnsurePoolPtr(WorkloadType == EWorkloadType::Burst ? p.BurstTinyWorkerPool : p.BackgroundTinyWorkerPool);
}

WorkerThreadPool&
GetSyncWorkerPool()
{
	auto& p = Pools();
	return p.EnsurePoolPtr(p.SyncWorkerPool);
}

void
ShutdownWorkerPools()
{
	Pools().Shutdown();
}
}  // namespace zen