• 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 EVENT_RUNNER_H
17 #define EVENT_RUNNER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <thread>
22 #include "EventQueue.h"
23 
24 namespace OHOS::AppExecFwk {
25 class EventRunner final {
26 public:
27     EventRunner() = default;
28     ~EventRunner() = default;
29     static EventRunner& Current();
30     static EventRunner& GetMainEventRunner();
31     void SetMainThreadId(std::thread::id id);
32     std::thread::id GetThreadId();
33     bool IsCurrentRunnerThread();
34     void Run();
35 
36     void PushTask(const Callback &callback, std::chrono::steady_clock::time_point targetTime);
37 
38 private:
39     EventRunner(const EventRunner&) = delete;
40     EventRunner &operator=(const EventRunner&) = delete;
41     std::thread::id threadId;
42     EventQueue queue;
43     std::mutex mutex;
44 };
45 }
46 #endif // EVENT_RUNNER_H
47