• 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 #include <unistd.h>
22 #include "window_manager_hilog.h"
23 
24 namespace OHOS::Rosen {
25 
26 void StartTraceForSyncTask(const std::string& name);
27 void FinishTraceForSyncTask();
28 
29 class TaskScheduler {
30 public:
31     explicit TaskScheduler(const std::string& threadName);
32     ~TaskScheduler() = default;
33 
34     std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
35 
36     using Task = std::function<void()>;
37     void PostAsyncTask(Task&& task, const std::string& name, int64_t delayTime = 0);
38     void PostTask(Task&& task, const std::string& name, int64_t delayTime = 0);
39     void RemoveTask(const std::string& name);
40     void PostVoidSyncTask(Task&& task, const std::string& name = "ssmTask");
41     template<typename SyncTask, typename Return = std::invoke_result_t<SyncTask>>
42     Return PostSyncTask(SyncTask&& task, const std::string& name = "ssmTask")
43     {
44         Return ret;
45         if (handler_->GetEventRunner()->IsCurrentRunnerThread()) {
46             StartTraceForSyncTask(name);
47             ret = task();
48             FinishTraceForSyncTask();
49             return ret;
50         }
51         auto syncTask = [this, &ret, &task, &name] {
52             StartTraceForSyncTask(name);
53             ret = task();
54             FinishTraceForSyncTask();
55             ExecuteExportTask();
56         };
57         AppExecFwk::EventQueue::Priority priority = AppExecFwk::EventQueue::Priority::IMMEDIATE;
58         static pid_t pid = getpid();
59         if (pid == gettid()) {
60             priority = AppExecFwk::EventQueue::Priority::VIP;
61         }
62         bool result = handler_->PostSyncTask(std::move(syncTask), "wms:" + name, priority);
63         if (!result) {
64             TLOGE(WmsLogTag::DEFAULT, "post task failed");
65         }
66         return ret;
67     }
68 
69     void SetExportHandler(const std::shared_ptr<AppExecFwk::EventHandler>& handler);
70     /*
71      * Add export task, which will be executed after a task OS_SceneSession,
72      * same name means same task, will be only executed once.
73      */
74     void AddExportTask(std::string taskName, Task&& task);
75 
76 private:
77     void ExecuteExportTask();
78     std::unordered_map<std::string, Task> exportFuncMap_; // ONLY Accessed in OS_SceneSession
79     std::shared_ptr<AppExecFwk::EventHandler> handler_;
80     std::shared_ptr<AppExecFwk::EventHandler> exportHandler_;
81 };
82 } // namespace OHOS::Rosen
83 #endif // OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
84