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