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 "eventhandler_interactive_queue.h"
17 #include "dfx/log/ffrt_log_api.h"
18
19 namespace ffrt {
~EventHandlerInteractiveQueue()20 EventHandlerInteractiveQueue::~EventHandlerInteractiveQueue()
21 {
22 FFRT_LOGI("destruct eventhandler interactive queueId=%u leave", queueId_);
23 }
24
Push(QueueTask * task)25 int EventHandlerInteractiveQueue::Push(QueueTask* task)
26 {
27 Priority prio = EventHandlerAdapter::Instance()->ConvertPriority(task->GetPriority());
28 FFRT_COND_DO_ERR((prio > Priority::IDLE), return FAILED, "Priority invalid.");
29
30 int delayUs = static_cast<int>(task->GetDelay());
31 auto f = reinterpret_cast<ffrt_function_header_t*>(task->func_storage);
32 std::function<void()> func = [=]() {
33 f->exec(f);
34 f->destroy(f);
35 task->DecDeleteRef();
36 };
37
38 int msPerSecond = 1000;
39 ffrt::TaskOptions taskOptions(
40 task->label, delayUs / msPerSecond, static_cast<Priority>(prio), static_cast<uintptr_t>(task->gid));
41 bool taskStatus = EventHandlerAdapter::Instance()->PostTask(eventHandler_, func, taskOptions);
42 FFRT_COND_DO_ERR((!taskStatus), return FAILED, "post task fail");
43
44 return SUCC;
45 }
46
CreateEventHandlerInteractiveQueue(const ffrt_queue_attr_t * attr)47 std::unique_ptr<BaseQueue> CreateEventHandlerInteractiveQueue(const ffrt_queue_attr_t* attr)
48 {
49 (void)attr;
50 return std::make_unique<EventHandlerInteractiveQueue>();
51 }
52 } // namespace ffrt
53