1 /*
2 * Copyright (c) 2023 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 <unordered_map>
17 #include "dfx/log/ffrt_log_api.h"
18 #include "tm/queue_task.h"
19 #include "serial_queue.h"
20 #include "concurrent_queue.h"
21 #include "eventhandler_adapter_queue.h"
22 #include "eventhandler_interactive_queue.h"
23 #include "base_queue.h"
24
25 namespace {
26 using CreateFunc = std::unique_ptr<ffrt::BaseQueue>(*)(const ffrt_queue_attr_t*);
27 const std::unordered_map<int, CreateFunc> CREATE_FUNC_MAP = {
28 { ffrt_queue_serial, ffrt::CreateSerialQueue },
29 { ffrt_queue_concurrent, ffrt::CreateConcurrentQueue },
30 { ffrt_queue_eventhandler_interactive, ffrt::CreateEventHandlerInteractiveQueue },
31 { ffrt_queue_eventhandler_adapter, ffrt::CreateEventHandlerAdapterQueue },
32 };
33
ClearWhenMap(std::multimap<uint64_t,ffrt::QueueTask * > & whenMap,ffrt::condition_variable & cond)34 void ClearWhenMap(std::multimap<uint64_t, ffrt::QueueTask*>& whenMap, ffrt::condition_variable& cond)
35 {
36 for (auto it = whenMap.begin(); it != whenMap.end(); it++) {
37 if (it->second) {
38 it->second->Notify();
39 it->second->Destroy();
40 it->second = nullptr;
41 }
42 }
43 whenMap.clear();
44 cond.notify_one();
45 }
46 }
47
48 namespace ffrt {
49 // 0预留为非法值
50 std::atomic_uint32_t BaseQueue::queueId(1);
Stop()51 void BaseQueue::Stop()
52 {
53 std::unique_lock lock(mutex_);
54 Stop(whenMap_);
55 FFRT_LOGI("clear [queueId=%u] succ", queueId_);
56 }
57
Stop(std::multimap<uint64_t,QueueTask * > & whenMap)58 void BaseQueue::Stop(std::multimap<uint64_t, QueueTask*>& whenMap)
59 {
60 isExit_ = true;
61 ClearWhenMap(whenMap, cond_);
62 }
63
Remove()64 void BaseQueue::Remove()
65 {
66 std::unique_lock lock(mutex_);
67 Remove(whenMap_);
68 }
69
Remove(std::multimap<uint64_t,QueueTask * > & whenMap)70 void BaseQueue::Remove(std::multimap<uint64_t, QueueTask*>& whenMap)
71 {
72 FFRT_COND_DO_ERR(isExit_, return, "cannot remove task, [queueId=%u] is exiting", queueId_);
73
74 ClearWhenMap(whenMap, cond_);
75 FFRT_LOGD("cancel [queueId=%u] all tasks succ", queueId_);
76 }
77
Remove(const char * name)78 int BaseQueue::Remove(const char* name)
79 {
80 std::unique_lock lock(mutex_);
81 return Remove(name, whenMap_);
82 }
83
Remove(const char * name,std::multimap<uint64_t,QueueTask * > & whenMap)84 int BaseQueue::Remove(const char* name, std::multimap<uint64_t, QueueTask*>& whenMap)
85 {
86 FFRT_COND_DO_ERR(isExit_, return FAILED, "cannot remove task, [queueId=%u] is exiting", queueId_);
87
88 int removedCount = 0;
89 for (auto iter = whenMap.begin(); iter != whenMap.end();) {
90 if (iter->second->IsMatch(name)) {
91 FFRT_LOGD("cancel task[%llu] %s succ", iter->second->gid, iter->second->label.c_str());
92 iter->second->Notify();
93 iter->second->Destroy();
94 iter = whenMap.erase(iter);
95 removedCount++;
96 } else {
97 ++iter;
98 }
99 }
100
101 return removedCount > 0 ? SUCC : FAILED;
102 }
103
Remove(const QueueTask * task)104 int BaseQueue::Remove(const QueueTask* task)
105 {
106 std::unique_lock lock(mutex_);
107 return Remove(task, whenMap_);
108 }
109
Remove(const QueueTask * task,std::multimap<uint64_t,QueueTask * > & whenMap)110 int BaseQueue::Remove(const QueueTask* task, std::multimap<uint64_t, QueueTask*>& whenMap)
111 {
112 FFRT_COND_DO_ERR(isExit_, return FAILED, "cannot remove task, [queueId=%u] is exiting", queueId_);
113
114 auto range = whenMap.equal_range(task->GetUptime());
115 for (auto it = range.first; it != range.second; it++) {
116 if (it->second == task) {
117 whenMap.erase(it);
118 return SUCC;
119 }
120 }
121
122 return FAILED;
123 }
124
HasTask(const char * name)125 bool BaseQueue::HasTask(const char* name)
126 {
127 std::unique_lock lock(mutex_);
128 return HasTask(name, whenMap_);
129 }
130
HasTask(const char * name,std::multimap<uint64_t,QueueTask * > whenMap)131 bool BaseQueue::HasTask(const char* name, std::multimap<uint64_t, QueueTask*> whenMap)
132 {
133 auto iter = std::find_if(whenMap.cbegin(), whenMap.cend(),
134 [name](const auto& pair) { return pair.second->IsMatch(name); });
135 return iter != whenMap.cend();
136 }
137
CreateQueue(int queueType,const ffrt_queue_attr_t * attr)138 std::unique_ptr<BaseQueue> CreateQueue(int queueType, const ffrt_queue_attr_t* attr)
139 {
140 const auto iter = CREATE_FUNC_MAP.find(queueType);
141 FFRT_COND_DO_ERR((iter == CREATE_FUNC_MAP.end()), return nullptr, "invalid queue type");
142
143 return iter->second(attr);
144 }
145 } // namespace ffrt
146