event_loop.h revision 8c56d08e149720fcb99e6ddc71289ebb5ee594e6
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CHRE_CORE_EVENT_LOOP_H_
18#define CHRE_CORE_EVENT_LOOP_H_
19
20#include "chre/core/event.h"
21#include "chre/core/nanoapp.h"
22#include "chre/core/timer_pool.h"
23#include "chre/platform/mutex.h"
24#include "chre/platform/platform_nanoapp.h"
25#include "chre/util/dynamic_vector.h"
26#include "chre/util/fixed_size_blocking_queue.h"
27#include "chre/util/non_copyable.h"
28#include "chre/util/synchronized_memory_pool.h"
29#include "chre/util/unique_ptr.h"
30
31namespace chre {
32
33/**
34 * The EventLoop represents a single thread of execution that is shared among
35 * zero or more nanoapps. As the name implies, the EventLoop is built around a
36 * loop that delivers events to the nanoapps managed within for processing.
37 */
38class EventLoop : public NonCopyable {
39 public:
40  /**
41   * Setup the event loop.
42   */
43  EventLoop();
44
45  /**
46   * Synchronous callback used with forEachNanoapp
47   */
48  typedef void (NanoappCallbackFunction)(const Nanoapp *nanoapp, void *data);
49
50  /**
51   * Searches the set of nanoapps managed by this EventLoop for one with the
52   * given app ID. If found, provides its instance ID, which can be used to send
53   * events to the app.
54   *
55   * This function is safe to call from any thread.
56   *
57   * @param appId The nanoapp identifier to search for.
58   * @param instanceId If this function returns true, will be populated with the
59   *        instanceId associated with the given appId; otherwise unmodified.
60   *        Must not be null.
61   * @return true if the given app ID was found and instanceId was populated
62   */
63  bool findNanoappInstanceIdByAppId(uint64_t appId, uint32_t *instanceId);
64
65  /**
66   * Iterates over the list of Nanoapps managed by this EventLoop, and invokes
67   * the supplied callback for each one. This holds a lock if necessary, so it
68   * is safe to call from any thread.
69   *
70   * @param callback Function to invoke on each Nanoapp (synchronously)
71   * @param data Arbitrary data to pass to the callback
72   */
73  void forEachNanoapp(NanoappCallbackFunction *callback, void *data);
74
75  /**
76   * Invokes the Nanoapp's start callback, and if successful, adds it to the
77   * set of Nanoapps managed by this EventLoop. This function must only be
78   * called from the context of the thread that runs this event loop (i.e. from
79   * the same thread that will call run() or from a callback invoked within
80   * run()).
81   *
82   * @param nanoapp The nanoapp that will be started. Upon success, this
83   *        UniquePtr will become invalid, as the underlying Nanoapp instance
84   *        will have been transferred to be managed by this EventLoop.
85   * @return true if the app was started successfully
86   */
87  bool startNanoapp(UniquePtr<Nanoapp>& nanoapp);
88
89  /**
90   * Stops a nanoapp by invoking the stop entry point. The nanoapp passed in
91   * must have been previously started by the startNanoapp method. After this
92   * function returns, all references to the Nanoapp are invalid.
93   *
94   * @param nanoapp A pointer to the nanoapp to stop.
95   */
96  void stopNanoapp(Nanoapp *nanoapp);
97
98  /**
99   * Executes the loop that blocks on the event queue and delivers received
100   * events to nanoapps. Only returns after stop() is called (from another
101   * context).
102   */
103  void run();
104
105  /**
106   * Signals the event loop currently executing in run() to exit gracefully at
107   * the next available opportunity. This function is thread-safe.
108   */
109  void stop();
110
111  /**
112   * Posts an event to a nanoapp that is currently running (or all nanoapps if
113   * the target instance ID is kBroadcastInstanceId).
114   *
115   * This function is safe to call from any thread.
116   *
117   * @param eventType The type of data being posted.
118   * @param eventData The data being posted.
119   * @param freeCallback The callback to invoke when the event is no longer
120   *        needed.
121   * @param senderInstanceId The instance ID of the sender of this event.
122   * @param targetInstanceId The instance ID of the destination of this event.
123   *
124   * @return true if the event was successfully added to the queue. Note that
125   *         unlike chreSendEvent, this does *not* invoke the free callback if
126   *         it failed to post the event.
127   *
128   * @see chreSendEvent
129   */
130  bool postEvent(uint16_t eventType, void *eventData,
131                 chreEventCompleteFunction *freeCallback,
132                 uint32_t senderInstanceId = kSystemInstanceId,
133                 uint32_t targetInstanceId = kBroadcastInstanceId);
134
135  /**
136   * Returns a pointer to the currently executing Nanoapp, or nullptr if none is
137   * currently executing. Must only be called from within the thread context
138   * associated with this EventLoop.
139   *
140   * @return the currently executing nanoapp, or nullptr
141   */
142  Nanoapp *getCurrentNanoapp() const;
143
144  /**
145   * Gets the number of nanoapps currently associated with this event loop. Must
146   * only be called within the context of this EventLoop.
147   *
148   * @return The number of nanoapps managed by this event loop
149   */
150  size_t getNanoappCount() const;
151
152  /**
153   * Obtains the TimerPool associated with this event loop.
154   *
155   * @return The timer pool owned by this event loop.
156   */
157  TimerPool& getTimerPool();
158
159  /**
160   * Searches the set of nanoapps managed by this EventLoop for one with the
161   * given instance ID.
162   *
163   * This function is safe to call from any thread.
164   *
165   * @param instanceId The nanoapp instance ID to search for.
166   * @return a pointer to the found nanoapp or nullptr if no match was found.
167   */
168  Nanoapp *findNanoappByInstanceId(uint32_t instanceId);
169
170 private:
171  //! The maximum number of events that can be active in the system.
172  static constexpr size_t kMaxEventCount = 1024;
173
174  //! The maximum number of events that are awaiting to be scheduled. These
175  //! events are in a queue to be distributed to apps.
176  static constexpr size_t kMaxUnscheduledEventCount = 1024;
177
178  //! The memory pool to allocate incoming events from.
179  SynchronizedMemoryPool<Event, kMaxEventCount> mEventPool;
180
181  //! The timer used schedule timed events for tasks running in this event loop.
182  TimerPool mTimerPool;
183
184  //! The list of nanoapps managed by this event loop.
185  DynamicVector<UniquePtr<Nanoapp>> mNanoapps;
186
187  //! This lock *must* be held whenever we:
188  //!   (1) make changes to the mNanoapps vector, or
189  //!   (2) read the mNanoapps vector from a thread other than the one
190  //!       associated with this EventLoop
191  //! It is not necessary to acquire the lock when reading mNanoapps from within
192  //! the thread context of this EventLoop.
193  Mutex mNanoappsLock;
194
195  //! The blocking queue of incoming events from the system that have not been
196  //!  distributed out to apps yet.
197  FixedSizeBlockingQueue<Event *, kMaxUnscheduledEventCount> mEvents;
198
199  // TODO: should probably be atomic to be fully correct
200  volatile bool mRunning = true;
201
202  Nanoapp *mCurrentApp = nullptr;
203
204  /**
205   * Delivers the next event pending in the Nanoapp's queue, and takes care of
206   * freeing events once they have been delivered to all nanoapps. Must only be
207   * called after confirming that the app has at least 1 pending event.
208   *
209   * @return true if the nanoapp has another event pending in its queue
210   */
211  bool deliverNextEvent(const UniquePtr<Nanoapp>& app);
212
213  /**
214   * Call after when an Event has been delivered to all intended recipients.
215   * Invokes the event's free callback (if given) and releases resources.
216   *
217   * @param event The event to be freed
218   */
219  void freeEvent(Event *event);
220
221  /**
222   * Finds a Nanoapp with the given instanceId.
223   *
224   * Only safe to call within this EventLoop's thread.
225   *
226   * @param instanceId Nanoapp instance identifier
227   * @return Nanoapp with the given instanceId, or nullptr if not found
228   */
229  Nanoapp *lookupAppByInstanceId(uint32_t instanceId);
230
231  /**
232   * Stops the Nanoapp at the given index in mNanoapps
233   */
234  void stopNanoapp(size_t index);
235};
236
237}  // namespace chre
238
239#endif  // CHRE_CORE_EVENT_LOOP_H_
240