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#include "EventRunner.h"
17
18namespace OHOS::AppExecFwk {
19EventRunner& EventRunner::Current()
20{
21    static EventRunner mainRunner;
22    return mainRunner;
23}
24
25EventRunner& EventRunner::GetMainEventRunner()
26{
27    return Current();
28}
29
30void EventRunner::SetMainThreadId(std::thread::id id)
31{
32    threadId = id;
33}
34
35std::thread::id EventRunner::GetThreadId()
36{
37    return threadId;
38}
39
40bool EventRunner::IsCurrentRunnerThread()
41{
42    return std::this_thread::get_id() == threadId;
43}
44
45void EventRunner::Run()
46{
47    const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
48    std::vector<Callback> copyTasks;
49    // Process expired tasks.
50    {
51        std::lock_guard<std::mutex> lock(mutex);
52        while (!queue.empty()) {
53            const auto& top = queue.top();
54            // If the task at the top of task queue has not yet expired, there is nothing more to do.
55            if (top.GetTargetTime() > now) {
56                break;
57            }
58            // Only record tasks without executing them when the task queue mutex is hold.
59            copyTasks.push_back(top.GetTask());
60            queue.pop();
61        }
62    }
63    {
64        // Flushing tasks here without holing onto the task queue mutex.
65        for (const auto& task : copyTasks) {
66            task();
67        }
68    }
69}
70
71void EventRunner::PushTask(const Callback &callback, std::chrono::steady_clock::time_point targetTime)
72{
73    static size_t order = 0;
74    std::lock_guard<std::mutex> lock(mutex);
75    order++;
76    queue.push({ order, callback, targetTime });
77}
78}