-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPoolImpl.h
148 lines (127 loc) · 4.18 KB
/
ThreadPoolImpl.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//-------------------------------------------------------------------------------
// File: ThreadPoolImpl.h
//
// Copyright (c) NVIDIA Corporation. All rights reserved.
//-------------------------------------------------------------------------------
#pragma once
#include "ThreadPool.h"
#include <array>
#include <atomic>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
//--------------------------------------------------------------------------------------
// ThreadPool
//--------------------------------------------------------------------------------------
class ThreadPool
{
public:
ThreadPool()
: m_shutdown(false)
, m_vecWorkers()
{
}
~ThreadPool()
{
m_shutdown = true;
for (auto& pWorker : m_vecWorkers) {
if (!pWorker) {
continue;
}
pWorker->cv.notify_one();
if (pWorker->thread.joinable()) {
pWorker->thread.join();
}
}
}
void RunOnThread(uint32_t i, NvExecuteOnThreadFlags flags, std::function<void()>&& fn)
{
// JIT create worker
if (i >= m_vecWorkers.size()) {
m_vecWorkers.resize(i + 1);
}
if (!m_vecWorkers[i]) {
m_vecWorkers[i] = createWorker();
}
auto& worker = *m_vecWorkers[i];
// Finish the previous work for this thread, if needed
while (!worker.m_workComplete.load()) {
}
// Supply work
{
std::unique_lock<std::mutex> lock(worker.m_mutex);
std::swap(worker.m_work, fn);
worker.m_workComplete = false;
}
// Notify worker
m_vecWorkers[i]->cv.notify_one();
if (!(flags & NvExecuteOnThreadFlags::NV_EXECUTE_ON_THREAD_FLAGS_NON_BLOCKING)) {
// Spin while we wait for it to finish
while (!worker.m_workComplete.load()) {
}
}
}
private:
struct PerWorker
{
PerWorker()
: thread()
, cv()
, m_work()
, m_mutex()
, m_workComplete(true)
{
}
std::thread thread;
std::condition_variable cv;
std::function<void()> m_work;
std::mutex m_mutex;
std::atomic<bool> m_workComplete;
};
std::unique_ptr<PerWorker> createWorker()
{
PerWorker* pWorker = new PerWorker;
pWorker->thread = std::thread([=] {
// Loop
while (!m_shutdown) {
// Wait for work
std::unique_lock<std::mutex> lock(pWorker->m_mutex);
pWorker->cv.wait(lock, [=] {
return pWorker->m_work || m_shutdown;
});
// Bail if told to shutdown
if (m_shutdown) {
return;
}
// Consume work and notify main thread when done
pWorker->m_work();
pWorker->m_work = nullptr;
pWorker->m_workComplete = true;
}
});
return std::unique_ptr<PerWorker>(pWorker);
}
bool m_shutdown;
std::vector<std::unique_ptr<PerWorker>> m_vecWorkers;
};
//--------------------------------------------------------------------------------------
// NvHasMultithreadedReplay
//--------------------------------------------------------------------------------------
bool NvHasMultithreadedReplay();
//--------------------------------------------------------------------------------------
// NvExecuteOnThread
//--------------------------------------------------------------------------------------
void NvExecuteOnThread(uint32_t threadId, NvExecuteOnThreadFlags flags, std::function<void()>&& fn)
{
// Run on the master thread thread if multi-threaded replay is disabled
if (!NvHasMultithreadedReplay()) {
fn();
return;
}
// Otherwise pass on to thread pool
static ThreadPool s_pool;
s_pool.RunOnThread(threadId, flags, std::move(fn));
}