scheduler.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // A variant of this is inlined in the VM at the pending exception check
  30. void mp_handle_pending(void) {
  31. if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
  32. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  33. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  34. if (obj != MP_OBJ_NULL) {
  35. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  36. if (!mp_sched_num_pending()) {
  37. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  38. }
  39. MICROPY_END_ATOMIC_SECTION(atomic_state);
  40. nlr_raise(obj);
  41. }
  42. mp_handle_pending_tail(atomic_state);
  43. }
  44. }
  45. // This function should only be called be mp_sched_handle_pending,
  46. // or by the VM's inlined version of that function.
  47. void mp_handle_pending_tail(mp_uint_t atomic_state) {
  48. MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
  49. if (MP_STATE_VM(sched_sp) > 0) {
  50. mp_sched_item_t item = MP_STATE_VM(sched_stack)[--MP_STATE_VM(sched_sp)];
  51. MICROPY_END_ATOMIC_SECTION(atomic_state);
  52. mp_call_function_1_protected(item.func, item.arg);
  53. } else {
  54. MICROPY_END_ATOMIC_SECTION(atomic_state);
  55. }
  56. mp_sched_unlock();
  57. }
  58. void mp_sched_lock(void) {
  59. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  60. if (MP_STATE_VM(sched_state) < 0) {
  61. --MP_STATE_VM(sched_state);
  62. } else {
  63. MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
  64. }
  65. MICROPY_END_ATOMIC_SECTION(atomic_state);
  66. }
  67. void mp_sched_unlock(void) {
  68. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  69. if (++MP_STATE_VM(sched_state) == 0) {
  70. // vm became unlocked
  71. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL || mp_sched_num_pending()) {
  72. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  73. } else {
  74. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  75. }
  76. }
  77. MICROPY_END_ATOMIC_SECTION(atomic_state);
  78. }
  79. bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg) {
  80. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  81. bool ret;
  82. if (MP_STATE_VM(sched_sp) < MICROPY_SCHEDULER_DEPTH) {
  83. if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
  84. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  85. }
  86. MP_STATE_VM(sched_stack)[MP_STATE_VM(sched_sp)].func = function;
  87. MP_STATE_VM(sched_stack)[MP_STATE_VM(sched_sp)].arg = arg;
  88. ++MP_STATE_VM(sched_sp);
  89. ret = true;
  90. } else {
  91. // schedule stack is full
  92. ret = false;
  93. }
  94. MICROPY_END_ATOMIC_SECTION(atomic_state);
  95. return ret;
  96. }
  97. #else // MICROPY_ENABLE_SCHEDULER
  98. // A variant of this is inlined in the VM at the pending exception check
  99. void mp_handle_pending(void) {
  100. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
  101. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  102. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  103. nlr_raise(obj);
  104. }
  105. }
  106. #endif // MICROPY_ENABLE_SCHEDULER