2016-05-27 12:35:46 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-10-17 18:53:43 -04:00
|
|
|
#include "py/runtime.h"
|
2016-05-27 12:35:46 -04:00
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/mpthread.h"
|
2016-10-18 23:24:56 -04:00
|
|
|
#include "py/mphal.h"
|
2016-05-31 12:30:07 -04:00
|
|
|
#include "mptask.h"
|
2016-05-27 12:35:46 -04:00
|
|
|
#include "task.h"
|
2016-10-18 23:24:56 -04:00
|
|
|
#include "irq.h"
|
2016-05-27 12:35:46 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
|
|
|
|
// this structure forms a linked list, one node per active thread
|
2022-05-03 23:03:43 -04:00
|
|
|
typedef struct _mp_thread_t {
|
2016-05-27 12:35:46 -04:00
|
|
|
TaskHandle_t id; // system id of thread
|
|
|
|
int ready; // whether the thread is ready and running
|
|
|
|
void *arg; // thread Python args, a GC root pointer
|
|
|
|
void *stack; // pointer to the stack
|
|
|
|
size_t stack_len; // number of words in the stack
|
2022-05-03 23:03:43 -04:00
|
|
|
struct _mp_thread_t *next;
|
|
|
|
} mp_thread_t;
|
2016-05-27 12:35:46 -04:00
|
|
|
|
|
|
|
// the mutex controls access to the linked list
|
|
|
|
STATIC mp_thread_mutex_t thread_mutex;
|
2022-05-03 23:03:43 -04:00
|
|
|
STATIC mp_thread_t thread_entry0;
|
|
|
|
STATIC mp_thread_t *thread; // root pointer, handled bp mp_thread_gc_others
|
2016-05-27 12:35:46 -04:00
|
|
|
|
|
|
|
void mp_thread_init(void) {
|
|
|
|
mp_thread_mutex_init(&thread_mutex);
|
|
|
|
mp_thread_set_state(&mp_state_ctx.thread);
|
|
|
|
|
|
|
|
// create first entry in linked list of all threads
|
|
|
|
thread = &thread_entry0;
|
2016-05-31 12:30:07 -04:00
|
|
|
thread->id = xTaskGetCurrentTaskHandle();
|
2016-05-27 12:35:46 -04:00
|
|
|
thread->ready = 1;
|
|
|
|
thread->arg = NULL;
|
2016-05-31 12:30:07 -04:00
|
|
|
thread->stack = mpTaskStack;
|
|
|
|
thread->stack_len = MICROPY_TASK_STACK_LEN;
|
2016-05-27 12:35:46 -04:00
|
|
|
thread->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_thread_gc_others(void) {
|
|
|
|
mp_thread_mutex_lock(&thread_mutex, 1);
|
2022-05-03 23:03:43 -04:00
|
|
|
for (mp_thread_t *th = thread; th != NULL; th = th->next) {
|
2020-02-26 23:36:53 -05:00
|
|
|
gc_collect_root((void **)&th, 1);
|
2016-05-31 12:32:37 -04:00
|
|
|
gc_collect_root(&th->arg, 1); // probably not needed
|
2016-05-27 12:35:46 -04:00
|
|
|
if (th->id == xTaskGetCurrentTaskHandle()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!th->ready) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-31 12:32:37 -04:00
|
|
|
gc_collect_root(th->stack, th->stack_len); // probably not needed
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
mp_thread_mutex_unlock(&thread_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_state_thread_t *mp_thread_get_state(void) {
|
|
|
|
return pvTaskGetThreadLocalStoragePointer(NULL, 0);
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:37:53 -05:00
|
|
|
void mp_thread_set_state(mp_state_thread_t *state) {
|
2016-05-27 12:35:46 -04:00
|
|
|
vTaskSetThreadLocalStoragePointer(NULL, 0, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_thread_start(void) {
|
|
|
|
mp_thread_mutex_lock(&thread_mutex, 1);
|
2022-05-03 23:03:43 -04:00
|
|
|
for (mp_thread_t *th = thread; th != NULL; th = th->next) {
|
2016-05-27 12:35:46 -04:00
|
|
|
if (th->id == xTaskGetCurrentTaskHandle()) {
|
|
|
|
th->ready = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_thread_mutex_unlock(&thread_mutex);
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
STATIC void *(*ext_thread_entry)(void *) = NULL;
|
2016-05-27 12:35:46 -04:00
|
|
|
|
|
|
|
STATIC void freertos_entry(void *arg) {
|
|
|
|
if (ext_thread_entry) {
|
|
|
|
ext_thread_entry(arg);
|
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
for (;;) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
|
2016-05-27 12:35:46 -04:00
|
|
|
// store thread entry function into a global variable so we can access it
|
|
|
|
ext_thread_entry = entry;
|
|
|
|
|
2016-05-30 11:56:51 -04:00
|
|
|
if (*stack_size == 0) {
|
|
|
|
*stack_size = 4096; // default stack size
|
|
|
|
} else if (*stack_size < 2048) {
|
|
|
|
*stack_size = 2048; // minimum stack size
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 12:34:13 -04:00
|
|
|
// allocate TCB, stack and linked-list node (must be outside thread_mutex lock)
|
|
|
|
StaticTask_t *tcb = m_new(StaticTask_t, 1);
|
|
|
|
StackType_t *stack = m_new(StackType_t, *stack_size / sizeof(StackType_t));
|
2022-05-03 23:03:43 -04:00
|
|
|
mp_thread_t *th = m_new_obj(mp_thread_t);
|
2016-05-31 12:34:13 -04:00
|
|
|
|
2016-05-27 12:35:46 -04:00
|
|
|
mp_thread_mutex_lock(&thread_mutex, 1);
|
|
|
|
|
|
|
|
// create thread
|
2020-02-26 23:36:53 -05:00
|
|
|
TaskHandle_t id = xTaskCreateStatic(freertos_entry, "Thread", *stack_size / sizeof(void *), arg, 2, stack, tcb);
|
2016-05-27 12:35:46 -04:00
|
|
|
if (id == NULL) {
|
|
|
|
mp_thread_mutex_unlock(&thread_mutex);
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("can't create thread"));
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add thread to linked list of all threads
|
|
|
|
th->id = id;
|
|
|
|
th->ready = 0;
|
|
|
|
th->arg = arg;
|
|
|
|
th->stack = stack;
|
2016-05-30 11:56:51 -04:00
|
|
|
th->stack_len = *stack_size / sizeof(StackType_t);
|
2016-05-27 12:35:46 -04:00
|
|
|
th->next = thread;
|
|
|
|
thread = th;
|
|
|
|
|
|
|
|
mp_thread_mutex_unlock(&thread_mutex);
|
2016-05-31 12:34:13 -04:00
|
|
|
|
|
|
|
// adjust stack_size to provide room to recover from hitting the limit
|
|
|
|
*stack_size -= 512;
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void mp_thread_finish(void) {
|
|
|
|
mp_thread_mutex_lock(&thread_mutex, 1);
|
|
|
|
// TODO unlink from list
|
2022-05-03 23:03:43 -04:00
|
|
|
for (mp_thread_t *th = thread; th != NULL; th = th->next) {
|
2016-05-27 12:35:46 -04:00
|
|
|
if (th->id == xTaskGetCurrentTaskHandle()) {
|
|
|
|
th->ready = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_thread_mutex_unlock(&thread_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_thread_mutex_init(mp_thread_mutex_t *mutex) {
|
2016-05-31 12:28:53 -04:00
|
|
|
mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer);
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 23:24:56 -04:00
|
|
|
// To allow hard interrupts to work with threading we only take/give the semaphore
|
|
|
|
// if we are not within an interrupt context and interrupts are enabled.
|
|
|
|
|
2016-05-27 12:35:46 -04:00
|
|
|
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {
|
2016-10-18 23:24:56 -04:00
|
|
|
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
|
|
|
|
int ret = xSemaphoreTake(mutex->handle, wait ? portMAX_DELAY : 0);
|
|
|
|
return ret == pdTRUE;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
|
2016-10-18 23:24:56 -04:00
|
|
|
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
|
|
|
|
xSemaphoreGive(mutex->handle);
|
|
|
|
// TODO check return value
|
|
|
|
}
|
2016-05-27 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_THREAD
|