1 #ifndef DALI_THREAD_POOL_H
2 #define DALI_THREAD_POOL_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/dali-common.h>
26 #include <condition_variable>
37 using Task = std::function<void(uint32_t)>;
39 using TaskQueue = std::queue<Task>;
42 * Future contains the result of submitted task. When queried
43 * it applies internal synchronization mechanism to make sure
44 * the value is available.
47 class DALI_INTERNAL Future final
49 friend class ThreadPool;
53 * @brief Constructor of Future
57 mFuture = mPromise.get_future();
61 * @brief Destructor of Future
69 * @brief Returns value of future, blocks if needed.
70 * @return Value stored by the future
78 * @brief Waits until the value of future is ready. This function
79 * is a fencing mechanism.
90 * @brief Tests whether the future is valid
91 * @return True if valid, False otherwise
95 return mFuture.valid();
99 * @brief Resets the future bringing it to the initial state.
100 * It's required in order to reuse the same Future object.
104 mPromise = std::promise<T>();
105 mFuture = mPromise.get_future();
109 std::promise<T> mPromise{};
110 std::future<T> mFuture{};
113 using SharedFuture = std::shared_ptr<Future<void>>;
116 * FutureGroup binds many Future objects and applies synchronization.
119 class FutureGroup final
121 friend class ThreadPool;
125 * Waits for all the Futures to complete.
129 for(auto& future : mFutures)
136 std::vector<std::shared_ptr<Future<T>>> mFutures;
139 using UniqueFutureGroup = std::unique_ptr<FutureGroup<void>>;
142 * ThreadPool creates and manages worker threads and tasks submitted for execution.
144 class DALI_CORE_API ThreadPool final
148 * @brief Constructor of thread pool.
153 * @brief Destructor of thread pool.
158 * @brief Intializes thread pool
159 * @param threadCount Number of worker threads to use. If 0 then thread count equals hardware thread count.
160 * @return True if success
162 bool Initialize(uint32_t threadCount = 0u);
165 * @brief Waits until all threads finish execution and go back to the idle state.
170 * @brief Submits a single task to specified ( by the index ) worker thread.
171 * @param workerIndex Index of thread to be used
172 * @param task Task submitted for execution
173 * @return Shared pointer to the Future object
175 SharedFuture SubmitTask(uint32_t workerIndex, const Task& task);
178 * @brief Submits vector of tasks to the pool
179 * @param tasks Vector containing tasks to be executed
180 * @return Shared pointer to the Future object
182 SharedFuture SubmitTasks(const std::vector<Task>& tasks);
185 * @brief Submits tasks to threads specified by thread mask.
186 * @param tasks Vector of tasks
187 * @param threadMask Mask of threads to be used or 0 to use all available threads
188 * @return Unique pointer to the FutureGroup object
190 UniqueFutureGroup SubmitTasks(const std::vector<Task>& tasks, uint32_t threadMask);
193 * @brief Returns number of worker threads
194 * @return Number of worker threads
196 size_t GetWorkerCount() const;
200 std::unique_ptr<Impl> mImpl;
205 #endif // DALI_THREAD_POOL_H