scheduler.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "py/runtime.h"
  28. #if MICROPY_KBD_EXCEPTION
  29. // This function may be called asynchronously at any time so only do the bare minimum.
  30. void MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(mp_keyboard_interrupt)(void) {
  31. MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
  32. MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
  33. #if MICROPY_ENABLE_SCHEDULER
  34. if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
  35. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  36. }
  37. #endif
  38. }
  39. #endif
  40. #if MICROPY_ENABLE_SCHEDULER
  41. #define IDX_MASK(i) ((i) & (MICROPY_SCHEDULER_DEPTH - 1))
  42. // This is a macro so it is guaranteed to be inlined in functions like
  43. // mp_sched_schedule that may be located in a special memory region.
  44. #define mp_sched_full() (mp_sched_num_pending() == MICROPY_SCHEDULER_DEPTH)
  45. static inline bool mp_sched_empty(void) {
  46. MP_STATIC_ASSERT(MICROPY_SCHEDULER_DEPTH <= 255); // MICROPY_SCHEDULER_DEPTH must fit in 8 bits
  47. MP_STATIC_ASSERT((IDX_MASK(MICROPY_SCHEDULER_DEPTH) == 0)); // MICROPY_SCHEDULER_DEPTH must be a power of 2
  48. return mp_sched_num_pending() == 0;
  49. }
  50. // A variant of this is inlined in the VM at the pending exception check
  51. void mp_handle_pending(bool raise_exc) {
  52. if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
  53. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  54. // Re-check state is still pending now that we're in the atomic section.
  55. if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
  56. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  57. if (obj != MP_OBJ_NULL) {
  58. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  59. if (!mp_sched_num_pending()) {
  60. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  61. }
  62. if (raise_exc) {
  63. MICROPY_END_ATOMIC_SECTION(atomic_state);
  64. nlr_raise(obj);
  65. }
  66. }
  67. mp_handle_pending_tail(atomic_state);
  68. } else {
  69. MICROPY_END_ATOMIC_SECTION(atomic_state);
  70. }
  71. }
  72. }
  73. // This function should only be called by mp_handle_pending,
  74. // or by the VM's inlined version of that function.
  75. void mp_handle_pending_tail(mp_uint_t atomic_state) {
  76. MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
  77. if (!mp_sched_empty()) {
  78. mp_sched_item_t item = MP_STATE_VM(sched_queue)[MP_STATE_VM(sched_idx)];
  79. MP_STATE_VM(sched_idx) = IDX_MASK(MP_STATE_VM(sched_idx) + 1);
  80. --MP_STATE_VM(sched_len);
  81. MICROPY_END_ATOMIC_SECTION(atomic_state);
  82. mp_call_function_1_protected(item.func, item.arg);
  83. } else {
  84. MICROPY_END_ATOMIC_SECTION(atomic_state);
  85. }
  86. mp_sched_unlock();
  87. }
  88. void mp_sched_lock(void) {
  89. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  90. if (MP_STATE_VM(sched_state) < 0) {
  91. --MP_STATE_VM(sched_state);
  92. } else {
  93. MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
  94. }
  95. MICROPY_END_ATOMIC_SECTION(atomic_state);
  96. }
  97. void mp_sched_unlock(void) {
  98. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  99. assert(MP_STATE_VM(sched_state) < 0);
  100. if (++MP_STATE_VM(sched_state) == 0) {
  101. // vm became unlocked
  102. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL || mp_sched_num_pending()) {
  103. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  104. } else {
  105. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  106. }
  107. }
  108. MICROPY_END_ATOMIC_SECTION(atomic_state);
  109. }
  110. bool MICROPY_WRAP_MP_SCHED_SCHEDULE(mp_sched_schedule)(mp_obj_t function, mp_obj_t arg) {
  111. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  112. bool ret;
  113. if (!mp_sched_full()) {
  114. if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
  115. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  116. }
  117. uint8_t iput = IDX_MASK(MP_STATE_VM(sched_idx) + MP_STATE_VM(sched_len)++);
  118. MP_STATE_VM(sched_queue)[iput].func = function;
  119. MP_STATE_VM(sched_queue)[iput].arg = arg;
  120. ret = true;
  121. } else {
  122. // schedule queue is full
  123. ret = false;
  124. }
  125. MICROPY_END_ATOMIC_SECTION(atomic_state);
  126. return ret;
  127. }
  128. #else // MICROPY_ENABLE_SCHEDULER
  129. // A variant of this is inlined in the VM at the pending exception check
  130. void mp_handle_pending(bool raise_exc) {
  131. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
  132. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  133. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  134. if (raise_exc) {
  135. nlr_raise(obj);
  136. }
  137. }
  138. }
  139. #endif // MICROPY_ENABLE_SCHEDULER