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 whenMap_.insert({task->GetUptime(), task});
37 if (task == whenMap_.begin()->second) {
38 cond_.NotifyOne();
39 }
40
41 return SUCC;
42 }
43
Pull()44 QueueTask* SerialQueue::Pull()
45 {
46 std::unique_lock lock(mutex_);
47 // wait for delay task
48 uint64_t now = GetNow();
49 while (!whenMap_.empty() && now < whenMap_.begin()->first && !isExit_) {
50 uint64_t diff = whenMap_.begin()->first - now;
51 FFRT_LOGD("[queueId=%u] stuck in %llu us wait", queueId_, diff);
52 cond_.WaitFor(lock, std::chrono::microseconds(diff));
53 FFRT_LOGD("[queueId=%u] wakeup from wait", queueId_);
54 now = GetNow();
55 }
56
57 // abort dequeue in abnormal scenarios
58 if (whenMap_.empty()) {
59 FFRT_LOGD("[queueId=%u] switch into inactive", queueId_);
60 isActiveState_.store(false);
61 return nullptr;
62 }
63 FFRT_COND_DO_ERR(isExit_, return nullptr, "cannot pull task, [queueId=%u] is exiting", queueId_);
64
65 // dequeue due tasks in batch
66 return dequeFunc_(queueId_, now, whenMap_, nullptr);
67 }
68
CreateSerialQueue(const ffrt_queue_attr_t* attr)69 std::unique_ptr<BaseQueue> CreateSerialQueue(const ffrt_queue_attr_t* attr)
70 {
71 (void)attr;
72 return std::make_unique<SerialQueue>();
73 }
74 } // namespace ffrt
75