|
| 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 |
| 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 "gccollect.h" |
| 34 | + |
| 35 | +#if MICROPY_PY_THREAD |
| 36 | + |
| 37 | +// the mutex controls access to the linked list |
| 38 | +STATIC mp_thread_mutex_t thread_mutex; |
| 39 | + |
| 40 | +void mp_thread_init(void) { |
| 41 | + mp_thread_mutex_init(&thread_mutex); |
| 42 | + mp_thread_set_state(&mp_state_ctx.thread); |
| 43 | +} |
| 44 | + |
| 45 | +void mp_thread_gc_others(void) { |
| 46 | + mp_thread_mutex_lock(&thread_mutex, 1); |
| 47 | + gc_collect_root((void**)&pyb_thread_cur, 1); |
| 48 | + for (pyb_thread_t *th = pyb_thread_cur;; th = th->next) { |
| 49 | + gc_collect_root(&th->arg, 1); |
| 50 | + if (th != pyb_thread_cur) { |
| 51 | + gc_collect_root(th->stack, th->stack_len); |
| 52 | + } |
| 53 | + if (th->next == pyb_thread_cur) { |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + mp_thread_mutex_unlock(&thread_mutex); |
| 58 | +} |
| 59 | + |
| 60 | +void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) { |
| 61 | + if (*stack_size == 0) { |
| 62 | + *stack_size = 4096; // default stack size |
| 63 | + } else if (*stack_size < 2048) { |
| 64 | + *stack_size = 2048; // minimum stack size |
| 65 | + } |
| 66 | + |
| 67 | + // round stack size to a multiple of the word size |
| 68 | + size_t stack_len = *stack_size / sizeof(uint32_t); |
| 69 | + *stack_size = stack_len * sizeof(uint32_t); |
| 70 | + |
| 71 | + // allocate stack and linked-list node (must be done outside thread_mutex lock) |
| 72 | + uint32_t *stack = m_new(uint32_t, stack_len); |
| 73 | + pyb_thread_t *th = m_new_obj(pyb_thread_t); |
| 74 | + |
| 75 | + mp_thread_mutex_lock(&thread_mutex, 1); |
| 76 | + |
| 77 | + // create thread |
| 78 | + uint32_t id = pyb_thread_new(th, stack, stack_len, entry, arg); |
| 79 | + if (id == 0) { |
| 80 | + mp_thread_mutex_unlock(&thread_mutex); |
| 81 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread")); |
| 82 | + } |
| 83 | + |
| 84 | + mp_thread_mutex_unlock(&thread_mutex); |
| 85 | + |
| 86 | + // adjust stack_size to provide room to recover from hitting the limit |
| 87 | + *stack_size -= 1024; |
| 88 | +} |
| 89 | + |
| 90 | +void mp_thread_start(void) { |
| 91 | +} |
| 92 | + |
| 93 | +void mp_thread_finish(void) { |
| 94 | +} |
| 95 | + |
| 96 | +void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { |
| 97 | + *mutex = 0; |
| 98 | +} |
| 99 | + |
| 100 | +int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { |
| 101 | + uint32_t irq_state = disable_irq(); |
| 102 | + if (*mutex) { |
| 103 | + // mutex is locked |
| 104 | + if (!wait) { |
| 105 | + enable_irq(irq_state); |
| 106 | + return 0; // failed to lock mutex |
| 107 | + } |
| 108 | + while (*mutex) { |
| 109 | + enable_irq(irq_state); |
| 110 | + pyb_thread_yield(); |
| 111 | + irq_state = disable_irq(); |
| 112 | + } |
| 113 | + } |
| 114 | + *mutex = 1; |
| 115 | + enable_irq(irq_state); |
| 116 | + return 1; // have mutex |
| 117 | +} |
| 118 | + |
| 119 | +void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) { |
| 120 | + *mutex = 0; |
| 121 | +} |
| 122 | + |
| 123 | +#endif // MICROPY_PY_THREAD |
0 commit comments