| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <atomic> |
| 7 |
#include <expected> |
| 8 |
#include <functional> |
| 9 |
#include <memory> |
| 10 |
#include <thread> |
| 11 |
#include <utility> |
| 12 |
|
| 13 |
#include "Common/Assert.h" |
| 14 |
#include "Common/Event.h" |
| 15 |
|
| 16 |
namespace DiscIO |
| 17 |
{ |
| 18 |
enum class ConversionResultCode |
| 19 |
{ |
| 20 |
Success, |
| 21 |
Canceled, |
| 22 |
ReadFailed, |
| 23 |
WriteFailed, |
| 24 |
InternalError, |
| 25 |
}; |
| 26 |
|
| 27 |
template <typename T> |
| 28 |
using ConversionResult = std::expected<T, ConversionResultCode>; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
template <typename CompressThreadState, typename CompressParameters, typename OutputParameters> |
| 39 |
class MultithreadedCompressor |
| 40 |
{ |
| 41 |
public: |
| 42 |
MultithreadedCompressor( |
| 43 |
std::function<ConversionResultCode(CompressThreadState*)> set_up_compress_thread_state, |
| 44 |
std::function<ConversionResult<OutputParameters>(CompressThreadState*, CompressParameters)> |
| 45 |
compress, |
| 46 |
std::function<ConversionResultCode(OutputParameters)> output) |
| 47 |
: m_set_up_compress_thread_state(std::move(set_up_compress_thread_state)), |
| 48 |
m_compress(std::move(compress)), m_output(std::move(output)), |
| 49 |
m_threads(std::max<unsigned int>(1, std::thread::hardware_concurrency())) |
| 50 |
{ |
| 51 |
m_compress_threads = std::make_unique<CompressThread[]>(m_threads); |
| 52 |
|
| 53 |
for (size_t i = 0; i < m_threads; ++i) |
| 54 |
{ |
| 55 |
m_compress_threads[i].thread = |
| 56 |
std::thread(std::mem_fn(&MultithreadedCompressor::CompressThreadFunction), this, |
| 57 |
&m_compress_threads[i]); |
| 58 |
} |
| 59 |
|
| 60 |
m_output_thread = |
| 61 |
std::thread(std::mem_fn(&MultithreadedCompressor::OutputThreadFunction), this); |
| 62 |
} |
| 63 |
|
| 64 |
~MultithreadedCompressor() |
| 65 |
{ |
| 66 |
if (!m_shutting_down.load()) |
| 67 |
Shutdown(); |
| 68 |
} |
| 69 |
|
| 70 |
void CompressAndWrite(CompressParameters parameters) |
| 71 |
{ |
| 72 |
if (GetStatus() != ConversionResultCode::Success) |
| 73 |
return; |
| 74 |
|
| 75 |
CompressThread& compress_thread = m_compress_threads[m_current_index]; |
| 76 |
|
| 77 |
compress_thread.compress_ready_event.Wait(); |
| 78 |
compress_thread.compress_parameters = std::move(parameters); |
| 79 |
compress_thread.compress_event.Set(); |
| 80 |
|
| 81 |
++m_current_index; |
| 82 |
if (m_current_index >= m_threads) |
| 83 |
m_current_index -= m_threads; |
| 84 |
} |
| 85 |
|
| 86 |
void SetError(ConversionResultCode result) |
| 87 |
{ |
| 88 |
ASSERT(result != ConversionResultCode::Success); |
| 89 |
|
| 90 |
|
| 91 |
ConversionResultCode expected = ConversionResultCode::Success; |
| 92 |
m_result.compare_exchange_strong(expected, result); |
| 93 |
} |
| 94 |
|
| 95 |
ConversionResultCode GetStatus() const { return m_result.load(); } |
| 96 |
|
| 97 |
void Shutdown() |
| 98 |
{ |
| 99 |
for (size_t i = 0; i < m_threads; ++i) |
| 100 |
m_compress_threads[i].compress_ready_event.Wait(); |
| 101 |
for (size_t i = 0; i < m_threads; ++i) |
| 102 |
m_compress_threads[i].compress_done_event.Wait(); |
| 103 |
for (size_t i = 0; i < m_threads; ++i) |
| 104 |
m_compress_threads[i].output_ready_event.Wait(); |
| 105 |
|
| 106 |
m_shutting_down.store(true); |
| 107 |
|
| 108 |
for (size_t i = 0; i < m_threads; ++i) |
| 109 |
m_compress_threads[i].compress_event.Set(); |
| 110 |
for (size_t i = 0; i < m_threads; ++i) |
| 111 |
m_compress_threads[i].output_event.Set(); |
| 112 |
|
| 113 |
for (size_t i = 0; i < m_threads; ++i) |
| 114 |
m_compress_threads[i].thread.join(); |
| 115 |
|
| 116 |
m_output_thread.join(); |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
struct CompressThread |
| 121 |
{ |
| 122 |
std::thread thread; |
| 123 |
|
| 124 |
Common::Event compress_ready_event; |
| 125 |
Common::Event compress_event; |
| 126 |
Common::Event compress_done_event; |
| 127 |
Common::Event output_ready_event; |
| 128 |
Common::Event output_event; |
| 129 |
|
| 130 |
CompressParameters compress_parameters; |
| 131 |
OutputParameters output_parameters; |
| 132 |
}; |
| 133 |
|
| 134 |
void CompressThreadFunction(CompressThread* state) |
| 135 |
{ |
| 136 |
CompressThreadState compress_thread_state; |
| 137 |
|
| 138 |
ConversionResultCode setup_result = m_set_up_compress_thread_state(&compress_thread_state); |
| 139 |
if (setup_result != ConversionResultCode::Success) |
| 140 |
SetError(setup_result); |
| 141 |
|
| 142 |
state->compress_ready_event.Set(); |
| 143 |
state->compress_done_event.Set(); |
| 144 |
|
| 145 |
while (true) |
| 146 |
{ |
| 147 |
state->compress_event.Wait(); |
| 148 |
|
| 149 |
if (m_shutting_down.load()) |
| 150 |
return; |
| 151 |
|
| 152 |
CompressParameters parameters = std::move(state->compress_parameters); |
| 153 |
|
| 154 |
state->compress_done_event.Reset(); |
| 155 |
state->compress_ready_event.Set(); |
| 156 |
|
| 157 |
ConversionResult<OutputParameters> result = |
| 158 |
m_compress(&compress_thread_state, std::move(parameters)); |
| 159 |
|
| 160 |
if (result) |
| 161 |
{ |
| 162 |
state->output_ready_event.Wait(); |
| 163 |
state->output_parameters = std::move(*result); |
| 164 |
state->output_event.Set(); |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
SetError(result.error()); |
| 169 |
} |
| 170 |
|
| 171 |
state->compress_done_event.Set(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void OutputThreadFunction() |
| 176 |
{ |
| 177 |
for (size_t i = 0; i < m_threads; ++i) |
| 178 |
m_compress_threads[i].output_ready_event.Set(); |
| 179 |
|
| 180 |
size_t index = 0; |
| 181 |
|
| 182 |
while (true) |
| 183 |
{ |
| 184 |
CompressThread& compress_thread = m_compress_threads[index]; |
| 185 |
|
| 186 |
compress_thread.output_event.Wait(); |
| 187 |
|
| 188 |
if (m_shutting_down.load()) |
| 189 |
return; |
| 190 |
|
| 191 |
OutputParameters parameters = std::move(compress_thread.output_parameters); |
| 192 |
|
| 193 |
compress_thread.output_ready_event.Set(); |
| 194 |
|
| 195 |
const ConversionResultCode result = m_output(std::move(parameters)); |
| 196 |
|
| 197 |
if (result != ConversionResultCode::Success) |
| 198 |
SetError(result); |
| 199 |
|
| 200 |
++index; |
| 201 |
if (index >= m_threads) |
| 202 |
index -= m_threads; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
std::function<ConversionResultCode(CompressThreadState*)> m_set_up_compress_thread_state; |
| 207 |
std::function<ConversionResult<OutputParameters>(CompressThreadState*, CompressParameters)> |
| 208 |
m_compress; |
| 209 |
std::function<ConversionResultCode(OutputParameters)> m_output; |
| 210 |
|
| 211 |
|
| 212 |
std::unique_ptr<CompressThread[]> m_compress_threads; |
| 213 |
std::thread m_output_thread; |
| 214 |
|
| 215 |
const size_t m_threads; |
| 216 |
size_t m_current_index = 0; |
| 217 |
|
| 218 |
std::atomic<ConversionResultCode> m_result = ConversionResultCode::Success; |
| 219 |
std::atomic<bool> m_shutting_down = false; |
| 220 |
}; |
| 221 |
|
| 222 |
} |