Skip to content

Commit 77c2bb0

Browse files
committed
Add ThreadedExecutionEngine that combines a thread pool reference and a job object, reexposing the legacy thread pool interface.
Signed-off-by: Michal Zientkiewicz <michalz@nvidia.com>
1 parent b13a253 commit 77c2bb0

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

include/dali/core/exec/engine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
1+
// Copyright (c) 2021-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ class SequentialExecutionEngine {
4646
* @brief Immediately execute a callable object `f` with thread index 0.
4747
*/
4848
template <typename FunctionLike>
49-
void AddWork(FunctionLike &&f, int64_t priority = 0, bool start_immediately = true) {
49+
void AddWork(FunctionLike &&f, int64_t priority = 0) {
5050
const int idx = 0; // use of 0 literal would successfully call f expecting a pointer
5151
f(idx);
5252
}

include/dali/core/exec/thread_pool_base.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,38 @@ class DLL_PUBLIC ThreadPoolBase {
161161
std::vector<std::thread> threads_;
162162
};
163163

164+
165+
template <typename ThreadPool>
166+
class ThreadedExecutionengine {
167+
public:
168+
ThreadedExecutionengine(ThreadPool &tp) : tp_(tp) {} // NOLINT
169+
170+
/**
171+
* @brief Immediately execute a callable object `f` with thread index 0.
172+
*/
173+
template <typename FunctionLike>
174+
void AddWork(FunctionLike &&f, int64_t priority = 0) {
175+
job_.AddTask(std::forward<FunctionLike>(f), priority);
176+
}
177+
178+
void RunAll() {
179+
job_.Run(tp_, true);
180+
}
181+
182+
int NumThreads() const noexcept {
183+
return tp_.NumThreads();
184+
}
185+
186+
ThreadPool &GetThreadPool() const noexcept {
187+
return tp_;
188+
}
189+
190+
private:
191+
ThreadPool &tp_;
192+
Job job_;
193+
};
194+
195+
164196
} // namespace dali
165197

166198
#endif // DALI_CORE_EXEC_THREAD_POOL_BASE_H_

0 commit comments

Comments
 (0)