mpy_scheduler.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_ENABLE_SCHEDULER
  29. #define IDX_MASK(i) ((i) & (MICROPY_SCHEDULER_DEPTH - 1))
  30. static inline bool mp_sched_full(void) {
  31. MP_STATIC_ASSERT(MICROPY_SCHEDULER_DEPTH <= 255); // MICROPY_SCHEDULER_DEPTH must fit in 8 bits
  32. MP_STATIC_ASSERT((IDX_MASK(MICROPY_SCHEDULER_DEPTH) == 0)); // MICROPY_SCHEDULER_DEPTH must be a power of 2
  33. return mp_sched_num_pending() == MICROPY_SCHEDULER_DEPTH;
  34. }
  35. static inline bool mp_sched_empty(void) {
  36. return mp_sched_num_pending() == 0;
  37. }
  38. // A variant of this is inlined in the VM at the pending exception check
  39. void mp_handle_pending(void) {
  40. if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
  41. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  42. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  43. if (obj != MP_OBJ_NULL) {
  44. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  45. if (!mp_sched_num_pending()) {
  46. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  47. }
  48. MICROPY_END_ATOMIC_SECTION(atomic_state);
  49. nlr_raise(obj);
  50. }
  51. mp_handle_pending_tail(atomic_state);
  52. }
  53. }
  54. // This function should only be called be mp_sched_handle_pending,
  55. // or by the VM's inlined version of that function.
  56. void mp_handle_pending_tail(mp_uint_t atomic_state) {
  57. MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
  58. if (!mp_sched_empty()) {
  59. mp_sched_item_t item = MP_STATE_VM(sched_queue)[MP_STATE_VM(sched_idx)];
  60. MP_STATE_VM(sched_idx) = IDX_MASK(MP_STATE_VM(sched_idx) + 1);
  61. --MP_STATE_VM(sched_len);
  62. MICROPY_END_ATOMIC_SECTION(atomic_state);
  63. mp_call_function_1_protected(item.func, item.arg);
  64. } else {
  65. MICROPY_END_ATOMIC_SECTION(atomic_state);
  66. }
  67. mp_sched_unlock();
  68. }
  69. void mp_sched_lock(void) {
  70. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  71. if (MP_STATE_VM(sched_state) < 0) {
  72. --MP_STATE_VM(sched_state);
  73. } else {
  74. MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
  75. }
  76. MICROPY_END_ATOMIC_SECTION(atomic_state);
  77. }
  78. void mp_sched_unlock(void) {
  79. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  80. if (++MP_STATE_VM(sched_state) == 0) {
  81. // vm became unlocked
  82. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL || mp_sched_num_pending()) {
  83. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  84. } else {
  85. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  86. }
  87. }
  88. MICROPY_END_ATOMIC_SECTION(atomic_state);
  89. }
  90. bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg) {
  91. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  92. bool ret;
  93. if (!mp_sched_full()) {
  94. if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
  95. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  96. }
  97. uint8_t iput = IDX_MASK(MP_STATE_VM(sched_idx) + MP_STATE_VM(sched_len)++);
  98. MP_STATE_VM(sched_queue)[iput].func = function;
  99. MP_STATE_VM(sched_queue)[iput].arg = arg;
  100. ret = true;
  101. } else {
  102. // schedule queue is full
  103. ret = false;
  104. }
  105. MICROPY_END_ATOMIC_SECTION(atomic_state);
  106. return ret;
  107. }
  108. #else // MICROPY_ENABLE_SCHEDULER
  109. // A variant of this is inlined in the VM at the pending exception check
  110. void mp_handle_pending(void) {
  111. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
  112. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  113. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  114. nlr_raise(obj);
  115. }
  116. }
  117. #endif // MICROPY_ENABLE_SCHEDULER