diff options
| author | Nick Brassel <nick@tzarc.org> | 2022-01-11 05:26:46 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-10 18:26:46 +0000 |
| commit | 0c402157fc8f586e443468e61ca94ce01a9a0ea4 (patch) | |
| tree | 65fb2581bbdc5dbba3a3acc545f9c7877d33c10d /quantum/deferred_exec.h | |
| parent | 05b6fbb1f5b9ac2ab848a062a64f84de4e2b6ed2 (diff) | |
Advanced deferred_exec for core-side code. (#15579)
Diffstat (limited to 'quantum/deferred_exec.h')
| -rw-r--r-- | quantum/deferred_exec.h | 119 |
1 files changed, 101 insertions, 18 deletions
diff --git a/quantum/deferred_exec.h b/quantum/deferred_exec.h index f80d353169..97ef0f6c0e 100644 --- a/quantum/deferred_exec.h +++ b/quantum/deferred_exec.h | |||
| @@ -5,34 +5,117 @@ | |||
| 5 | 5 | ||
| 6 | #include <stdbool.h> | 6 | #include <stdbool.h> |
| 7 | #include <stdint.h> | 7 | #include <stdint.h> |
| 8 | #include <stdlib.h> | ||
| 8 | 9 | ||
| 9 | // A token that can be used to cancel an existing deferred execution. | 10 | //------------------------------------ |
| 11 | // Common | ||
| 12 | //------------------------------------ | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @typedef A token that can be used to cancel or extend an existing deferred execution. | ||
| 16 | */ | ||
| 10 | typedef uint8_t deferred_token; | 17 | typedef uint8_t deferred_token; |
| 18 | |||
| 19 | /** | ||
| 20 | * @def The constant used to denote an invalid deferred execution token. | ||
| 21 | */ | ||
| 11 | #define INVALID_DEFERRED_TOKEN 0 | 22 | #define INVALID_DEFERRED_TOKEN 0 |
| 12 | 23 | ||
| 13 | // Callback to execute. | 24 | /** |
| 14 | // -- Parameter trigger_time: the intended trigger time to execute the callback -- equivalent time-space as timer_read32() | 25 | * @typedef Callback to execute. |
| 15 | // cb_arg: the callback argument specified when enqueueing the deferred executor | 26 | * @param trigger_time[in] the intended trigger time to execute the callback -- equivalent time-space as timer_read32() |
| 16 | // -- Return value: Non-zero re-queues the callback to execute after the returned number of milliseconds. Zero cancels repeated execution. | 27 | * @param cb_arg[in] the callback argument specified when enqueueing the deferred executor |
| 28 | * @return non-zero re-queues the callback to execute after the returned number of milliseconds. Zero cancels repeated execution. | ||
| 29 | */ | ||
| 17 | typedef uint32_t (*deferred_exec_callback)(uint32_t trigger_time, void *cb_arg); | 30 | typedef uint32_t (*deferred_exec_callback)(uint32_t trigger_time, void *cb_arg); |
| 18 | 31 | ||
| 19 | // Configures the supplied deferred executor to be executed after the required number of milliseconds. | 32 | //------------------------------------ |
| 20 | // -- Parameter delay_ms: the number of milliseconds before executing the callback | 33 | // Basic API: used by user-mode code, guaranteed to not collide with core deferred execution |
| 21 | // -- callback: the executor to invoke | 34 | //------------------------------------ |
| 22 | // -- cb_arg: the argument to pass to the executor, may be NULL if unused by the executor | 35 | |
| 23 | // -- Return value: a token usable for cancellation, or INVALID_DEFERRED_TOKEN if an error occurred | 36 | /** |
| 37 | * Configures the supplied deferred executor to be executed after the required number of milliseconds. | ||
| 38 | * | ||
| 39 | * @param delay_ms[in] the number of milliseconds before executing the callback | ||
| 40 | * @param callback[in] the executor to invoke | ||
| 41 | * @param cb_arg[in] the argument to pass to the executor, may be NULL if unused by the executor | ||
| 42 | * @return a token usable for extension/cancellation, or INVALID_DEFERRED_TOKEN if an error occurred | ||
| 43 | */ | ||
| 24 | deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg); | 44 | deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg); |
| 25 | 45 | ||
| 26 | // Allows for extending the timeframe before an existing deferred execution is invoked. | 46 | /** |
| 27 | // -- Parameter token: the returned value from defer_exec for the deferred execution you wish to extend. | 47 | * Allows for extending the timeframe before an existing deferred execution is invoked. |
| 28 | // -- delay_ms: the new delay (with respect to the current time) | 48 | * |
| 29 | // -- Return value: if the token was found, and the delay was extended | 49 | * @param token[in] the returned value from defer_exec for the deferred execution you wish to extend |
| 50 | * @param delay_ms[in] the number of milliseconds before executing the callback | ||
| 51 | * @return true if the token was extended successfully, otherwise false | ||
| 52 | */ | ||
| 30 | bool extend_deferred_exec(deferred_token token, uint32_t delay_ms); | 53 | bool extend_deferred_exec(deferred_token token, uint32_t delay_ms); |
| 31 | 54 | ||
| 32 | // Allows for cancellation of an existing deferred execution. | 55 | /** |
| 33 | // -- Parameter token: the returned value from defer_exec for the deferred execution you wish to cancel. | 56 | * Allows for cancellation of an existing deferred execution. |
| 34 | // -- Return value: if the token was found, and the executor was cancelled | 57 | * |
| 58 | * @param token[in] the returned value from defer_exec for the deferred execution you wish to cancel | ||
| 59 | * @return true if the token was cancelled successfully, otherwise false | ||
| 60 | */ | ||
| 35 | bool cancel_deferred_exec(deferred_token token); | 61 | bool cancel_deferred_exec(deferred_token token); |
| 36 | 62 | ||
| 37 | // Forward declaration for the main loop in order to execute any deferred executors. Should not be invoked by keyboard/user code. | 63 | /** |
| 64 | * Forward declaration for the main loop in order to execute any deferred executors. Should not be invoked by keyboard/user code. | ||
| 65 | */ | ||
| 38 | void deferred_exec_task(void); | 66 | void deferred_exec_task(void); |
| 67 | |||
| 68 | //------------------------------------ | ||
| 69 | // Advanced API: used when a custom-allocated table is used, primarily for core code. | ||
| 70 | //------------------------------------ | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @struct Structure for containing self-hosted deferred executor tables. | ||
| 74 | * @brief Core-side code can use this to create their own tables without impacting on the use of users' ability to add deferred execution. | ||
| 75 | * Code outside deferred_exec.c should not worry about internals of this struct, and should just allocate the required number in an array. | ||
| 76 | */ | ||
| 77 | typedef struct deferred_executor_t { | ||
| 78 | deferred_token token; | ||
| 79 | uint32_t trigger_time; | ||
| 80 | deferred_exec_callback callback; | ||
| 81 | void * cb_arg; | ||
| 82 | } deferred_executor_t; | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Configures the supplied deferred executor to be executed after the required number of milliseconds. | ||
| 86 | * | ||
| 87 | * @param table[in] the custom table used for storage | ||
| 88 | * @param table_count[in] the number of available items in the table | ||
| 89 | * @param delay_ms[in] the number of milliseconds before executing the callback | ||
| 90 | * @param callback[in] the executor to invoke | ||
| 91 | * @param cb_arg[in] the argument to pass to the executor, may be NULL if unused by the executor | ||
| 92 | * @return a token usable for extension/cancellation, or INVALID_DEFERRED_TOKEN if an error occurred | ||
| 93 | */ | ||
| 94 | deferred_token defer_exec_advanced(deferred_executor_t *table, size_t table_count, uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg); | ||
| 95 | |||
| 96 | /** | ||
| 97 | * Allows for extending the timeframe before an existing deferred execution is invoked. | ||
| 98 | * | ||
| 99 | * @param token[in] the returned value from defer_exec for the deferred execution you wish to extend | ||
| 100 | * @param delay_ms[in] the number of milliseconds before executing the callback | ||
| 101 | * @return true if the token was extended successfully, otherwise false | ||
| 102 | */ | ||
| 103 | bool extend_deferred_exec_advanced(deferred_executor_t *table, size_t table_count, deferred_token token, uint32_t delay_ms); | ||
| 104 | |||
| 105 | /** | ||
| 106 | * Allows for cancellation of an existing deferred execution. | ||
| 107 | * | ||
| 108 | * @param token[in] the returned value from defer_exec for the deferred execution you wish to cancel | ||
| 109 | * @return true if the token was cancelled successfully, otherwise false | ||
| 110 | */ | ||
| 111 | bool cancel_deferred_exec_advanced(deferred_executor_t *table, size_t table_count, deferred_token token); | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Forward declaration for the main loop in order to execute any custom table deferred executors. Should not be invoked by keyboard/user code. | ||
| 115 | * Needed for any custom-allocated deferred execution tables. Any core tasks should add appropriate invocation to quantum/main.c. | ||
| 116 | * | ||
| 117 | * @param table[in] the custom table used for storage | ||
| 118 | * @param table_count[in] the number of available items in the table | ||
| 119 | * @param last_execution_time[in,out] the last execution time -- this will be checked first to determine if execution is needed, and updated if execution occurred | ||
| 120 | */ | ||
| 121 | void deferred_exec_advanced_task(deferred_executor_t *table, size_t table_count, uint32_t *last_execution_time); | ||
