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