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.

171 lines
6.5 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. // Check if we're supposed to execute this entry
  109. if (entry->token != INVALID_DEFERRED_TOKEN && ((int32_t)TIMER_DIFF_32(entry->trigger_time, now)) <= 0) {
  110. // Invoke the callback and work work out if we should be requeued
  111. uint32_t delay_ms = entry->callback(entry->trigger_time, entry->cb_arg);
  112. // Update the trigger time if we have to repeat, otherwise clear it out
  113. if (delay_ms > 0) {
  114. // Intentionally add just the delay to the existing trigger time -- this ensures the next
  115. // invocation is with respect to the previous trigger, rather than when it got to execution. Under
  116. // normal circumstances this won't cause issue, but if another executor is invoked that takes a
  117. // considerable length of time, then this ensures best-effort timing between invocations.
  118. entry->trigger_time += delay_ms;
  119. } else {
  120. // If it was zero, then the callback is cancelling repeated execution. Free up the slot.
  121. entry->token = INVALID_DEFERRED_TOKEN;
  122. entry->trigger_time = 0;
  123. entry->callback = NULL;
  124. entry->cb_arg = NULL;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. //------------------------------------
  131. // Basic API: used by user-mode code, guaranteed to not collide with core deferred execution
  132. //
  133. static uint32_t last_deferred_exec_check = 0;
  134. static deferred_executor_t basic_executors[MAX_DEFERRED_EXECUTORS] = {0};
  135. deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) {
  136. return defer_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, delay_ms, callback, cb_arg);
  137. }
  138. bool extend_deferred_exec(deferred_token token, uint32_t delay_ms) {
  139. return extend_deferred_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, token, delay_ms);
  140. }
  141. bool cancel_deferred_exec(deferred_token token) {
  142. return cancel_deferred_exec_advanced(basic_executors, MAX_DEFERRED_EXECUTORS, token);
  143. }
  144. void deferred_exec_task(void) {
  145. deferred_exec_advanced_task(basic_executors, MAX_DEFERRED_EXECUTORS, &last_deferred_exec_check);
  146. }