Skip to content

Commit c39e2a8

Browse files
author
Mateusz Kopciński
committed
adressed review comments
1 parent 1d89f5f commit c39e2a8

File tree

6 files changed

+186
-194
lines changed

6 files changed

+186
-194
lines changed

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "RnExecutorchInstaller.h"
22

3-
#include <rnexecutorch/Log.h>
43
#include <rnexecutorch/TokenizerModule.h>
54
#include <rnexecutorch/host_objects/JsiConversions.h>
65
#include <rnexecutorch/models/classification/Classification.h>
@@ -15,7 +14,7 @@
1514
#include <rnexecutorch/models/style_transfer/StyleTransfer.h>
1615
#include <rnexecutorch/models/vertical_ocr/VerticalOCR.h>
1716
#include <rnexecutorch/threads/GlobalThreadPool.h>
18-
#include <rnexecutorch/threads/ThreadUtils.h>
17+
#include <rnexecutorch/threads/utils/ThreadUtils.h>
1918

2019
namespace rnexecutorch {
2120

@@ -94,7 +93,7 @@ void RnExecutorchInstaller::injectJSIBindings(
9493
RnExecutorchInstaller::loadModel<models::speech_to_text::SpeechToText>(
9594
jsiRuntime, jsCallInvoker, "loadSpeechToText"));
9695

97-
threads::ThreadUtils::unsafeSetupThreadPool();
96+
threads::utils::unsafeSetupThreadPool();
9897
threads::GlobalThreadPool::initialize();
9998
}
10099

packages/react-native-executorch/common/rnexecutorch/threads/GlobalThreadPool.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,42 @@
44
#include <executorch/extension/threadpool/cpuinfo_utils.h>
55
#include <memory>
66
#include <mutex>
7+
#include <optional>
78
#include <rnexecutorch/Log.h>
89
#include <rnexecutorch/threads/HighPerformanceThreadPool.h>
910

10-
namespace rnexecutorch {
11-
namespace threads {
11+
namespace rnexecutorch::threads {
1212

1313
class GlobalThreadPool {
14-
private:
15-
inline static std::unique_ptr<HighPerformanceThreadPool> instance;
16-
inline static std::once_flag initFlag;
17-
14+
public:
1815
GlobalThreadPool() = delete;
16+
GlobalThreadPool(const GlobalThreadPool &) = delete;
17+
GlobalThreadPool &operator=(const GlobalThreadPool &) = delete;
18+
GlobalThreadPool(GlobalThreadPool &&) = delete;
19+
GlobalThreadPool &operator=(GlobalThreadPool &&) = delete;
1920

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 = {}) {
2230
std::call_once(initFlag, [&numThreads, config]() {
23-
// Auto-detect optimal thread count if not specified
24-
if (numThreads == 0) {
31+
if (!numThreads) {
2532
numThreads =
2633
::executorch::extension::cpuinfo::get_num_performant_cores();
2734
}
2835

2936
log(rnexecutorch::LOG_LEVEL::Info, "Initializing global thread pool with",
3037
numThreads, "threads");
31-
instance =
32-
std::make_unique<HighPerformanceThreadPool>(numThreads, config);
38+
instance = std::make_unique<HighPerformanceThreadPool>(numThreads.value(),
39+
config);
3340
});
3441
}
3542

36-
// Get the global thread pool instance
37-
static HighPerformanceThreadPool &get() {
38-
if (!instance) {
39-
initialize();
40-
}
41-
return *instance;
42-
}
43-
4443
// Convenience methods that mirror std::thread interface
4544
template <typename Func, typename... Args>
4645
static auto async(Func &&func, Args &&...args) {
@@ -71,7 +70,10 @@ class GlobalThreadPool {
7170
instance.reset();
7271
}
7372
}
73+
74+
private:
75+
inline static std::unique_ptr<HighPerformanceThreadPool> instance;
76+
inline static std::once_flag initFlag;
7477
};
7578

76-
} // namespace threads
77-
} // namespace rnexecutorch
79+
} // namespace rnexecutorch::threads

0 commit comments

Comments
 (0)