1 #ifndef DALI_THREAD_POOL_H
2 #define DALI_THREAD_POOL_H
5 * Copyright (c) 2019 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>
29 #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;
54 * @brief Constructor of Future
58 mFuture = mPromise.get_future();
62 * @brief Destructor of Future
70 * @brief Returns value of future, blocks if needed.
71 * @return Value stored by the future
79 * @brief Waits until the value of future is ready. This function
80 * is a fencing mechanism.
91 * @brief Tests whether the future is valid
92 * @return True if valid, False otherwise
96 return mFuture.valid();
100 * @brief Resets the future bringing it to the initial state.
101 * It's required in order to reuse the same Future object.
105 mPromise = std::promise<T>();
106 mFuture = mPromise.get_future();
111 std::promise<T> mPromise{};
112 std::future<T> mFuture{};
115 using SharedFuture = std::shared_ptr<Future<void>>;
118 * FutureGroup binds many Future objects and applies synchronization.
121 class FutureGroup final
123 friend class ThreadPool;
128 * Waits for all the Futures to complete.
132 for (auto &future : mFutures)
140 std::vector<std::shared_ptr<Future<T> > > mFutures;
143 using UniqueFutureGroup = std::unique_ptr<FutureGroup<void>>;
148 * ThreadPool creates and manages worker threads and tasks submitted for execution.
150 class DALI_CORE_API ThreadPool final
155 * @brief Constructor of thread pool.
160 * @brief Destructor of thread pool.
165 * @brief Intializes thread pool
166 * @param threadCount Number of worker threads to use. If 0 then thread count equals hardware thread count.
167 * @return True if success
169 bool Initialize( uint32_t threadCount = 0u );
172 * @brief Waits until all threads finish execution and go back to the idle state.
177 * @brief Submits a single task to specified ( by the index ) worker thread.
178 * @param workerIndex Index of thread to be used
179 * @param task Task submitted for execution
180 * @return Shared pointer to the Future object
182 SharedFuture SubmitTask(uint32_t workerIndex, const Task &task);
185 * @brief Submits vector of tasks to the pool
186 * @param tasks Vector containing tasks to be executed
187 * @return Shared pointer to the Future object
189 SharedFuture SubmitTasks(const std::vector<Task>& tasks);
192 * @brief Submits tasks to threads specified by thread mask.
193 * @param tasks Vector of tasks
194 * @param threadMask Mask of threads to be used or 0 to use all available threads
195 * @return Unique pointer to the FutureGroup object
197 UniqueFutureGroup SubmitTasks(const std::vector<Task>& tasks, uint32_t threadMask);
200 * @brief Returns number of worker threads
201 * @return Number of worker threads
203 size_t GetWorkerCount() const;
208 std::unique_ptr<Impl> mImpl;
213 #endif // DALI_THREAD_POOL_H