• 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 #include "serial_handler.h"
16 #include "dfx/log/ffrt_log_api.h"
17 #include "ffrt_trace.h"
18 
19 namespace ffrt {
Cancel(ITask * task)20 int SerialHandler::Cancel(ITask* task)
21 {
22     FFRT_COND_DO_ERR((task == nullptr), return -1, "submit task is nullptr");
23     FFRT_COND_DO_ERR((looper_ == nullptr || looper_->GetQueueIns() == nullptr), return -1, "queue is nullptr");
24     int ret = looper_->GetQueueIns()->RemoveTask(task);
25     FFRT_LOGD("cancel serial task gid=%llu return [%d], qid=%u", task->gid, ret, looper_->GetQueueId());
26     if (ret == 0) {
27         auto f = reinterpret_cast<ffrt_function_header_t*>(task->func_storage);
28         f->destroy(f);
29         DestroyTask(task);
30     }
31     return ret;
32 }
33 
DispatchTask(ITask * task)34 void SerialHandler::DispatchTask(ITask* task)
35 {
36     FFRT_COND_DO_ERR((task == nullptr), return, "failed to dispatch, task is nullptr");
37     FFRT_SERIAL_QUEUE_TASK_EXECUTE_MARKER(task->gid);
38     auto f = reinterpret_cast<ffrt_function_header_t*>(task->func_storage);
39     f->exec(f);
40     FFRT_LOGD("dispatch serial task gid=%llu succ, qid=%u", task->gid, looper_->GetQueueId());
41     f->destroy(f);
42     DestroyTask(task);
43 }
44 
SubmitDelayed(ITask * task,uint64_t delayUs)45 int SerialHandler::SubmitDelayed(ITask* task, uint64_t delayUs)
46 {
47     FFRT_COND_DO_ERR((task == nullptr), return -1, "submit task is nullptr");
48     FFRT_COND_DO_ERR((looper_ == nullptr || looper_->GetQueueIns() == nullptr), return -1, "queue is nullptr");
49     FFRT_LOGD("submit serial task gid=%llu with delay [%llu us], qid=%u", task->gid, delayUs, looper_->GetQueueId());
50     FFRT_SERIAL_QUEUE_TASK_SUBMIT_MARKER(looper_->GetQueueId(), task->gid);
51     auto nowUs = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
52     uint64_t upTime = static_cast<uint64_t>(nowUs.time_since_epoch().count());
53     if (delayUs > 0) {
54         upTime = upTime + delayUs;
55     }
56     return looper_->GetQueueIns()->PushTask(task, upTime);
57 }
58 
DestroyTask(ITask * task)59 void SerialHandler::DestroyTask(ITask* task)
60 {
61     FFRT_SERIAL_QUEUE_TASK_FINISH_MARKER(task->gid);
62     task->Notify();
63     task->DecDeleteRef();
64 }
65 } // namespace ffrt
66