RavEngine
Loading...
Searching...
No Matches
task_thread_pool.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_UTILS_TASK_THREAD_POOL_H_
18#define RESONANCE_AUDIO_UTILS_TASK_THREAD_POOL_H_
19
20#include <atomic>
21#include <chrono>
22#include <condition_variable>
23#include <functional>
24#include <mutex>
25#include <vector>
26
27namespace vraudio {
28
29class TaskThreadPool;
30
31// A very basic thread pool for launching discrete encapsulated task functions
32// on a configurable number of task threads. Note that this pool expects tasks
33// to complete their work with no management by the pool itself. Any means of
34// managing or terminating tasks must be designed into the task contexts and
35// managed externally in a thread-safe way.
37 friend class WorkerThread;
38
39 public:
40 // Type definition for task function which may be assigned to a |WorkerThread|
41 // in this pool. Note that tasks should be self-contained and not require
42 // communication with other tasks.
43 typedef std::function<void()> TaskClosure;
44
45 // Constructor.
46 //
48
50
51 // Creates and initializes thread pool. This method blocks until all threads
52 // are loaded and initialized.
53 //
54 // @param num_worker_threads The number of worker threads to make available in
55 // the pool.
56 // @return true on success or if thread pool has been already started.
57 bool StartThreadPool(size_t num_worker_threads);
58
59 // Signals all |WorkerThread|s to stop and waits for completion.
60 void StopThreadPool();
61
62 // Waits until a |WorkerThread| becomes available. It is assumed that only a
63 // single thread will dispatch threads using this function, and therefore this
64 // function should not itself be considered thread safe.
65 //
66 // @return True if a |WorkerThread| is available.
67 bool WaitUntilWorkerBecomesAvailable();
68
69 // Executes a |TaskClosure| on a |WorkerThread|. It is assumed that only a
70 // signal thread will dispatch threads using this function, and therefore this
71 // function should not itself be considered thread safe.
72 //
73 // @param closure The client task which will begin execution if and when this
74 // function returns True.
75 // @return True if a |WorkerThread| is allocated to execute the closure
76 // function, false if no |WorkerThread| is available.
77 bool RunOnWorkerThread(TaskClosure closure);
78
79 // Query the number of |WorkerThread|s current available to do work.
80 size_t GetAvailableTaskThreadCount() const;
81
82 private:
83 // Forward declaration of |WorkerThread| class. See implementation file for
84 // class details.
85 class WorkerThread;
86
87 // Query whether the |TaskThreadPool| is active.
88 //
89 // @return True if the pool is still running.
90 bool IsPoolRunning();
91
92 // Signals to thread pool that a worker thread has become available for
93 // task assignment.
94 void SignalWorkerAvailable();
95
96 // Closure reusable task loop to be executed by each |WorkerThread|.
97 void WorkerLoopFunction();
98
99 // Task Loop executed by each worker thread.
100 void WorkerThreadLoop();
101
102 // Number of worker threads currently available to execute tasks.
103 std::atomic<int> num_worker_threads_available_;
104
105 // Control of all worker thread loops.
106 std::atomic<bool> is_pool_running_;
107
108 // Container of available worker threads, waiting to be used.
109
110 std::vector<WorkerThread> worker_threads_;
111
112 // Condition to indicate that a worker thread has become available.
113 std::condition_variable worker_available_condition_;
114
115 // Mutex for the worker thread available condition notification receiver.
116 std::mutex worker_available_mutex_;
117};
118
119} // namespace vraudio
120
121#endif // RESONANCE_AUDIO_UTILS_TASK_THREAD_POOL_H_
Definition task_thread_pool.cc:27
Definition task_thread_pool.h:36