Can create a new thread and run it. Does not use the GIL at this point.crypto-aes
parent
9b1c1262dc
commit
eef4f13a33
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include "py/mpstate.h"
|
||||
#include "py/gc.h"
|
||||
#include "py/mpthread.h"
|
||||
#include "task.h"
|
||||
|
||||
#if MICROPY_PY_THREAD
|
||||
|
||||
// this structure forms a linked list, one node per active thread
|
||||
typedef struct _thread_t {
|
||||
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
|
||||
struct _thread_t *next;
|
||||
} thread_t;
|
||||
|
||||
// the mutex controls access to the linked list
|
||||
STATIC mp_thread_mutex_t thread_mutex;
|
||||
STATIC thread_t thread_entry0;
|
||||
STATIC thread_t *thread; // root pointer, handled bp mp_thread_gc_others
|
||||
|
||||
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;
|
||||
thread->id = NULL; // TODO
|
||||
thread->ready = 1;
|
||||
thread->arg = NULL;
|
||||
thread->next = NULL;
|
||||
}
|
||||
|
||||
void mp_thread_gc_others(void) {
|
||||
mp_thread_mutex_lock(&thread_mutex, 1);
|
||||
gc_collect_root((void**)&thread, 1);
|
||||
for (thread_t *th = thread; th != NULL; th = th->next) {
|
||||
gc_collect_root(&th->arg, 1);
|
||||
if (th->id == xTaskGetCurrentTaskHandle()) {
|
||||
continue;
|
||||
}
|
||||
if (!th->ready) {
|
||||
continue;
|
||||
}
|
||||
gc_collect_root(th->stack, th->stack_len);
|
||||
}
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
}
|
||||
|
||||
mp_state_thread_t *mp_thread_get_state(void) {
|
||||
return pvTaskGetThreadLocalStoragePointer(NULL, 0);
|
||||
}
|
||||
|
||||
void mp_thread_set_state(void *state) {
|
||||
vTaskSetThreadLocalStoragePointer(NULL, 0, state);
|
||||
}
|
||||
|
||||
void mp_thread_start(void) {
|
||||
mp_thread_mutex_lock(&thread_mutex, 1);
|
||||
for (thread_t *th = thread; th != NULL; th = th->next) {
|
||||
if (th->id == xTaskGetCurrentTaskHandle()) {
|
||||
th->ready = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
}
|
||||
|
||||
STATIC void *(*ext_thread_entry)(void*) = NULL;
|
||||
|
||||
STATIC void freertos_entry(void *arg) {
|
||||
if (ext_thread_entry) {
|
||||
ext_thread_entry(arg);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
for (;;) {
|
||||
}
|
||||
}
|
||||
|
||||
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
|
||||
// store thread entry function into a global variable so we can access it
|
||||
ext_thread_entry = entry;
|
||||
|
||||
if (stack_size == 0) {
|
||||
stack_size = 2048; // default stack size
|
||||
}
|
||||
|
||||
mp_thread_mutex_lock(&thread_mutex, 1);
|
||||
|
||||
// create thread
|
||||
StackType_t *stack = m_new(StackType_t, stack_size / sizeof(StackType_t));
|
||||
StaticTask_t *task_buf = m_new(StaticTask_t, 1);
|
||||
TaskHandle_t id = xTaskCreateStatic(freertos_entry, "Thread", stack_size / sizeof(void*), arg, 2, stack, task_buf);
|
||||
if (id == NULL) {
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, "can't create thread"));
|
||||
}
|
||||
|
||||
// add thread to linked list of all threads
|
||||
thread_t *th = m_new_obj(thread_t);
|
||||
th->id = id;
|
||||
th->ready = 0;
|
||||
th->arg = arg;
|
||||
th->stack = stack;
|
||||
th->stack_len = stack_size / sizeof(StackType_t);
|
||||
th->next = thread;
|
||||
thread = th;
|
||||
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
}
|
||||
|
||||
void mp_thread_finish(void) {
|
||||
mp_thread_mutex_lock(&thread_mutex, 1);
|
||||
// TODO unlink from list
|
||||
for (thread_t *th = thread; th != NULL; th = th->next) {
|
||||
if (th->id == xTaskGetCurrentTaskHandle()) {
|
||||
th->ready = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mp_thread_mutex_unlock(&thread_mutex);
|
||||
}
|
||||
|
||||
void mp_thread_mutex_init(mp_thread_mutex_t *mutex) {
|
||||
*mutex = xSemaphoreCreateMutex();
|
||||
if (*mutex == NULL) {
|
||||
// error!
|
||||
}
|
||||
}
|
||||
|
||||
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {
|
||||
int ret = xSemaphoreTake(*mutex, wait ? portMAX_DELAY : 0);
|
||||
return ret == pdTRUE;
|
||||
}
|
||||
|
||||
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
|
||||
xSemaphoreGive(*mutex);
|
||||
// TODO check return value
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_THREAD
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef __MICROPY_INCLUDED_CC3200_MPTHREADPORT_H__
|
||||
#define __MICROPY_INCLUDED_CC3200_MPTHREADPORT_H__
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
|
||||
typedef SemaphoreHandle_t mp_thread_mutex_t;
|
||||
|
||||
void mp_thread_init(void);
|
||||
void mp_thread_gc_others(void);
|
||||
|
||||
#endif // __MICROPY_INCLUDED_CC3200_MPTHREADPORT_H__
|
Loading…
Reference in new issue