• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "serial_queue.h"
17 #include "dfx/log/ffrt_log_api.h"
18 #include "tm/queue_task.h"
19 
20 namespace {
21 constexpr uint32_t MIN_OVERLOAD_INTERVAL = 16;
22 constexpr uint32_t MAX_OVERLOAD_INTERVAL = 128;
23 }
24 namespace ffrt {
SerialQueue()25 SerialQueue::SerialQueue()
26 {
27     dequeFunc_ = QueueStrategy<QueueTask>::DequeBatch;
28     overloadThreshold_ = MIN_OVERLOAD_INTERVAL;
29 }
30 
~SerialQueue()31 SerialQueue::~SerialQueue()
32 {
33     FFRT_LOGI("destruct serial queueId=%u leave", queueId_);
34 }
35 
Push(QueueTask * task)36 int SerialQueue::Push(QueueTask* task)
37 {
38     std::unique_lock lock(mutex_);
39     FFRT_COND_DO_ERR(isExit_, return FAILED, "cannot push task, [queueId=%u] is exiting", queueId_);
40 
41     if (!isActiveState_.load()) {
42         isActiveState_.store(true);
43         return INACTIVE;
44     }
45 
46     if (task->InsertHead() && !whenMap_.empty()) {
47         FFRT_LOGD("head insert task=%u in [queueId=%u]", task->gid, queueId_);
48         uint64_t headTime = (whenMap_.begin()->first > 0) ? whenMap_.begin()->first - 1 : 0;
49         whenMap_.insert({std::min(headTime, task->GetUptime()), task});
50     } else {
51         whenMap_.insert({task->GetUptime(), task});
52     }
53 
54     if (task == whenMap_.begin()->second) {
55         cond_.notify_one();
56     } else if ((whenMap_.begin()->second->GetDelay() > 0) && (GetNow() > whenMap_.begin()->first)) {
57         FFRT_LOGI("push task notify cond_wait.");
58         cond_.notify_one();
59     }
60 
61     if (whenMap_.size() >= overloadThreshold_) {
62         FFRT_LOGW("[queueId=%u] overload warning, size=%llu", queueId_, whenMap_.size());
63         overloadThreshold_ += std::min(overloadThreshold_, MAX_OVERLOAD_INTERVAL);
64     }
65 
66     return SUCC;
67 }
68 
Pull()69 QueueTask* SerialQueue::Pull()
70 {
71     std::unique_lock lock(mutex_);
72     // wait for delay task
73     uint64_t now = GetNow();
74     while (!whenMap_.empty() && now < whenMap_.begin()->first && !isExit_) {
75         uint64_t diff = whenMap_.begin()->first - now;
76         FFRT_LOGD("[queueId=%u] stuck in %llu us wait", queueId_, diff);
77         cond_.wait_for(lock, std::chrono::microseconds(diff));
78         FFRT_LOGD("[queueId=%u] wakeup from wait", queueId_);
79         now = GetNow();
80     }
81 
82     // abort dequeue in abnormal scenarios
83     if (whenMap_.empty()) {
84         FFRT_LOGD("[queueId=%u] switch into inactive", queueId_);
85         isActiveState_.store(false);
86         return nullptr;
87     }
88     FFRT_COND_DO_ERR(isExit_, return nullptr, "cannot pull task, [queueId=%u] is exiting", queueId_);
89 
90     if (overloadThreshold_ > MAX_OVERLOAD_INTERVAL && whenMap_.size() < MAX_OVERLOAD_INTERVAL) {
91         overloadThreshold_ = MAX_OVERLOAD_INTERVAL;
92     }
93 
94     // dequeue due tasks in batch
95     return dequeFunc_(queueId_, now, &whenMap_, nullptr);
96 }
97 
CreateSerialQueue(const ffrt_queue_attr_t * attr)98 std::unique_ptr<BaseQueue> CreateSerialQueue(const ffrt_queue_attr_t* attr)
99 {
100     (void)attr;
101     return std::make_unique<SerialQueue>();
102 }
103 } // namespace ffrt
104