event_loop.h revision 65d679851a2c062c26f685afb450f17bce177d29
1e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie/* 2e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Copyright (C) 2016 The Android Open Source Project 3e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * 4e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Licensed under the Apache License, Version 2.0 (the "License"); 5e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * you may not use this file except in compliance with the License. 6e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * You may obtain a copy of the License at 7e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * 8e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * http://www.apache.org/licenses/LICENSE-2.0 9e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * 10e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Unless required by applicable law or agreed to in writing, software 11e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * distributed under the License is distributed on an "AS IS" BASIS, 12e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * See the License for the specific language governing permissions and 14e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * limitations under the License. 15e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 16e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 17e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#ifndef CHRE_CORE_EVENT_LOOP_H_ 18e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#define CHRE_CORE_EVENT_LOOP_H_ 19e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 20e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#include "chre/core/event.h" 21e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#include "chre/core/nanoapp.h" 22fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol#include "chre/core/timer_pool.h" 23977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie#include "chre/platform/mutex.h" 242de17c9c5c541d72248f8371dfb4d7e402dc93e8Andrew Rossignol#include "chre/platform/platform_nanoapp.h" 25c03d7df169f13195652de29f4452830130a0c53fAndrew Rossignol#include "chre/util/dynamic_vector.h" 26022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol#include "chre/util/fixed_size_blocking_queue.h" 27e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#include "chre/util/non_copyable.h" 28a1d5686c7513f6b8a436efd9f554d8c2227a2291Brian Duddie#include "chre/util/synchronized_memory_pool.h" 292de17c9c5c541d72248f8371dfb4d7e402dc93e8Andrew Rossignol#include "chre/util/unique_ptr.h" 3062f187d9d736346275492e916f6001576b68bb00Brian Duddie#include "chre_api/chre/event.h" 31e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 32e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddienamespace chre { 33e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 34e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie/** 35977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * The EventLoop represents a single thread of execution that is shared among 36977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * zero or more nanoapps. As the name implies, the EventLoop is built around a 37977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * loop that delivers events to the nanoapps managed within for processing. 38e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 39e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddieclass EventLoop : public NonCopyable { 40e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie public: 41fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol /** 42fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol * Setup the event loop. 43fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol */ 44fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol EventLoop(); 45fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol 46977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie /** 47a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * Synchronous callback used with forEachNanoapp 48a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie */ 49a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie typedef void (NanoappCallbackFunction)(const Nanoapp *nanoapp, void *data); 50a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie 51a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie /** 52977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * Searches the set of nanoapps managed by this EventLoop for one with the 53977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * given app ID. If found, provides its instance ID, which can be used to send 54977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * events to the app. 55977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * 56977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * This function is safe to call from any thread. 57977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * 58977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @param appId The nanoapp identifier to search for. 59977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @param instanceId If this function returns true, will be populated with the 60977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * instanceId associated with the given appId; otherwise unmodified. 61977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * Must not be null. 62977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @return true if the given app ID was found and instanceId was populated 63977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie */ 642fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie bool findNanoappInstanceIdByAppId(uint64_t appId, uint32_t *instanceId) const; 65e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 66e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie /** 67a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * Iterates over the list of Nanoapps managed by this EventLoop, and invokes 68a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * the supplied callback for each one. This holds a lock if necessary, so it 69a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * is safe to call from any thread. 70a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * 71a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * @param callback Function to invoke on each Nanoapp (synchronously) 72a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * @param data Arbitrary data to pass to the callback 73a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie */ 74a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie void forEachNanoapp(NanoappCallbackFunction *callback, void *data); 75a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie 76a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie /** 7709a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * Invokes a message to host free callback supplied by the given nanoapp 7809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * (identified by app ID). Ensures that the calling context is updated 7909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * appropriately. 8009a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * 8109a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * @param appId Identifies the nanoapp that sent this message and supplied the 8209a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * free callback 8309a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * @param freeFunction The non-null message free callback given by the nanoapp 8409a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * @param message Pointer to the message data 8509a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * @param messageSize Size of the message 8609a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie */ 8709a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie void invokeMessageFreeFunction( 8809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie uint64_t appId, chreMessageFreeFunction *freeFunction, void *message, 8909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie size_t messageSize); 9009a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie 9109a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie /** 929d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * Invokes the Nanoapp's start callback, and if successful, adds it to the 939d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * set of Nanoapps managed by this EventLoop. This function must only be 949d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * called from the context of the thread that runs this event loop (i.e. from 959d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * the same thread that will call run() or from a callback invoked within 969d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * run()). 97e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * 989d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * @param nanoapp The nanoapp that will be started. Upon success, this 999d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * UniquePtr will become invalid, as the underlying Nanoapp instance 1009d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * will have been transferred to be managed by this EventLoop. 1019d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie * @return true if the app was started successfully 102e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 1039d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie bool startNanoapp(UniquePtr<Nanoapp>& nanoapp); 104e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 105e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie /** 10699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Stops and unloads a nanoapp identified by its instance ID. The end entry 10799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * point will be invoked, and the chre::Nanoapp instance will be destroyed. 10899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * After this function returns, all references to the Nanoapp instance are 10999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * invalidated. 110e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * 11199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * @param instanceId The nanoapp's unique instance identifier 11299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * @param allowSystemNanoappUnload If false, this function will reject 11399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * attempts to unload a system nanoapp 11499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * 11599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * @return true if the nanoapp with the given instance ID was found & unloaded 116e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 11799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie bool unloadNanoapp(uint32_t instanceId, bool allowSystemNanoappUnload); 118e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 119e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie /** 120977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * Executes the loop that blocks on the event queue and delivers received 121977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * events to nanoapps. Only returns after stop() is called (from another 122977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * context). 123e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 124e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie void run(); 125e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 126e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie /** 127e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Signals the event loop currently executing in run() to exit gracefully at 128f80ea015d077330cfdbd619680b250915ffcf1d2Brian Duddie * the next available opportunity. This function is thread-safe. 129e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 130e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie void stop(); 131e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 1324927ee586656424c827920876673228fbdcf27c3Andrew Rossignol /** 1334927ee586656424c827920876673228fbdcf27c3Andrew Rossignol * Posts an event to a nanoapp that is currently running (or all nanoapps if 134042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie * the target instance ID is kBroadcastInstanceId). 135042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie * 136042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie * This function is safe to call from any thread. 1374927ee586656424c827920876673228fbdcf27c3Andrew Rossignol * 1385d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @param eventType The type of data being posted. 1395d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @param eventData The data being posted. 1405d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @param freeCallback The callback to invoke when the event is no longer 1415d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * needed. 1425d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @param senderInstanceId The instance ID of the sender of this event. 1435d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @param targetInstanceId The instance ID of the destination of this event. 1444927ee586656424c827920876673228fbdcf27c3Andrew Rossignol * 145a12d05f5b1b8bd8d953a328758426b7ca8b5e6deBrian Duddie * @return true if the event was successfully added to the queue. Note that 146a12d05f5b1b8bd8d953a328758426b7ca8b5e6deBrian Duddie * unlike chreSendEvent, this does *not* invoke the free callback if 147a12d05f5b1b8bd8d953a328758426b7ca8b5e6deBrian Duddie * it failed to post the event. 1485d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * 1495d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @see chreSendEvent 1504927ee586656424c827920876673228fbdcf27c3Andrew Rossignol */ 151042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie bool postEvent(uint16_t eventType, void *eventData, 1524927ee586656424c827920876673228fbdcf27c3Andrew Rossignol chreEventCompleteFunction *freeCallback, 1534927ee586656424c827920876673228fbdcf27c3Andrew Rossignol uint32_t senderInstanceId = kSystemInstanceId, 1544927ee586656424c827920876673228fbdcf27c3Andrew Rossignol uint32_t targetInstanceId = kBroadcastInstanceId); 155e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 156e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie /** 157e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Returns a pointer to the currently executing Nanoapp, or nullptr if none is 158977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * currently executing. Must only be called from within the thread context 159977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * associated with this EventLoop. 160e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * 161977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @return the currently executing nanoapp, or nullptr 162e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */ 1637983050d7cd84f1d5736f220ef9fd49be716b2c8Andrew Rossignol Nanoapp *getCurrentNanoapp() const; 164e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 165e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie /** 166a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * Gets the number of nanoapps currently associated with this event loop. Must 167a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * only be called within the context of this EventLoop. 168a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * 169a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie * @return The number of nanoapps managed by this event loop 170a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie */ 171a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie size_t getNanoappCount() const; 172a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie 173a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie /** 174725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol * Obtains the TimerPool associated with this event loop. 175725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol * 176725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol * @return The timer pool owned by this event loop. 177725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol */ 178725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol TimerPool& getTimerPool(); 179725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol 18030f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol /** 18130f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * Searches the set of nanoapps managed by this EventLoop for one with the 18230f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * given instance ID. 18330f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * 18430f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * This function is safe to call from any thread. 18530f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * 18630f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * @param instanceId The nanoapp instance ID to search for. 18730f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol * @return a pointer to the found nanoapp or nullptr if no match was found. 18830f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol */ 1892fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie Nanoapp *findNanoappByInstanceId(uint32_t instanceId) const; 19030f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol 19199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie /** 19262f187d9d736346275492e916f6001576b68bb00Brian Duddie * Looks for an app with the given ID and if found, populates info with its 19362f187d9d736346275492e916f6001576b68bb00Brian Duddie * metadata. Safe to call from any thread. 19462f187d9d736346275492e916f6001576b68bb00Brian Duddie * 19562f187d9d736346275492e916f6001576b68bb00Brian Duddie * @see chreGetNanoappInfoByAppId 19662f187d9d736346275492e916f6001576b68bb00Brian Duddie */ 19762f187d9d736346275492e916f6001576b68bb00Brian Duddie bool populateNanoappInfoForAppId(uint64_t appId, 19862f187d9d736346275492e916f6001576b68bb00Brian Duddie struct chreNanoappInfo *info) const; 19962f187d9d736346275492e916f6001576b68bb00Brian Duddie 20062f187d9d736346275492e916f6001576b68bb00Brian Duddie /** 20162f187d9d736346275492e916f6001576b68bb00Brian Duddie * Looks for an app with the given instance ID and if found, populates info 20262f187d9d736346275492e916f6001576b68bb00Brian Duddie * with its metadata. Safe to call from any thread. 20362f187d9d736346275492e916f6001576b68bb00Brian Duddie * 20462f187d9d736346275492e916f6001576b68bb00Brian Duddie * @see chreGetNanoappInfoByInstanceId 20562f187d9d736346275492e916f6001576b68bb00Brian Duddie */ 20662f187d9d736346275492e916f6001576b68bb00Brian Duddie bool populateNanoappInfoForInstanceId(uint32_t instanceId, 20762f187d9d736346275492e916f6001576b68bb00Brian Duddie struct chreNanoappInfo *info) const; 20862f187d9d736346275492e916f6001576b68bb00Brian Duddie 20962f187d9d736346275492e916f6001576b68bb00Brian Duddie /** 21099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * @return true if the current Nanoapp (or entire CHRE) is being unloaded, and 21199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * therefore it should not be allowed to send events or messages, etc. 21299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie */ 2132fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie bool currentNanoappIsStopping() const; 21499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie 215e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie private: 2164927ee586656424c827920876673228fbdcf27c3Andrew Rossignol //! The maximum number of events that can be active in the system. 2174927ee586656424c827920876673228fbdcf27c3Andrew Rossignol static constexpr size_t kMaxEventCount = 1024; 2184927ee586656424c827920876673228fbdcf27c3Andrew Rossignol 219022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol //! The maximum number of events that are awaiting to be scheduled. These 220022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol //! events are in a queue to be distributed to apps. 221022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol static constexpr size_t kMaxUnscheduledEventCount = 1024; 222022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol 2234927ee586656424c827920876673228fbdcf27c3Andrew Rossignol //! The memory pool to allocate incoming events from. 224f7914d0fbadf2644c0c4096380c741bee2a026e0Andrew Rossignol SynchronizedMemoryPool<Event, kMaxEventCount> mEventPool; 2254927ee586656424c827920876673228fbdcf27c3Andrew Rossignol 226fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol //! The timer used schedule timed events for tasks running in this event loop. 227fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol TimerPool mTimerPool; 228fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol 229c03d7df169f13195652de29f4452830130a0c53fAndrew Rossignol //! The list of nanoapps managed by this event loop. 2302de17c9c5c541d72248f8371dfb4d7e402dc93e8Andrew Rossignol DynamicVector<UniquePtr<Nanoapp>> mNanoapps; 231725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol 232977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie //! This lock *must* be held whenever we: 233977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie //! (1) make changes to the mNanoapps vector, or 234977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie //! (2) read the mNanoapps vector from a thread other than the one 235977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie //! associated with this EventLoop 236977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie //! It is not necessary to acquire the lock when reading mNanoapps from within 237977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie //! the thread context of this EventLoop. 2382fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie mutable Mutex mNanoappsLock; 239977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie 240c03d7df169f13195652de29f4452830130a0c53fAndrew Rossignol //! The blocking queue of incoming events from the system that have not been 24199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie //! distributed out to apps yet. 2421795db1e2282dac8454cf59d23314f70b6930f7bAndrew Rossignol FixedSizeBlockingQueue<Event *, kMaxUnscheduledEventCount> mEvents; 243e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 244977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie // TODO: should probably be atomic to be fully correct 2458c56d08e149720fcb99e6ddc71289ebb5ee594e6Brian Duddie volatile bool mRunning = true; 246e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 24799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie //! The nanoapp that is currently executing - must be set any time we call 24899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie //! into the nanoapp's entry points or callbacks 249e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie Nanoapp *mCurrentApp = nullptr; 250e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 25199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie //! Set to the nanoapp we are in the process of unloading in unloadNanoapp() 25299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie Nanoapp *mStoppingNanoapp = nullptr; 25399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie 25499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie /** 25599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Do one round of Nanoapp event delivery, only considering events in 25699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Nanoapps' own queues (not mEvents). 25799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * 25899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * @return true if there are more events pending in Nanoapps' own queues 25999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie */ 26099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie bool deliverEvents(); 26199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie 262977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie /** 2635d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * Delivers the next event pending in the Nanoapp's queue, and takes care of 2645d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * freeing events once they have been delivered to all nanoapps. Must only be 2655d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * called after confirming that the app has at least 1 pending event. 2665d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * 2675d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie * @return true if the nanoapp has another event pending in its queue 2685d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie */ 2695d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie bool deliverNextEvent(const UniquePtr<Nanoapp>& app); 2705d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie 2715d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie /** 27299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Given an event pulled from the main incoming event queue (mEvents), deliver 27399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * it to all Nanoapps that should receive the event, or free the event if 27499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * there are no valid recipients. 27599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * 27699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * @param event The Event to distribute to Nanoapps 27799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie */ 27899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie void distributeEvent(Event *event); 27999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie 28099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie /** 28199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Distribute all events pending in the inbound event queue. Note that this 28299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * function only guarantees that any events in the inbound queue at the time 28399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * it is called will be distributed to Nanoapp event queues - new events may 28499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * still be posted during or after this function call from other threads as 28599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * long as postEvent() will accept them. 28699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie */ 28799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie void flushInboundEventQueue(); 28899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie 28999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie /** 29099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Delivers events pending in Nanoapps' own queues until they are all empty. 29199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie */ 29299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie void flushNanoappEventQueues(); 29399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie 29499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie /** 295977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * Call after when an Event has been delivered to all intended recipients. 296977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * Invokes the event's free callback (if given) and releases resources. 297977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * 298977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @param event The event to be freed 299977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie */ 300e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie void freeEvent(Event *event); 301e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 302977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie /** 30309a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * Finds a Nanoapp with the given 64-bit appId. 30409a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * 3052fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie * Only safe to call within this EventLoop's thread, or if mNanoappsLock is 3062fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie * held. 30709a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * 30809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * @param appId Nanoapp ID 30909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * @return Pointer to Nanoapp instance in this EventLoop with the given app 31009a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie * ID, or nullptr if not found 31109a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie */ 3122fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie Nanoapp *lookupAppByAppId(uint64_t appId) const; 31309a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie 31409a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie /** 315977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * Finds a Nanoapp with the given instanceId. 316977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * 3172fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie * Only safe to call within this EventLoop's thread, or if mNanoappsLock is 3182fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie * held. 319977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * 320977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @param instanceId Nanoapp instance identifier 321977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * @return Nanoapp with the given instanceId, or nullptr if not found 322977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie */ 3232fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie Nanoapp *lookupAppByInstanceId(uint32_t instanceId) const; 3245d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie 3255d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie /** 32665d679851a2c062c26f685afb450f17bce177d29Brian Duddie * Sends an event with payload struct chreNanoappInfo populated from the given 32765d679851a2c062c26f685afb450f17bce177d29Brian Duddie * Nanoapp instance to inform other nanoapps about it starting/stopping. 32865d679851a2c062c26f685afb450f17bce177d29Brian Duddie * 32965d679851a2c062c26f685afb450f17bce177d29Brian Duddie * @param eventType Should be one of CHRE_EVENT_NANOAPP_{STARTED, STOPPED} 33065d679851a2c062c26f685afb450f17bce177d29Brian Duddie * @param nanoapp The nanoapp instance whose status has changed 33165d679851a2c062c26f685afb450f17bce177d29Brian Duddie */ 33265d679851a2c062c26f685afb450f17bce177d29Brian Duddie void notifyAppStatusChange(uint16_t eventType, const Nanoapp& nanoapp); 33365d679851a2c062c26f685afb450f17bce177d29Brian Duddie 33465d679851a2c062c26f685afb450f17bce177d29Brian Duddie /** 33599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * Stops and unloads the Nanoapp at the given index in mNanoapps. 33699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * 33799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * IMPORTANT: prior to calling this function, the event queues must be in a 33899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * safe condition for removal of this nanoapp. This means that there must not 33999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * be any pending events in this nanoapp's queue, and there must not be any 34099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * outstanding events sent by this nanoapp, as they may reference the 34199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie * nanoapp's own memory (even if there is no free callback). 3425d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie */ 34399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie void unloadNanoappAtIndex(size_t index); 344e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie}; 345e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 346e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie} // namespace chre 347e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie 348e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#endif // CHRE_CORE_EVENT_LOOP_H_ 349