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 | |
| parent | 05b6fbb1f5b9ac2ab848a062a64f84de4e2b6ed2 (diff) | |
Advanced deferred_exec for core-side code. (#15579)
| -rw-r--r-- | quantum/deferred_exec.c | 85 | ||||
| -rw-r--r-- | quantum/deferred_exec.h | 119 |
2 files changed, 149 insertions, 55 deletions
diff --git a/quantum/deferred_exec.c b/quantum/deferred_exec.c index 5b0a5b1425..a64b451df2 100644 --- a/quantum/deferred_exec.c +++ b/quantum/deferred_exec.c | |||
| @@ -9,32 +9,27 @@ | |||
| 9 | # define MAX_DEFERRED_EXECUTORS 8 | 9 | # define MAX_DEFERRED_EXECUTORS 8 |
| 10 | #endif | 10 | #endif |
| 11 | 11 | ||
| 12 | typedef struct deferred_executor_t { | 12 | //------------------------------------ |
| 13 | deferred_token token; | 13 | // Helpers |
| 14 | uint32_t trigger_time; | 14 | // |
| 15 | deferred_exec_callback callback; | 15 | |
| 16 | void * cb_arg; | 16 | static deferred_token current_token = 0; |
| 17 | } deferred_executor_t; | 17 | |
| 18 | 18 | static inline bool token_can_be_used(deferred_executor_t *table, size_t table_count, deferred_token token) { | |
| 19 | static deferred_token current_token = 0; | ||
| 20 | static uint32_t last_deferred_exec_check = 0; | ||
| 21 | static deferred_executor_t executors[MAX_DEFERRED_EXECUTORS] = {0}; | ||
| 22 | |||
| 23 | static inline bool token_can_be_used(deferred_token token) { | ||
| 24 | if (token == INVALID_DEFERRED_TOKEN) { | 19 | if (token == INVALID_DEFERRED_TOKEN) { |
| 25 | return false; | 20 | return false; |
| 26 | } | 21 | } |
| 27 | for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) { | 22 | for (int i = 0; i < table_count; ++i) { |
| 28 | if (executors[i].token == token) { | 23 | if (table[i].token == token) { |
| 29 | return false; | 24 | return false; |
| 30 | } | 25 | } |
| 31 | } | 26 | } |
| 32 | return true; | 27 | return true; |
| 33 | } | 28 | } |
| 34 | 29 | ||
| 35 | static inline deferred_token allocate_token(void) { | 30 | static inline deferred_token allocate_token(deferred_executor_t *table, size_t table_count) { |
| 36 | deferred_token first = ++current_token; | 31 | deferred_token first = ++current_token; |
| 37 | while (!token_can_be_used(current_token)) { | 32 | while (!token_can_be_used(table, table_count, current_token)) { |
| 38 | ++current_token; | 33 | ++current_token; |
| 39 | if (current_token == first) { | 34 | if (current_token == first) { |
| 40 | // If we've looped back around to the first, everything is already allocated (yikes!). Need to exit with a failure. | 35 | // If we've looped back around to the first, everything is already allocated (yikes!). Need to exit with a failure. |
| @@ -44,18 +39,22 @@ static inline deferred_token allocate_token(void) { | |||
| 44 | return current_token; | 39 | return current_token; |
| 45 | } | 40 | } |
| 46 | 41 | ||
| 47 | deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) { | 42 | //------------------------------------ |
| 48 | // Ignore queueing if it's a zero-time delay, or invalid callback | 43 | // Advanced API: used when a custom-allocated table is used, primarily for core code. |
| 49 | if (delay_ms == 0 || !callback) { | 44 | // |
| 45 | |||
| 46 | deferred_token defer_exec_advanced(deferred_executor_t *table, size_t table_count, uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) { | ||
| 47 | // Ignore queueing if the table isn't valid, it's a zero-time delay, or the token is not valid | ||
| 48 | if (!table || table_count == 0 || delay_ms == 0 || !callback) { | ||
| 50 | return INVALID_DEFERRED_TOKEN; | 49 | return INVALID_DEFERRED_TOKEN; |
| 51 | } | 50 | } |
| 52 | 51 | ||
| 53 | // Find an unused slot and claim it | 52 | // Find an unused slot and claim it |
| 54 | for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) { | 53 | for (int i = 0; i < table_count; ++i) { |
| 55 | deferred_executor_t *entry = &executors[i]; | 54 | deferred_executor_t *entry = &table[i]; |
| 56 | if (entry->token == INVALID_DEFERRED_TOKEN) { | 55 | if (entry->token == INVALID_DEFERRED_TOKEN) { |
| 57 | // Work out the new token value, dropping out if none were available | 56 | // Work out the new token value, dropping out if none were available |
| 58 | deferred_token token = allocate_token(); | 57 | deferred_token token = allocate_token(table, table_count); |
| 59 | if (token == INVALID_DEFERRED_TOKEN) { | 58 | if (token == INVALID_DEFERRED_TOKEN) { |
| 60 | return false; | 59 | return false; |
| 61 | } | 60 | } |
| @@ -73,15 +72,15 @@ deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, vo | |||
| 73 | return INVALID_DEFERRED_TOKEN; | 72 | return INVALID_DEFERRED_TOKEN; |
| 74 | } | 73 | } |
| 75 | 74 | ||
| 76 | bool extend_deferred_exec(deferred_token token, uint32_t delay_ms) { | 75 | bool extend_deferred_exec_advanced(deferred_executor_t *table, size_t table_count, deferred_token token, uint32_t delay_ms) { |
| 77 | // Ignore queueing if it's a zero-time delay, or the token is not valid | 76 | // Ignore queueing if the table isn't valid, it's a zero-time delay, or the token is not valid |
| 78 | if (delay_ms == 0 || token == INVALID_DEFERRED_TOKEN) { | 77 | if (!table || table_count == 0 || delay_ms == 0 || token == INVALID_DEFERRED_TOKEN) { |
| 79 | return false; | 78 | return false; |
| 80 | } | 79 | } |
| 81 | 80 | ||
| 82 | // Find the entry corresponding to the token | 81 | // Find the entry corresponding to the token |
| 83 | for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) { | 82 | for (int i = 0; i < table_count; ++i) { |
| 84 | deferred_executor_t *entry = &executors[i]; | 83 | deferred_executor_t *entry = &table[i]; |
| 85 | if (entry->token == token) { | 84 | if (entry->token == token) { |
| 86 | // Found it, extend the delay | 85 | // Found it, extend the delay |
| 87 | entry->trigger_time = timer_read32() + delay_ms; | 86 | entry->trigger_time = timer_read32() + delay_ms; |
| @@ -93,15 +92,15 @@ bool extend_deferred_exec(deferred_token token, uint32_t delay_ms) { | |||
| 93 | return false; | 92 | return false; |
| 94 | } | 93 | } |
| 95 | 94 | ||
| 96 | bool cancel_deferred_exec(deferred_token token) { | 95 | bool cancel_deferred_exec_advanced(deferred_executor_t *table, size_t table_count, deferred_token token) { |
| 97 | // Ignore request if the token is not valid | 96 | // Ignore request if the table/token are not valid |
| 98 | if (token == INVALID_DEFERRED_TOKEN) { | 97 | if (!table || table_count == 0 || token == INVALID_DEFERRED_TOKEN) { |
| 99 | return false; | 98 | return false; |
| 100 | } | 99 | } |
| 101 | 100 | ||
| 102 | // Find the entry corresponding to the token | 101 | // Find the entry corresponding to the token |
| 103 | for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) { | 102 | for (int i = 0; i < table_count; ++i) { |
| 104 | deferred_executor_t *entry = &executors[i]; | 103 | deferred_executor_t *entry = &table[i]; |
| 105 | if (entry->token == token) { | 104 | if (entry->token == token) { |
| 106 | // Found it, cancel and clear the table entry | 105 | // Found it, cancel and clear the table entry |
| 107 | entry->token = INVALID_DEFERRED_TOKEN; | 106 | entry->token = INVALID_DEFERRED_TOKEN; |
| @@ -116,16 +115,16 @@ bool cancel_deferred_exec(deferred_token token) { | |||
| 116 | return false; | 115 | return false; |
| 117 | } | 116 | } |
| 118 | 117 | ||
| 119 | void deferred_exec_task(void) { | 118 | void deferred_exec_advanced_task(deferred_executor_t *table, size_t table_count, uint32_t *last_execution_time) { |
| 120 | uint32_t now = timer_read32(); | 119 | uint32_t now = timer_read32(); |
| 121 | 120 | ||
| 122 | // Throttle only once per millisecond | 121 | // Throttle only once per millisecond |
| 123 | if (((int32_t)TIMER_DIFF_32(now, last_deferred_exec_check)) > 0) { | 122 | if (((int32_t)TIMER_DIFF_32(now, (*last_execution_time))) > 0) { |
| 124 | last_deferred_exec_check = now; | 123 | *last_execution_time = now; |
| 125 | 124 | ||
| 126 | // Run through each of the executors | 125 | // Run through each of the executors |
| 127 | for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) { | 126 | for (int i = 0; i < table_count; ++i) { |
| 128 | deferred_executor_t *entry = &executors[i]; | 127 | deferred_executor_t *entry = &table[i]; |
| 129 | 128 | ||
| 130 | // Check if we're supposed to execute this entry | 129 | // Check if we're supposed to execute this entry |
| 131 | if (entry->token != INVALID_DEFERRED_TOKEN && ((int32_t)TIMER_DIFF_32(entry->trigger_time, now)) <= 0) { | 130 | if (entry->token != INVALID_DEFERRED_TOKEN && ((int32_t)TIMER_DIFF_32(entry->trigger_time, now)) <= 0) { |
| @@ -150,3 +149,15 @@ void deferred_exec_task(void) { | |||
| 150 | } | 149 | } |
| 151 | } | 150 | } |
| 152 | } | 151 | } |
| 152 | |||
| 153 | //------------------------------------ | ||
| 154 | // Basic API: used by user-mode code, guaranteed to not collide with core deferred execution | ||
| 155 | // | ||
| 156 | |||
| 157 | static uint32_t last_deferred_exec_check = 0; | ||
| 158 | static deferred_executor_t basic_executors[MAX_DEFERRED_EXECUTORS] = {0}; | ||
| 159 | |||
| 160 | deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) { return defer_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, delay_ms, callback, cb_arg); } | ||
| 161 | bool extend_deferred_exec(deferred_token token, uint32_t delay_ms) { return extend_deferred_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, token, delay_ms); } | ||
| 162 | bool cancel_deferred_exec(deferred_token token) { return cancel_deferred_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, token); } | ||
| 163 | void deferred_exec_task(void) { deferred_exec_advanced_task(basic_executors, MAX_DEFERRED_EXECUTORS, &last_deferred_exec_check); } | ||
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); | ||
