|
4 | 4 | #include <executorch/extension/threadpool/cpuinfo_utils.h> |
5 | 5 | #include <memory> |
6 | 6 | #include <mutex> |
| 7 | +#include <optional> |
7 | 8 | #include <rnexecutorch/Log.h> |
8 | 9 | #include <rnexecutorch/threads/HighPerformanceThreadPool.h> |
9 | 10 |
|
10 | | -namespace rnexecutorch { |
11 | | -namespace threads { |
| 11 | +namespace rnexecutorch::threads { |
12 | 12 |
|
13 | 13 | class GlobalThreadPool { |
14 | | -private: |
15 | | - inline static std::unique_ptr<HighPerformanceThreadPool> instance; |
16 | | - inline static std::once_flag initFlag; |
17 | | - |
| 14 | +public: |
18 | 15 | GlobalThreadPool() = delete; |
| 16 | + GlobalThreadPool(const GlobalThreadPool &) = delete; |
| 17 | + GlobalThreadPool &operator=(const GlobalThreadPool &) = delete; |
| 18 | + GlobalThreadPool(GlobalThreadPool &&) = delete; |
| 19 | + GlobalThreadPool &operator=(GlobalThreadPool &&) = delete; |
19 | 20 |
|
20 | | -public: |
21 | | - static void initialize(uint32_t numThreads = 0, ThreadConfig config = {}) { |
| 21 | + static HighPerformanceThreadPool &get() { |
| 22 | + if (!instance) { |
| 23 | + initialize(); |
| 24 | + } |
| 25 | + return *instance; |
| 26 | + } |
| 27 | + |
| 28 | + static void initialize(std::optional<uint32_t> numThreads = std::nullopt, |
| 29 | + ThreadConfig config = {}) { |
22 | 30 | std::call_once(initFlag, [&numThreads, config]() { |
23 | | - // Auto-detect optimal thread count if not specified |
24 | | - if (numThreads == 0) { |
| 31 | + if (!numThreads) { |
25 | 32 | numThreads = |
26 | 33 | ::executorch::extension::cpuinfo::get_num_performant_cores(); |
27 | 34 | } |
28 | 35 |
|
29 | 36 | log(rnexecutorch::LOG_LEVEL::Info, "Initializing global thread pool with", |
30 | 37 | numThreads, "threads"); |
31 | | - instance = |
32 | | - std::make_unique<HighPerformanceThreadPool>(numThreads, config); |
| 38 | + instance = std::make_unique<HighPerformanceThreadPool>(numThreads.value(), |
| 39 | + config); |
33 | 40 | }); |
34 | 41 | } |
35 | 42 |
|
36 | | - // Get the global thread pool instance |
37 | | - static HighPerformanceThreadPool &get() { |
38 | | - if (!instance) { |
39 | | - initialize(); |
40 | | - } |
41 | | - return *instance; |
42 | | - } |
43 | | - |
44 | 43 | // Convenience methods that mirror std::thread interface |
45 | 44 | template <typename Func, typename... Args> |
46 | 45 | static auto async(Func &&func, Args &&...args) { |
@@ -71,7 +70,10 @@ class GlobalThreadPool { |
71 | 70 | instance.reset(); |
72 | 71 | } |
73 | 72 | } |
| 73 | + |
| 74 | +private: |
| 75 | + inline static std::unique_ptr<HighPerformanceThreadPool> instance; |
| 76 | + inline static std::once_flag initFlag; |
74 | 77 | }; |
75 | 78 |
|
76 | | -} // namespace threads |
77 | | -} // namespace rnexecutorch |
| 79 | +} // namespace rnexecutorch::threads |
0 commit comments