1 /*
2 * Copyright (c) 2025 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 "ffrt_queue_helper.h"
17
18 #include <cstdint>
19
20 #include "ffrt_inner.h"
21 #include "window_manager_hilog.h"
22
23 namespace OHOS::Rosen {
24 constexpr int32_t FFRT_USER_INTERACTIVE_MAX_THREAD_NUM = 5;
25
FfrtQueueHelper()26 FfrtQueueHelper::FfrtQueueHelper()
27 {
28 ffrtQueue_ = std::make_unique<ffrt::queue>(ffrt::queue_concurrent, "FfrtQueueHelper",
29 ffrt::queue_attr()
30 .qos(ffrt_qos_user_interactive)
31 .max_concurrency(FFRT_USER_INTERACTIVE_MAX_THREAD_NUM));
32 }
33
34 FfrtQueueHelper::~FfrtQueueHelper() = default;
35
SubmitTaskAndWait(std::function<void ()> && task,uint64_t timeout)36 bool FfrtQueueHelper::SubmitTaskAndWait(std::function<void()>&& task, uint64_t timeout)
37 {
38 auto timeoutFuture = std::make_shared<TimeoutFuture<bool>>();
39 ffrtQueue_->submit([localTask = std::move(task), timeoutFuture, where = __func__] {
40 if (localTask) {
41 localTask();
42 } else {
43 TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s: task function is empty", where);
44 }
45 timeoutFuture->FutureCall(true);
46 }, __func__);
47 bool isTimeout = false;
48 timeoutFuture->GetResult(timeout, isTimeout);
49 return isTimeout;
50 }
51
SubmitTask(std::function<void ()> && task)52 void FfrtQueueHelper::SubmitTask(std::function<void()>&& task)
53 {
54 ffrtQueue_->submit(task);
55 }
56 } // namespace OHOS::Rosen
57