• 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      * Check whether the current thread is the worker thread of this EventRunner.
143      *
144      * @return Returns true if the current thread is the worker thread of this EventRunner; returns false otherwise.
145      */
146     bool IsCurrentRunnerThread();
147 
148     /**
149      * Set the distribution standard expiration time.
150      *
151      * @param deliveryTimeout the distribution standard expiration time.
152      */
SetDeliveryTimeout(int64_t deliveryTimeout)153     void SetDeliveryTimeout(int64_t deliveryTimeout)
154     {
155         deliveryTimeout_ = deliveryTimeout;
156     }
157 
158     /**
159      * Get the distribution standard expiration time.
160      *
161      * @return the distribution standard expiration time.
162      */
GetDeliveryTimeout()163     int64_t GetDeliveryTimeout() const
164     {
165         return deliveryTimeout_;
166     }
167 
168     /**
169      * Set the execution standard timeout period.
170      *
171      * @param distributeTimeout the distribution standard expiration time.
172      */
SetDistributeTimeout(int64_t distributeTimeout)173     void SetDistributeTimeout(int64_t distributeTimeout)
174     {
175         distributeTimeout_ = distributeTimeout;
176     }
177 
178     /**
179      * Get the execution standard timeout period.
180      *
181      * @return the distribution standard expiration time.
182      */
GetDistributeTimeout()183     int64_t GetDistributeTimeout() const
184     {
185         return distributeTimeout_;
186     }
187 
188     /**
189      * Obtains the EventRunner for the main thread of the application.
190      *
191      * @return Returns the EventRunner for the main thread of the application.
192      */
193     static std::shared_ptr<EventRunner> GetMainEventRunner();
194 
195 private:
196     explicit EventRunner(bool deposit);
197 
198     friend class EventHandler;
199 
200     /**
201      * Check whether this event runner is running.
202      *
203      * @return if this event runner is running return true otherwise return false
204      */
IsRunning()205     inline bool IsRunning() const
206     {
207         // If this runner is deposited, it it always running
208         return (deposit_) || (running_.load());
209     }
210 
211     int64_t deliveryTimeout_ = 0;
212     int64_t distributeTimeout_ = 0;
213     bool deposit_{true};
214     std::atomic<bool> running_{false};
215     std::shared_ptr<EventQueue> queue_;
216     std::shared_ptr<EventInnerRunner> innerRunner_;
217     static std::shared_ptr<EventRunner> mainRunner_;
218 };
219 }  // namespace AppExecFwk
220 namespace EventHandling = AppExecFwk;
221 }  // namespace OHOS
222 
223 #endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_RUNNER_H
224