|
| 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 | + * |
| 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 | + |
| 27 | +#include <stdio.h> |
| 28 | + |
| 29 | +#include "py/mpconfig.h" |
| 30 | +#include "py/mpstate.h" |
| 31 | +#include "py/gc.h" |
| 32 | +#include "py/mpthread.h" |
| 33 | +#include "task.h" |
| 34 | + |
| 35 | +#if MICROPY_PY_THREAD |
| 36 | + |
| 37 | +// this structure forms a linked list, one node per active thread |
| 38 | +typedef struct _thread_t { |
| 39 | + TaskHandle_t id; // system id of thread |
| 40 | + int ready; // whether the thread is ready and running |
| 41 | + void *arg; // thread Python args, a GC root pointer |
| 42 | + void *stack; // pointer to the stack |
| 43 | + size_t stack_len; // number of words in the stack |
| 44 | + struct _thread_t *next; |
| 45 | +} thread_t; |
| 46 | + |
| 47 | +// the mutex controls access to the linked list |
| 48 | +STATIC mp_thread_mutex_t thread_mutex; |
| 49 | +STATIC thread_t thread_entry0; |
| 50 | +STATIC thread_t *thread; // root pointer, handled bp mp_thread_gc_others |
| 51 | + |
| 52 | +void mp_thread_init(void) { |
| 53 | + mp_thread_mutex_init(&thread_mutex); |
| 54 | + mp_thread_set_state(&mp_state_ctx.thread); |
| 55 | + |
| 56 | + // create first entry in linked list of all threads |
| 57 | + thread = &thread_entry0; |
| 58 | + thread->id = NULL; // TODO |
| 59 | + thread->ready = 1; |
| 60 | + thread->arg = NULL; |
| 61 | + thread->next = NULL; |
| 62 | +} |
| 63 | + |
| 64 | +void mp_thread_gc_others(void) { |
| 65 | + mp_thread_mutex_lock(&thread_mutex, 1); |
| 66 | + gc_collect_root((void**)&thread, 1); |
| 67 | + for (thread_t *th = thread; th != NULL; th = th->next) { |
| 68 | + gc_collect_root(&th->arg, 1); |
| 69 | + if (th->id == xTaskGetCurrentTaskHandle()) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + if (!th->ready) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + gc_collect_root(th->stack, th->stack_len); |
| 76 | + } |
| 77 | + mp_thread_mutex_unlock(&thread_mutex); |
| 78 | +} |
| 79 | + |
| 80 | +mp_state_thread_t *mp_thread_get_state(void) { |
| 81 | + return pvTaskGetThreadLocalStoragePointer(NULL, 0); |
| 82 | +} |
| 83 | + |
| 84 | +void mp_thread_set_state(void *state) { |
| 85 | + vTaskSetThreadLocalStoragePointer(NULL, 0, state); |
| 86 | +} |
| 87 | + |
| 88 | +void mp_thread_start(void) { |
| 89 | + mp_thread_mutex_lock(&thread_mutex, 1); |
| 90 | + for (thread_t *th = thread; th != NULL; th = th->next) { |
| 91 | + if (th->id == xTaskGetCurrentTaskHandle()) { |
| 92 | + th->ready = 1; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + mp_thread_mutex_unlock(&thread_mutex); |
| 97 | +} |
| 98 | + |
| 99 | +STATIC void *(*ext_thread_entry)(void*) = NULL; |
| 100 | + |
| 101 | +STATIC void freertos_entry(void *arg) { |
| 102 | + if (ext_thread_entry) { |
| 103 | + ext_thread_entry(arg); |
| 104 | + } |
| 105 | + vTaskDelete(NULL); |
| 106 | + for (;;) { |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) { |
| 111 | + // store thread entry function into a global variable so we can access it |
| 112 | + ext_thread_entry = entry; |
| 113 | + |
| 114 | + if (stack_size == 0) { |
| 115 | + stack_size = 2048; // default stack size |
| 116 | + } |
| 117 | + |
| 118 | + mp_thread_mutex_lock(&thread_mutex, 1); |
| 119 | + |
| 120 | + // create thread |
| 121 | + StackType_t *stack = m_new(StackType_t, stack_size / sizeof(StackType_t)); |
| 122 | + StaticTask_t *task_buf = m_new(StaticTask_t, 1); |
| 123 | + TaskHandle_t id = xTaskCreateStatic(freertos_entry, "Thread", stack_size / sizeof(void*), arg, 2, stack, task_buf); |
| 124 | + if (id == NULL) { |
| 125 | + mp_thread_mutex_unlock(&thread_mutex); |
| 126 | + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, "can't create thread")); |
| 127 | + } |
| 128 | + |
| 129 | + // add thread to linked list of all threads |
| 130 | + thread_t *th = m_new_obj(thread_t); |
| 131 | + th->id = id; |
| 132 | + th->ready = 0; |
| 133 | + th->arg = arg; |
| 134 | + th->stack = stack; |
| 135 | + th->stack_len = stack_size / sizeof(StackType_t); |
| 136 | + th->next = thread; |
| 137 | + thread = th; |
| 138 | + |
| 139 | + mp_thread_mutex_unlock(&thread_mutex); |
| 140 | +} |
| 141 | + |
| 142 | +void mp_thread_finish(void) { |
| 143 | + mp_thread_mutex_lock(&thread_mutex, 1); |
| 144 | + // TODO unlink from list |
| 145 | + for (thread_t *th = thread; th != NULL; th = th->next) { |
| 146 | + if (th->id == xTaskGetCurrentTaskHandle()) { |
| 147 | + th->ready = 0; |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + mp_thread_mutex_unlock(&thread_mutex); |
| 152 | +} |
| 153 | + |
| 154 | +void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { |
| 155 | + *mutex = xSemaphoreCreateMutex(); |
| 156 | + if (*mutex == NULL) { |
| 157 | + // error! |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { |
| 162 | + int ret = xSemaphoreTake(*mutex, wait ? portMAX_DELAY : 0); |
| 163 | + return ret == pdTRUE; |
| 164 | +} |
| 165 | + |
| 166 | +void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) { |
| 167 | + xSemaphoreGive(*mutex); |
| 168 | + // TODO check return value |
| 169 | +} |
| 170 | + |
| 171 | +#endif // MICROPY_PY_THREAD |
0 commit comments