• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_RUNNER_H
17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_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 thread name
88      *
89      * @return Returns thread name.
90      */
91     std::string GetRunnerThreadName() const;
92 
93     /**
94      * Get event queue from event runner.
95      * This method only called by 'EventHandler'.
96      *
97      * @return Returns event queue.
98      */
GetEventQueue()99     inline const std::shared_ptr<EventQueue> &GetEventQueue() const
100     {
101         return queue_;
102     }
103 
104     /**
105      * Obtain the event queue of the EventRunner associated with the current thread.
106      *
107      * @return Return current event queue.
108      */
109     static std::shared_ptr<EventQueue> GetCurrentEventQueue();
110 
111     /**
112      * Print out the internal information about an object in the specified format,
113      * helping you diagnose internal errors of the object.
114      *
115      * @param dumpr The Dumper object you have implemented to process the output internal information.
116      */
117     void Dump(Dumper &dumper);
118 
119     /**
120      * Print out the internal information about an object in the specified format,
121      * helping you diagnose internal errors of the object.
122      *
123      * @param runnerInfo runner Info.
124      */
125     void DumpRunnerInfo(std::string& runnerInfo);
126 
127     /**
128      * Set the Logger object for logging messages that are processed by this event runner.
129      *
130      * @param logger The Logger object you have implemented for logging messages.
131      */
132     void SetLogger(const std::shared_ptr<Logger> &logger);
133 
134     /**
135      * Obtain the ID of the worker thread associated with this EventRunner.
136      *
137      * @return thread id.
138      */
139     uint64_t GetThreadId();
140 
141     /**
142      * Obtain the kernel thread ID of the worker thread associated with this EventRunner.
143      *
144      * @return kernel thread id.
145      */
146     uint64_t GetKernelThreadId();
147 
148     /**
149      * Check whether the current thread is the worker thread of this EventRunner.
150      *
151      * @return Returns true if the current thread is the worker thread of this EventRunner; returns false otherwise.
152      */
153     bool IsCurrentRunnerThread();
154 
155     /**
156      * Set the distribution standard expiration time.
157      *
158      * @param deliveryTimeout the distribution standard expiration time.
159      */
SetDeliveryTimeout(int64_t deliveryTimeout)160     void SetDeliveryTimeout(int64_t deliveryTimeout)
161     {
162         deliveryTimeout_ = deliveryTimeout;
163     }
164 
165     /**
166      * Get the distribution standard expiration time.
167      *
168      * @return the distribution standard expiration time.
169      */
GetDeliveryTimeout()170     int64_t GetDeliveryTimeout() const
171     {
172         return deliveryTimeout_;
173     }
174 
175     /**
176      * Set the execution standard timeout period.
177      *
178      * @param distributeTimeout the distribution standard expiration time.
179      */
SetDistributeTimeout(int64_t distributeTimeout)180     void SetDistributeTimeout(int64_t distributeTimeout)
181     {
182         distributeTimeout_ = distributeTimeout;
183     }
184 
185     /**
186      * Get the execution standard timeout period.
187      *
188      * @return the distribution standard expiration time.
189      */
GetDistributeTimeout()190     int64_t GetDistributeTimeout() const
191     {
192         return distributeTimeout_;
193     }
194 
195     /**
196      * Obtains the EventRunner for the main thread of the application.
197      *
198      * @return Returns the EventRunner for the main thread of the application.
199      */
200     static std::shared_ptr<EventRunner> GetMainEventRunner();
201 
202 private:
203     explicit EventRunner(bool deposit);
204 
205     friend class EventHandler;
206 
207     /**
208      * Check whether this event runner is running.
209      *
210      * @return if this event runner is running return true otherwise return false
211      */
IsRunning()212     inline bool IsRunning() const
213     {
214         // If this runner is deposited, it it always running
215         return (deposit_) || (running_.load());
216     }
217 
218     int64_t deliveryTimeout_ = 0;
219     int64_t distributeTimeout_ = 0;
220     bool deposit_{true};
221     std::atomic<bool> running_{false};
222     std::shared_ptr<EventQueue> queue_;
223     std::shared_ptr<EventInnerRunner> innerRunner_;
224     static std::shared_ptr<EventRunner> mainRunner_;
225     std::string currentEventInfo_;
226 };
227 }  // namespace AppExecFwk
228 namespace EventHandling = AppExecFwk;
229 }  // namespace OHOS
230 
231 #endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_RUNNER_H
232