aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/workerpools.cpp
blob: 7970349785c1c1dfa9e7f6cf2749a6fd2814d76b (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
// 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>(Max(std::thread::hardware_concurrency() - 1u, 2u));
	const int MediumWorkerThreadPoolTreadCount = gsl::narrow<int>(Max((std::thread::hardware_concurrency() / 4u), 2u));
	const int SmallWorkerThreadPoolTreadCount  = gsl::narrow<int>(Max((std::thread::hardware_concurrency() / 8u), 1u));
	const int TinyWorkerThreadPoolTreadCount   = 1;

	static 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 = "LargeThreadPool(burst)"};
	WorkerPool BackgroundLargeWorkerPool = {.TreadCount = LargeWorkerThreadPoolTreadCount, .Name = "LargeThreadPool(bkg)"};

	WorkerPool BurstMediumWorkerPool	  = {.TreadCount = MediumWorkerThreadPoolTreadCount, .Name = "MediumThreadPool(burst)"};
	WorkerPool BackgroundMediumWorkerPool = {.TreadCount = MediumWorkerThreadPoolTreadCount, .Name = "MediumThreadPool(bkg)"};

	WorkerPool BurstSmallWorkerPool		 = {.TreadCount = SmallWorkerThreadPoolTreadCount, .Name = "SmallThreadPool(burst)"};
	WorkerPool BackgroundSmallWorkerPool = {.TreadCount = SmallWorkerThreadPoolTreadCount, .Name = "SmallThreadPool(bkg)"};

	WorkerPool BurstTinyWorkerPool		= {.TreadCount = TinyWorkerThreadPoolTreadCount, .Name = "TinyThreadPool(burst)"};
	WorkerPool BackgroundTinyWorkerPool = {.TreadCount = TinyWorkerThreadPoolTreadCount, .Name = "TinyThreadPool(bkg)"};

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

	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;
	}
}  // namespace

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

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

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

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

WorkerThreadPool&
GetSyncWorkerPool()
{
	return EnsurePoolPtr(SyncWorkerPool);
}

void
ShutdownWorkerPools()
{
	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();
}
}  // namespace zen