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