mpthreadport.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
  7. * Copyright (c) 2017 Pycom Limited
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "stdio.h"
  28. #include "py/mpconfig.h"
  29. #include "py/mpstate.h"
  30. #include "py/gc.h"
  31. #include "py/mpthread.h"
  32. #include "mpthreadport.h"
  33. #include "esp_task.h"
  34. #if MICROPY_PY_THREAD
  35. #define MP_THREAD_MIN_STACK_SIZE (4 * 1024)
  36. #define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + 1024)
  37. #define MP_THREAD_PRIORITY (ESP_TASK_PRIO_MIN + 1)
  38. // this structure forms a linked list, one node per active thread
  39. typedef struct _thread_t {
  40. TaskHandle_t id; // system id of thread
  41. int ready; // whether the thread is ready and running
  42. void *arg; // thread Python args, a GC root pointer
  43. void *stack; // pointer to the stack
  44. StaticTask_t *tcb; // pointer to the Task Control Block
  45. size_t stack_len; // number of words in the stack
  46. struct _thread_t *next;
  47. } thread_t;
  48. // the mutex controls access to the linked list
  49. STATIC mp_thread_mutex_t thread_mutex;
  50. STATIC thread_t thread_entry0;
  51. STATIC thread_t *thread; // root pointer, handled by mp_thread_gc_others
  52. void mp_thread_init(void *stack, uint32_t stack_len) {
  53. mp_thread_set_state(&mp_state_ctx.thread);
  54. // create the first entry in the linked list of all threads
  55. thread = &thread_entry0;
  56. thread->id = xTaskGetCurrentTaskHandle();
  57. thread->ready = 1;
  58. thread->arg = NULL;
  59. thread->stack = stack;
  60. thread->stack_len = stack_len;
  61. thread->next = NULL;
  62. mp_thread_mutex_init(&thread_mutex);
  63. }
  64. void mp_thread_gc_others(void) {
  65. mp_thread_mutex_lock(&thread_mutex, 1);
  66. for (thread_t *th = thread; th != NULL; th = th->next) {
  67. gc_collect_root((void**)&th, 1);
  68. gc_collect_root(&th->arg, 1); // probably not needed
  69. if (th->id == xTaskGetCurrentTaskHandle()) {
  70. continue;
  71. }
  72. if (!th->ready) {
  73. continue;
  74. }
  75. gc_collect_root(th->stack, th->stack_len); // probably not needed
  76. }
  77. mp_thread_mutex_unlock(&thread_mutex);
  78. }
  79. mp_state_thread_t *mp_thread_get_state(void) {
  80. return pvTaskGetThreadLocalStoragePointer(NULL, 1);
  81. }
  82. void mp_thread_set_state(void *state) {
  83. vTaskSetThreadLocalStoragePointer(NULL, 1, state);
  84. }
  85. void mp_thread_start(void) {
  86. mp_thread_mutex_lock(&thread_mutex, 1);
  87. for (thread_t *th = thread; th != NULL; th = th->next) {
  88. if (th->id == xTaskGetCurrentTaskHandle()) {
  89. th->ready = 1;
  90. break;
  91. }
  92. }
  93. mp_thread_mutex_unlock(&thread_mutex);
  94. }
  95. STATIC void *(*ext_thread_entry)(void*) = NULL;
  96. STATIC void freertos_entry(void *arg) {
  97. if (ext_thread_entry) {
  98. ext_thread_entry(arg);
  99. }
  100. vTaskDelete(NULL);
  101. for (;;);
  102. }
  103. void mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack_size, int priority, char *name) {
  104. // store thread entry function into a global variable so we can access it
  105. ext_thread_entry = entry;
  106. if (*stack_size == 0) {
  107. *stack_size = MP_THREAD_DEFAULT_STACK_SIZE; // default stack size
  108. } else if (*stack_size < MP_THREAD_MIN_STACK_SIZE) {
  109. *stack_size = MP_THREAD_MIN_STACK_SIZE; // minimum stack size
  110. }
  111. // allocate TCB, stack and linked-list node (must be outside thread_mutex lock)
  112. StaticTask_t *tcb = m_new(StaticTask_t, 1);
  113. StackType_t *stack = m_new(StackType_t, *stack_size / sizeof(StackType_t));
  114. thread_t *th = m_new_obj(thread_t);
  115. mp_thread_mutex_lock(&thread_mutex, 1);
  116. // create thread
  117. TaskHandle_t id = xTaskCreateStaticPinnedToCore(freertos_entry, name, *stack_size / sizeof(StackType_t), arg, priority, stack, tcb, 0);
  118. if (id == NULL) {
  119. mp_thread_mutex_unlock(&thread_mutex);
  120. nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread"));
  121. }
  122. // adjust the stack_size to provide room to recover from hitting the limit
  123. *stack_size -= 1024;
  124. // add thread to linked list of all threads
  125. th->id = id;
  126. th->ready = 0;
  127. th->arg = arg;
  128. th->stack = stack;
  129. th->tcb = tcb;
  130. th->stack_len = *stack_size / sizeof(StackType_t);
  131. th->next = thread;
  132. thread = th;
  133. mp_thread_mutex_unlock(&thread_mutex);
  134. }
  135. void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
  136. mp_thread_create_ex(entry, arg, stack_size, MP_THREAD_PRIORITY, "mp_thread");
  137. }
  138. void mp_thread_finish(void) {
  139. mp_thread_mutex_lock(&thread_mutex, 1);
  140. for (thread_t *th = thread; th != NULL; th = th->next) {
  141. if (th->id == xTaskGetCurrentTaskHandle()) {
  142. th->ready = 0;
  143. break;
  144. }
  145. }
  146. mp_thread_mutex_unlock(&thread_mutex);
  147. }
  148. void vPortCleanUpTCB(void *tcb) {
  149. thread_t *prev = NULL;
  150. mp_thread_mutex_lock(&thread_mutex, 1);
  151. for (thread_t *th = thread; th != NULL; prev = th, th = th->next) {
  152. // unlink the node from the list
  153. if (th->tcb == tcb) {
  154. if (prev != NULL) {
  155. prev->next = th->next;
  156. } else {
  157. // move the start pointer
  158. thread = th->next;
  159. }
  160. // explicitly release all its memory
  161. m_del(StaticTask_t, th->tcb, 1);
  162. m_del(StackType_t, th->stack, th->stack_len);
  163. m_del(thread_t, th, 1);
  164. break;
  165. }
  166. }
  167. mp_thread_mutex_unlock(&thread_mutex);
  168. }
  169. void mp_thread_mutex_init(mp_thread_mutex_t *mutex) {
  170. mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer);
  171. }
  172. int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {
  173. return (pdTRUE == xSemaphoreTake(mutex->handle, wait ? portMAX_DELAY : 0));
  174. }
  175. void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
  176. xSemaphoreGive(mutex->handle);
  177. }
  178. void mp_thread_deinit(void) {
  179. mp_thread_mutex_lock(&thread_mutex, 1);
  180. for (thread_t *th = thread; th != NULL; th = th->next) {
  181. // don't delete the current task
  182. if (th->id == xTaskGetCurrentTaskHandle()) {
  183. continue;
  184. }
  185. vTaskDelete(th->id);
  186. }
  187. mp_thread_mutex_unlock(&thread_mutex);
  188. // allow FreeRTOS to clean-up the threads
  189. vTaskDelay(2);
  190. }
  191. #else
  192. void vPortCleanUpTCB(void *tcb) {
  193. }
  194. #endif // MICROPY_PY_THREAD