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