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 "queue_task_handler_wrap.h"
17
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace AAFwk {
22 constexpr int32_t QUEUE_TIME_OUT = 500000; // us
QueueTaskHandlerWrap(const std::string & queueName,TaskQoS queueQos)23 QueueTaskHandlerWrap::QueueTaskHandlerWrap(const std::string &queueName, TaskQoS queueQos)
24 : taskQueue_(queueName.c_str(), ffrt::queue_attr().qos(Convert2FfrtQos(queueQos)).timeout(QUEUE_TIME_OUT))
25 {}
SubmitTaskInner(std::function<void ()> && task,const TaskAttribute & taskAttr)26 std::shared_ptr<InnerTaskHandle> QueueTaskHandlerWrap::SubmitTaskInner(std::function<void()> &&task,
27 const TaskAttribute &taskAttr)
28 {
29 if (taskAttr.IsDefault()) {
30 return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task)));
31 } else {
32 ffrt::task_attr ffrtTaskAttr;
33 BuildFfrtTaskAttr(taskAttr, ffrtTaskAttr);
34 return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task),
35 ffrtTaskAttr));
36 }
37 }
CancelTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)38 bool QueueTaskHandlerWrap::CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
39 {
40 if (!taskHandle) {
41 return false;
42 }
43 return taskQueue_.cancel(taskHandle->GetFfrtHandle()) == 0;
44 }
WaitTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)45 void QueueTaskHandlerWrap::WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
46 {
47 if (!taskHandle) {
48 return;
49 }
50 taskQueue_.wait(taskHandle->GetFfrtHandle());
51 }
52 } // namespace AAFwk
53 } // namespace OHOS