1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#ifndef OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
17e0dac50fSopenharmony_ci#define OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
18e0dac50fSopenharmony_ci
19e0dac50fSopenharmony_ci#include <event_handler.h>
20e0dac50fSopenharmony_ci
21e0dac50fSopenharmony_ci#include <unistd.h>
22e0dac50fSopenharmony_ci
23e0dac50fSopenharmony_cinamespace OHOS::Rosen {
24e0dac50fSopenharmony_ci
25e0dac50fSopenharmony_civoid StartTraceForSyncTask(std::string name);
26e0dac50fSopenharmony_civoid FinishTraceForSyncTask();
27e0dac50fSopenharmony_ci
28e0dac50fSopenharmony_ciclass TaskScheduler : public std::enable_shared_from_this<TaskScheduler> {
29e0dac50fSopenharmony_cipublic:
30e0dac50fSopenharmony_ci    explicit TaskScheduler(const std::string& threadName);
31e0dac50fSopenharmony_ci    ~TaskScheduler() = default;
32e0dac50fSopenharmony_ci
33e0dac50fSopenharmony_ci    std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
34e0dac50fSopenharmony_ci    using Task = std::function<void()>;
35e0dac50fSopenharmony_ci    void PostAsyncTask(Task&& task, const std::string& name = "ssmTask", int64_t delayTime = 0);
36e0dac50fSopenharmony_ci    void PostVoidSyncTask(Task&& task, const std::string& name = "ssmTask");
37e0dac50fSopenharmony_ci    void PostTask(Task&& task, const std::string& name, int64_t delayTime = 0);
38e0dac50fSopenharmony_ci    void RemoveTask(const std::string& name);
39e0dac50fSopenharmony_ci    template<typename SyncTask, typename Return = std::invoke_result_t<SyncTask>>
40e0dac50fSopenharmony_ci    Return PostSyncTask(SyncTask&& task, const std::string& name = "ssmTask")
41e0dac50fSopenharmony_ci    {
42e0dac50fSopenharmony_ci        Return ret;
43e0dac50fSopenharmony_ci        if (handler_->GetEventRunner()->IsCurrentRunnerThread()) {
44e0dac50fSopenharmony_ci            StartTraceForSyncTask(name);
45e0dac50fSopenharmony_ci            ret = task();
46e0dac50fSopenharmony_ci            FinishTraceForSyncTask();
47e0dac50fSopenharmony_ci            return ret;
48e0dac50fSopenharmony_ci        }
49e0dac50fSopenharmony_ci        auto syncTask = [weak = weak_from_this(), &ret, &task, name]() {
50e0dac50fSopenharmony_ci            StartTraceForSyncTask(name);
51e0dac50fSopenharmony_ci            ret = task();
52e0dac50fSopenharmony_ci            FinishTraceForSyncTask();
53e0dac50fSopenharmony_ci            if (auto self = weak.lock()) {
54e0dac50fSopenharmony_ci                self->ExecuteExportTask();
55e0dac50fSopenharmony_ci            }
56e0dac50fSopenharmony_ci        };
57e0dac50fSopenharmony_ci        AppExecFwk::EventQueue::Priority priority = AppExecFwk::EventQueue::Priority::IMMEDIATE;
58e0dac50fSopenharmony_ci        static pid_t pid = getpid();
59e0dac50fSopenharmony_ci        if (pid == gettid()) {
60e0dac50fSopenharmony_ci            priority = AppExecFwk::EventQueue::Priority::VIP;
61e0dac50fSopenharmony_ci        }
62e0dac50fSopenharmony_ci        handler_->PostSyncTask(std::move(syncTask), "wms:" + name, priority);
63e0dac50fSopenharmony_ci        return ret;
64e0dac50fSopenharmony_ci    }
65e0dac50fSopenharmony_ci
66e0dac50fSopenharmony_ci    void SetExportHandler(const std::shared_ptr<AppExecFwk::EventHandler>& handler);
67e0dac50fSopenharmony_ci    /*
68e0dac50fSopenharmony_ci     * add export task, will be executed after a task OS_SceneSession,
69e0dac50fSopenharmony_ci     * same name means same task, will be only executed once
70e0dac50fSopenharmony_ci     */
71e0dac50fSopenharmony_ci    void AddExportTask(std::string taskName, Task task);
72e0dac50fSopenharmony_ci
73e0dac50fSopenharmony_ciprivate:
74e0dac50fSopenharmony_ci    void ExecuteExportTask();
75e0dac50fSopenharmony_ci    std::unordered_map<std::string, Task> exportFuncMap_; // will used in OS_SceneSession
76e0dac50fSopenharmony_ci    std::shared_ptr<AppExecFwk::EventHandler> handler_;
77e0dac50fSopenharmony_ci    std::weak_ptr<AppExecFwk::EventHandler> exportHandler_;
78e0dac50fSopenharmony_ci    pid_t ssmTid_ = 0;
79e0dac50fSopenharmony_ci};
80e0dac50fSopenharmony_ci} // namespace OHOS::Rosen
81e0dac50fSopenharmony_ci#endif // OHOS_ROSEN_WINDOW_SCENE_TASK_SCHEDULER_H
82