• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_LIBEVENTHANDLER_INCLUDE_EVENT_RUNNER_H
17 #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_LIBEVENTHANDLER_INCLUDE_EVENT_RUNNER_H
18 
19 #include <atomic>
20 
21 #include "event_queue.h"
22 #include "dumper.h"
23 #include "logger.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class EventInnerRunner;
28 
29 class EventRunner final {
30 public:
31     EventRunner() = delete;
32     ~EventRunner();
33     DISALLOW_COPY_AND_MOVE(EventRunner);
34 
35     /**
36      * Create new 'EventRunner'.
37      *
38      * @param inNewThread True if create new thread to start the 'EventRunner' automatically.
39      * @return Returns shared pointer of the new 'EventRunner'.
40      */
41     static std::shared_ptr<EventRunner> Create(bool inNewThread = true);
42 
43     /**
44      * Create new 'EventRunner' and start to run in a new thread.
45      *
46      * @param threadName Thread name of the new created thread.
47      * @return Returns shared pointer of the new 'EventRunner'.
48      */
49     static std::shared_ptr<EventRunner> Create(const std::string &threadName);
50 
51     /**
52      * Create new 'EventRunner' and start to run in a new thread.
53      * Eliminate ambiguity, while calling like 'EventRunner::Create("threadName")'.
54      *
55      * @param threadName Thread name of the new created thread.
56      * @return Returns shared pointer of the new 'EventRunner'.
57      */
Create(const char * threadName)58     static inline std::shared_ptr<EventRunner> Create(const char *threadName)
59     {
60         return Create((threadName != nullptr) ? std::string(threadName) : std::string());
61     }
62 
63     /**
64      * Get event runner on current thread.
65      *
66      * @return Returns shared pointer of the current 'EventRunner'.
67      */
68     static std::shared_ptr<EventRunner> Current();
69 
70     /**
71      * Start to run the 'EventRunner'. Only used for the 'EventRunner' which is not running in new thread.
72      * Only running on single thread.
73      *
74      * @return Returns 'ERR_OK' on success.
75      */
76     ErrCode Run();
77 
78     /**
79      * Stop to run the 'EventRunner'. Only used for the 'EventRunner' which is not running in new thread.
80      * It is a good practice to call {@link #Stop} on the same thread that called {@link #Run}.
81      *
82      * @return Returns 'ERR_OK' on success.
83      */
84     ErrCode Stop();
85 
86     /**
87      * Get event queue from event runner.
88      * This method only called by 'EventHandler'.
89      *
90      * @return Returns event queue.
91      */
GetEventQueue()92     inline const std::shared_ptr<EventQueue> &GetEventQueue() const
93     {
94         return queue_;
95     }
96 
97     /**
98      * Obtain the event queue of the EventRunner associated with the current thread.
99      *
100      * @return Return current event queue.
101      */
102     static std::shared_ptr<EventQueue> GetCurrentEventQueue();
103 
104     /**
105      * Print out the internal information about an object in the specified format,
106      * helping you diagnose internal errors of the object.
107      *
108      * @param dumpr The Dumper object you have implemented to process the output internal information.
109      */
110     void Dump(Dumper &dumper);
111 
112     /**
113      * Set the Logger object for logging messages that are processed by this event runner.
114      *
115      * @param logger The Logger object you have implemented for logging messages.
116      */
117     void SetLogger(const std::shared_ptr<Logger> &logger);
118 
119     /**
120      * Obtain the ID of the worker thread associated with this EventRunner.
121      *
122      * @return thread id.
123      */
124     uint64_t GetThreadId();
125 
126     /**
127      * Check whether the current thread is the worker thread of this EventRunner.
128      *
129      * @return Returns true if the current thread is the worker thread of this EventRunner; returns false otherwise.
130      */
131     bool IsCurrentRunnerThread();
132 
133     /**
134      * Obtains the EventRunner for the main thread of the application.
135      *
136      * @return Returns the EventRunner for the main thread of the application.
137      */
138     static std::shared_ptr<EventRunner> GetMainEventRunner();
139 
140 private:
141     explicit EventRunner(bool deposit);
142 
143     friend class EventHandler;
144 
145     /**
146      * Check whether this event runner is running.
147      *
148      * @return if this event runner is running return true otherwise return false
149      */
IsRunning()150     inline bool IsRunning() const
151     {
152         // If this runner is deposited, it it always running
153         return (deposit_) || (running_.load());
154     }
155 
156     bool deposit_{true};
157     std::atomic<bool> running_{false};
158     std::shared_ptr<EventQueue> queue_;
159     std::shared_ptr<EventInnerRunner> innerRunner_;
160     static std::shared_ptr<EventRunner> mainRunner_;
161 };
162 }  // namespace AppExecFwk
163 }  // namespace OHOS
164 
165 #endif  // #ifndef FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_LIBEVENTHANDLER_INCLUDE_EVENT_RUNNER_H
166