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