• 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 
16 #ifndef OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
17 #define OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
18 
19 #include <event_handler.h>
20 
21 namespace OHOS::Rosen {
22 class TaskScheduler {
23 public:
24     TaskScheduler(const std::string& threadName);
25     ~TaskScheduler() = default;
26 
27     std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
28     using Task = std::function<void()>;
29     void PostAsyncTask(Task task, int64_t delayTime = 0);
30     void PostVoidSyncTask(Task task);
31     template<typename SyncTask, typename Return = std::invoke_result_t<SyncTask>>
PostSyncTask(SyncTask && task)32     Return PostSyncTask(SyncTask&& task)
33     {
34         Return ret;
35         auto syncTask = [&ret, &task]() { ret = task(); };
36         if (handler_) {
37             handler_->PostSyncTask(syncTask, AppExecFwk::EventQueue::Priority::IMMEDIATE);
38         }
39         return ret;
40     }
41 
42 private:
43     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
44 };
45 } // namespace OHOS::Rosen
46 
47 #endif // OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
48