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
36 // LCOV_EXCL_START
SubmitTaskAndWait(std::function<void ()> && task,uint64_t timeout)37 bool FfrtQueueHelper::SubmitTaskAndWait(std::function<void()>&& task, uint64_t timeout)
38 {
39 auto timeoutFuture = std::make_shared<TimeoutFuture<bool>>();
40 ffrtQueue_->submit([localTask = std::move(task), timeoutFuture, where = __func__] {
41 if (localTask) {
42 localTask();
43 } else {
44 TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s: task function is empty", where);
45 }
46 timeoutFuture->FutureCall(true);
47 }, __func__);
48 bool isTimeout = false;
49 timeoutFuture->GetResult(timeout, isTimeout);
50 return isTimeout;
51 }
52
SubmitTask(std::function<void ()> && task)53 void FfrtQueueHelper::SubmitTask(std::function<void()>&& task)
54 {
55 ffrtQueue_->submit(task);
56 }
57 // LCOV_EXCL_STOP
58 } // namespace OHOS::Rosen
59