1 /*
2 * Copyright (c) 2024 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 #include "threading/sequential_task_queue.h"
17
18 #include <algorithm>
19 #include <cstddef>
20
21 #include <base/containers/array_view.h>
22 #include <base/containers/iterator.h>
23 #include <base/containers/unique_ptr.h>
24 #include <base/containers/vector.h>
25 #include <core/log.h>
26 #include <core/namespace.h>
27 #include <core/threading/intf_thread_pool.h>
28
CORE_BEGIN_NAMESPACE()29 CORE_BEGIN_NAMESPACE()
30 // -- Sequential task queue.
31 SequentialTaskQueue::SequentialTaskQueue(const IThreadPool::Ptr& threadPool) : TaskQueue(threadPool) {}
32
~SequentialTaskQueue()33 SequentialTaskQueue::~SequentialTaskQueue()
34 {
35 Wait();
36 }
37
Execute()38 void SequentialTaskQueue::Execute()
39 {
40 // A function that executes tasks one by one.
41 for (auto& entry : tasks_) {
42 (*entry.task)();
43 }
44 }
45
Submit(uint64_t taskIdentifier,IThreadPool::ITask::Ptr && task)46 void SequentialTaskQueue::Submit(uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task)
47 {
48 CORE_ASSERT(std::find(tasks_.cbegin(), tasks_.cend(), taskIdentifier) == tasks_.cend());
49
50 tasks_.emplace_back(taskIdentifier, std::move(task));
51 }
52
SubmitAfter(uint64_t afterIdentifier,uint64_t taskIdentifier,IThreadPool::ITask::Ptr && task)53 void SequentialTaskQueue::SubmitAfter(uint64_t afterIdentifier, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task)
54 {
55 auto it = std::find(tasks_.begin(), tasks_.end(), afterIdentifier);
56 if (it != tasks_.end()) {
57 tasks_.emplace(++it, taskIdentifier, std::move(task));
58 } else {
59 tasks_.emplace_back(taskIdentifier, std::move(task));
60 }
61 }
62
SubmitAfter(BASE_NS::array_view<const uint64_t> afterIdentifiers,uint64_t taskIdentifier,IThreadPool::ITask::Ptr && task)63 void SequentialTaskQueue::SubmitAfter(
64 BASE_NS::array_view<const uint64_t> afterIdentifiers, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task)
65 {
66 ptrdiff_t pos = -1;
67 for (const auto afterIdentifier : afterIdentifiers) {
68 auto it = std::find(tasks_.begin(), tasks_.end(), afterIdentifier);
69 if (it != tasks_.end()) {
70 pos = std::max(pos, std::distance(tasks_.begin(), it));
71 }
72 }
73 if (pos >= 0) {
74 tasks_.emplace(tasks_.begin() + (pos + 1), taskIdentifier, std::move(task));
75 } else {
76 tasks_.emplace_back(taskIdentifier, std::move(task));
77 }
78 }
79
SubmitBefore(uint64_t beforeIdentifier,uint64_t taskIdentifier,IThreadPool::ITask::Ptr && task)80 void SequentialTaskQueue::SubmitBefore(
81 uint64_t beforeIdentifier, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task)
82 {
83 CORE_ASSERT(std::find(tasks_.cbegin(), tasks_.cend(), taskIdentifier) == tasks_.cend());
84
85 auto it = std::find(tasks_.begin(), tasks_.end(), beforeIdentifier);
86 if (it != tasks_.end()) {
87 tasks_.emplace(it, taskIdentifier, std::move(task));
88 }
89 }
90
Remove(uint64_t taskIdentifier)91 void SequentialTaskQueue::Remove(uint64_t taskIdentifier)
92 {
93 auto it = std::find(tasks_.cbegin(), tasks_.cend(), taskIdentifier);
94 if (it != tasks_.cend()) {
95 tasks_.erase(it);
96 }
97 }
98
Clear()99 void SequentialTaskQueue::Clear()
100 {
101 Wait();
102 tasks_.clear();
103 }
104 CORE_END_NAMESPACE()
105