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.

152 lines
5.3 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. typedef struct deferred_executor_t {
  10. deferred_token token;
  11. uint32_t trigger_time;
  12. deferred_exec_callback callback;
  13. void * cb_arg;
  14. } deferred_executor_t;
  15. static deferred_token current_token = 0;
  16. static uint32_t last_deferred_exec_check = 0;
  17. static deferred_executor_t executors[MAX_DEFERRED_EXECUTORS] = {0};
  18. static inline bool token_can_be_used(deferred_token token) {
  19. if (token == INVALID_DEFERRED_TOKEN) {
  20. return false;
  21. }
  22. for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) {
  23. if (executors[i].token == token) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. static inline deferred_token allocate_token(void) {
  30. deferred_token first = ++current_token;
  31. while (!token_can_be_used(current_token)) {
  32. ++current_token;
  33. if (current_token == first) {
  34. // If we've looped back around to the first, everything is already allocated (yikes!). Need to exit with a failure.
  35. return INVALID_DEFERRED_TOKEN;
  36. }
  37. }
  38. return current_token;
  39. }
  40. deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg) {
  41. // Ignore queueing if it's a zero-time delay, or invalid callback
  42. if (delay_ms == 0 || !callback) {
  43. return INVALID_DEFERRED_TOKEN;
  44. }
  45. // Find an unused slot and claim it
  46. for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) {
  47. deferred_executor_t *entry = &executors[i];
  48. if (entry->token == INVALID_DEFERRED_TOKEN) {
  49. // Work out the new token value, dropping out if none were available
  50. deferred_token token = allocate_token();
  51. if (token == INVALID_DEFERRED_TOKEN) {
  52. return false;
  53. }
  54. // Set up the executor table entry
  55. entry->token = current_token;
  56. entry->trigger_time = timer_read32() + delay_ms;
  57. entry->callback = callback;
  58. entry->cb_arg = cb_arg;
  59. return current_token;
  60. }
  61. }
  62. // None available
  63. return INVALID_DEFERRED_TOKEN;
  64. }
  65. bool extend_deferred_exec(deferred_token token, uint32_t delay_ms) {
  66. // Ignore queueing if it's a zero-time delay, or the token is not valid
  67. if (delay_ms == 0 || token == INVALID_DEFERRED_TOKEN) {
  68. return false;
  69. }
  70. // Find the entry corresponding to the token
  71. for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) {
  72. deferred_executor_t *entry = &executors[i];
  73. if (entry->token == token) {
  74. // Found it, extend the delay
  75. entry->trigger_time = timer_read32() + delay_ms;
  76. return true;
  77. }
  78. }
  79. // Not found
  80. return false;
  81. }
  82. bool cancel_deferred_exec(deferred_token token) {
  83. // Ignore request if the token is not valid
  84. if (token == INVALID_DEFERRED_TOKEN) {
  85. return false;
  86. }
  87. // Find the entry corresponding to the token
  88. for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) {
  89. deferred_executor_t *entry = &executors[i];
  90. if (entry->token == token) {
  91. // Found it, cancel and clear the table entry
  92. entry->token = INVALID_DEFERRED_TOKEN;
  93. entry->trigger_time = 0;
  94. entry->callback = NULL;
  95. entry->cb_arg = NULL;
  96. return true;
  97. }
  98. }
  99. // Not found
  100. return false;
  101. }
  102. void deferred_exec_task(void) {
  103. uint32_t now = timer_read32();
  104. // Throttle only once per millisecond
  105. if (((int32_t)TIMER_DIFF_32(now, last_deferred_exec_check)) > 0) {
  106. last_deferred_exec_check = now;
  107. // Run through each of the executors
  108. for (int i = 0; i < MAX_DEFERRED_EXECUTORS; ++i) {
  109. deferred_executor_t *entry = &executors[i];
  110. // Check if we're supposed to execute this entry
  111. if (entry->token != INVALID_DEFERRED_TOKEN && ((int32_t)TIMER_DIFF_32(entry->trigger_time, now)) <= 0) {
  112. // Invoke the callback and work work out if we should be requeued
  113. uint32_t delay_ms = entry->callback(entry->trigger_time, entry->cb_arg);
  114. // Update the trigger time if we have to repeat, otherwise clear it out
  115. if (delay_ms > 0) {
  116. // Intentionally add just the delay to the existing trigger time -- this ensures the next
  117. // invocation is with respect to the previous trigger, rather than when it got to execution. Under
  118. // normal circumstances this won't cause issue, but if another executor is invoked that takes a
  119. // considerable length of time, then this ensures best-effort timing between invocations.
  120. entry->trigger_time += delay_ms;
  121. } else {
  122. // If it was zero, then the callback is cancelling repeated execution. Free up the slot.
  123. entry->token = INVALID_DEFERRED_TOKEN;
  124. entry->trigger_time = 0;
  125. entry->callback = NULL;
  126. entry->cb_arg = NULL;
  127. }
  128. }
  129. }
  130. }
  131. }