unleashed-firmware/furi/core/event_loop.h
Georgii Surkov 139660d206
[FL-3846] Event Loop Timers (#3721)
* Implement POC event loop tmers (not all edge cases are handled)
* Use a separate ready list to allow for (re)starting and stopping of timers from callback
* Improve the test application
* Improve timer API and test application
* Improve timeout calculation logic
* Improve timer API, update documentation
* Fix API usage error
* Update doxygen comments
* Revert the old (correct) check
* Improve function naming
* Check whether a timer was on the expired list before processing it
* Implement tick callback
* Add critical sections to improve timer consistency
* Simplify event loop timer API
* Remove redundant search
* Refactor timer logic, use message queue
* Simplify FuriEventLoopTimer API
* Improve event loop timer logic
* Update the f18 target
* Remove superfluous clears
* Correct f18 api symbols
* Fix doxygen comments
* Update .pvsconfig
* Use a double push list instead of deque
* Update .pvsconfig
* Add pending callback functionality
* Restore unprocessed flags when applicable
* Refactor Dolphin app to use FuriEventLoop
* Improve naming
* Update naming some more
* Fix a typo Co-authored-by: Silent <CookiePLMonster@users.noreply.github.com>
* Fix wait time in example
* Bump API version
* Debug: multiple of 25 timings in event loop blink test
* Separate FuriEventLoopTimer to its own set of files
* Improve start time calculation for periodic timers
* Do not use dynamic allocations for timer requests
* Split the tick functionality in separate files, rearrange code
* Improve timer queue handling
* Properly reset GPIO pins in the test app
* Properly initialise GPIO pins in the test app too
* Furi: variable naming in event loop
* Furi: fix spelling in event loop

Co-authored-by: あく <alleteam@gmail.com>
Co-authored-by: Silent <CookiePLMonster@users.noreply.github.com>
2024-07-02 13:09:50 +01:00

162 lines
4.9 KiB
C

/**
* @file event_loop.h
* @brief Furi Event Loop
*
* This module is designed to handle application event loop in fully
* asynchronous, reactive nature. On the low level this modules is
* inspired by epoll/kqueue concept, on the high level by asyncio
* event loop.
*
* This module is trying to best fit into Furi OS, so we don't
* provide any compatibility with other event driven APIs. But
* programming concepts are the same, except some runtime
* limitations from our side.
*/
#pragma once
#include "base.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Event Loop events */
typedef enum {
FuriEventLoopEventOut, /**< On departure: item was retrieved from container, flag reset, etc... */
FuriEventLoopEventIn, /**< On arrival: item was inserted into container, flag set, etc... */
} FuriEventLoopEvent;
/** Anonymous message queue type */
typedef struct FuriEventLoop FuriEventLoop;
/** Allocate Event Loop instance
*
* Couple things to keep in mind:
* - You can have 1 event_loop per 1 thread
* - You can not use event_loop instance in the other thread
* - Do not use blocking API to query object delegated to Event Loop
*
* @return The Event Loop instance
*/
FuriEventLoop* furi_event_loop_alloc(void);
/** Free Event Loop instance
*
* @param instance The Event Loop instance
*/
void furi_event_loop_free(FuriEventLoop* instance);
/** Continuously poll for events
*
* Can be stopped with `furi_event_loop_stop`
*
* @param instance The Event Loop instance
*/
void furi_event_loop_run(FuriEventLoop* instance);
/** Stop Event Loop instance
*
* @param instance The Event Loop instance
*/
void furi_event_loop_stop(FuriEventLoop* instance);
/*
* Tick related API
*/
/** Tick callback type
*
* @param context The context for callback
*/
typedef void (*FuriEventLoopTickCallback)(void* context);
/** Set Event Loop tick callback
*
* Tick callback is called periodically after specified inactivity time.
* It acts like a low-priority timer: it will only fire if there is time
* left after processing the synchronization primitives and the regular timers.
* Therefore, it is not monotonic: ticks will be skipped if the event loop is busy.
*
* @param instance The Event Loop instance
* @param[in] interval The tick interval
* @param[in] callback The callback to call
* @param context The context for callback
*/
void furi_event_loop_tick_set(
FuriEventLoop* instance,
uint32_t interval,
FuriEventLoopTickCallback callback,
void* context);
/*
* Deferred function call API
*/
/**
* @brief Timer callback type for functions to be called in a deferred manner.
*
* @param[in,out] context pointer to a user-specific object that was provided during
* furi_event_loop_pend_callback() call
*/
typedef void (*FuriEventLoopPendingCallback)(void* context);
/**
* @brief Call a function when all preceding timer commands are processed
*
* This function may be useful to call another function when the event loop has been started.
*
* @param[in,out] instance pointer to the current FuriEventLoop instance
* @param[in] callback pointer to the callback to be executed when previous commands have been processed
* @param[in,out] context pointer to a user-specific object (will be passed to the callback)
*/
void furi_event_loop_pend_callback(
FuriEventLoop* instance,
FuriEventLoopPendingCallback callback,
void* context);
/*
* Message queue related APIs
*/
/** Anonymous message queue type */
typedef struct FuriMessageQueue FuriMessageQueue;
/** Callback type for message queue
*
* @param queue The queue that triggered event
* @param context The context that was provided on
* furi_event_loop_message_queue_subscribe call
*
* @return true if event was processed, false if we need to delay processing
*/
typedef bool (*FuriEventLoopMessageQueueCallback)(FuriMessageQueue* queue, void* context);
/** Subscribe to message queue events
*
* @warning you can only have one subscription for one event type.
*
* @param instance The Event Loop instance
* @param message_queue The message queue to add
* @param[in] event The Event Loop event to trigger on
* @param[in] callback The callback to call on event
* @param context The context for callback
*/
void furi_event_loop_message_queue_subscribe(
FuriEventLoop* instance,
FuriMessageQueue* message_queue,
FuriEventLoopEvent event,
FuriEventLoopMessageQueueCallback callback,
void* context);
/** Unsubscribe from message queue
*
* @param instance The Event Loop instance
* @param message_queue The message queue
*/
void furi_event_loop_message_queue_unsubscribe(
FuriEventLoop* instance,
FuriMessageQueue* message_queue);
#ifdef __cplusplus
}
#endif