You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.7 KiB

  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <stddef.h>
  4. #include <timer.h>
  5. #include <deferred_exec.h>
  6. #ifndef MAX_DEFERRED_EXECUTORS
  7. # define MAX_DEFERRED_EXECUTORS 8
  8. #endif
  9. //------------------------------------
  10. // Helpers
  11. //
  12. static deferred_token current_token = 0;
  13. static inline bool token_can_be_used(deferred_executor_t *table, size_t table_count, deferred_token token) {
  14. if (token == INVALID_DEFERRED_TOKEN) {
  15. return false;
  16. }
  17. for (int i = 0; i < table_count; ++i) {
  18. if (table[i].token == token) {
  19. return false;
  20. }
  21. }
  22. return true;
  23. }
  24. static inline deferred_token allocate_token(deferred_executor_t *table, size_t table_count) {
  25. deferred_token first = ++current_token;
  26. while (!token_can_be_used(table, table_count, current_token)) {
  27. ++current_token;
  28. if (current_token == first) {
  29. // If we've looped back around to the first, everything is already allocated (yikes!). Need to exit with a failure.
  30. return INVALID_DEFERRED_TOKEN;
  31. }
  32. }
  33. return current_token;
  34. }
  35. //------------------------------------
  36. // Advanced API: used when a custom-allocated table is used, primarily for core code.
  37. //
  38. deferred_token defer_exec_advanced(deferred_executor_t *table, size_t table_count, uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) {
  39. // Ignore queueing if the table isn't valid, it's a zero-time delay, or the token is not valid
  40. if (!table || table_count == 0 || delay_ms == 0 || !callback) {
  41. return INVALID_DEFERRED_TOKEN;
  42. }
  43. // Find an unused slot and claim it
  44. for (int i = 0; i < table_count; ++i) {
  45. deferred_executor_t *entry = &table[i];
  46. if (entry->token == INVALID_DEFERRED_TOKEN) {
  47. // Work out the new token value, dropping out if none were available
  48. deferred_token token = allocate_token(table, table_count);
  49. if (token == INVALID_DEFERRED_TOKEN) {
  50. return false;
  51. }
  52. // Set up the executor table entry
  53. entry->token = current_token;
  54. entry->trigger_time = timer_read32() + delay_ms;
  55. entry->callback = callback;
  56. entry->cb_arg = cb_arg;
  57. return current_token;
  58. }
  59. }
  60. // None available
  61. return INVALID_DEFERRED_TOKEN;
  62. }
  63. bool extend_deferred_exec_advanced(deferred_executor_t *table, size_t table_count, deferred_token token, uint32_t delay_ms) {
  64. // Ignore queueing if the table isn't valid, it's a zero-time delay, or the token is not valid
  65. if (!table || table_count == 0 || delay_ms == 0 || token == INVALID_DEFERRED_TOKEN) {
  66. return false;
  67. }
  68. // Find the entry corresponding to the token
  69. for (int i = 0; i < table_count; ++i) {
  70. deferred_executor_t *entry = &table[i];
  71. if (entry->token == token) {
  72. // Found it, extend the delay
  73. entry->trigger_time = timer_read32() + delay_ms;
  74. return true;
  75. }
  76. }
  77. // Not found
  78. return false;
  79. }
  80. bool cancel_deferred_exec_advanced(deferred_executor_t *table, size_t table_count, deferred_token token) {
  81. // Ignore request if the table/token are not valid
  82. if (!table || table_count == 0 || token == INVALID_DEFERRED_TOKEN) {
  83. return false;
  84. }
  85. // Find the entry corresponding to the token
  86. for (int i = 0; i < table_count; ++i) {
  87. deferred_executor_t *entry = &table[i];
  88. if (entry->token == token) {
  89. // Found it, cancel and clear the table entry
  90. entry->token = INVALID_DEFERRED_TOKEN;
  91. entry->trigger_time = 0;
  92. entry->callback = NULL;
  93. entry->cb_arg = NULL;
  94. return true;
  95. }
  96. }
  97. // Not found
  98. return false;
  99. }
  100. void deferred_exec_advanced_task(deferred_executor_t *table, size_t table_count, uint32_t *last_execution_time) {
  101. uint32_t now = timer_read32();
  102. // Throttle only once per millisecond
  103. if (((int32_t)TIMER_DIFF_32(now, (*last_execution_time))) > 0) {
  104. *last_execution_time = now;
  105. // Run through each of the executors
  106. for (int i = 0; i < table_count; ++i) {
  107. deferred_executor_t *entry = &table[i];
  108. deferred_token curr_token = entry->token;
  109. // Check if we're supposed to execute this entry
  110. if (curr_token != INVALID_DEFERRED_TOKEN && ((int32_t)TIMER_DIFF_32(entry->trigger_time, now)) <= 0) {
  111. // Invoke the callback and work work out if we should be requeued
  112. uint32_t delay_ms = entry->callback(entry->trigger_time, entry->cb_arg);
  113. // If the token has changed, then the callback has canceled and re-queued. Skip further processing.
  114. if (entry->token != curr_token) {
  115. continue;
  116. }
  117. // Update the trigger time if we have to repeat, otherwise clear it out
  118. if (delay_ms > 0) {
  119. // Intentionally add just the delay to the existing trigger time -- this ensures the next
  120. // invocation is with respect to the previous trigger, rather than when it got to execution. Under
  121. // normal circumstances this won't cause issue, but if another executor is invoked that takes a
  122. // considerable length of time, then this ensures best-effort timing between invocations.
  123. entry->trigger_time += delay_ms;
  124. } else {
  125. // If it was zero, then the callback is cancelling repeated execution. Free up the slot.
  126. entry->token = INVALID_DEFERRED_TOKEN;
  127. entry->trigger_time = 0;
  128. entry->callback = NULL;
  129. entry->cb_arg = NULL;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. //------------------------------------
  136. // Basic API: used by user-mode code, guaranteed to not collide with core deferred execution
  137. //
  138. static uint32_t last_deferred_exec_check = 0;
  139. static deferred_executor_t basic_executors[MAX_DEFERRED_EXECUTORS] = {0};
  140. deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) {
  141. return defer_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, delay_ms, callback, cb_arg);
  142. }
  143. bool extend_deferred_exec(deferred_token token, uint32_t delay_ms) {
  144. return extend_deferred_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, token, delay_ms);
  145. }
  146. bool cancel_deferred_exec(deferred_token token) {
  147. return cancel_deferred_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, token);
  148. }
  149. void deferred_exec_task(void) {
  150. deferred_exec_advanced_task(basic_executors, MAX_DEFERRED_EXECUTORS, &last_deferred_exec_check);
  151. }