• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CORE_THREADING_TASK_QUEUE_H
17 #define CORE_THREADING_TASK_QUEUE_H
18 
19 #include <cstdint>
20 #include <functional>
21 
22 #include <base/containers/vector.h>
23 #include <core/namespace.h>
24 #include <core/threading/intf_thread_pool.h>
25 
CORE_BEGIN_NAMESPACE()26 CORE_BEGIN_NAMESPACE()
27 
28 // Helper class for running std::function as a ThreadPool task.
29 class FunctionTask final : public IThreadPool::ITask {
30 public:
31     static Ptr Create(std::function<void()>&& func)
32     {
33         return Ptr { new FunctionTask(std::move(func)) };
34     }
35 
36     explicit FunctionTask(std::function<void()>&& func) : func_(std::move(func)) {};
37 
38     void operator()() override
39     {
40         func_();
41     }
42 
43 protected:
44     void Destroy() override
45     {
46         delete this;
47     }
48 
49 private:
50     std::function<void()> func_;
51 };
52 
53 // Abstract base class for task queues.
54 /* Usage examples:
55  *  1. SequentialTaskQueue queue(threadManager.GetThreadPool());
56  *  2. queue.Submit("identifier A", FunctionTask::Create(function));
57  *  3. queue.Submit("identifier B", FunctionTask::Create(std::bind(&Classname::function, this)));
58  *  4. queue.Submit("identifier C", FunctionTask::Create([]() { <code> }));
59  */
60 class TaskQueue {
61 public:
62     /** Constructor for the task queue.
63         @param aThreadPool Optional thread pool, if support for threading is desired.
64     */
65     explicit TaskQueue(const IThreadPool::Ptr& threadPool);
66     TaskQueue(const TaskQueue& other) = delete;
67     TaskQueue& operator=(const TaskQueue& other) = delete;
68 
69     virtual ~TaskQueue();
70 
71     /** Submit task to end of execution queue.
72         @param taskIdentifier Identifier of the task, must be unique.
73         @param task Task to execute.
74     */
75     virtual void Submit(uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task) = 0;
76 
77     /** Remove task from execution queue.
78         @param taskIdentifier Identifier of the task.
79     */
80     virtual void Remove(uint64_t taskIdentifier) = 0;
81 
82     /** Remove all tasks from execution queue. */
83     virtual void Clear() = 0;
84 
85     /** Execute task queue in this thread. */
86     virtual void Execute() = 0;
87 
88     /** Execute task queue asynchronously in new thread. */
89     virtual void ExecuteAsync();
90 
91     /** Checks if task queue is running asynchronously.
92         @return True if task queue is currently running and can't be re-executed.
93     */
94     bool IsRunningAsync() const;
95 
96     /** Waits until task queue has completed asynchronous execution */
97     void Wait();
98 
99 protected:
100     class ExecuteAsyncTask final : public IThreadPool::ITask {
101     public:
102         explicit ExecuteAsyncTask(TaskQueue& queue);
103         void operator()() override;
104 
105     protected:
106         void Destroy() override;
107 
108     private:
109         TaskQueue& queue_;
110     };
111 
112     struct Entry {
113         Entry() = default;
114         Entry(uint64_t identifier, IThreadPool::ITask::Ptr task);
115         bool operator==(uint64_t identifier) const;
116         bool operator==(const Entry& other) const;
117 
118         IThreadPool::ITask::Ptr task;
119         uint64_t identifier {};
120         BASE_NS::vector<uint64_t> dependencies;
121     };
122 
123     IThreadPool::Ptr threadPool_;
124     IThreadPool::IResult::Ptr asyncOperation_;
125     int32_t isRunningAsync_;
126 };
127 CORE_END_NAMESPACE()
128 
129 #endif // CORE_THREADING_TASK_QUEUE_H
130