diff options
author | Damien George <damien.p.george@gmail.com> | 2016-05-31 17:34:13 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-06-28 11:28:52 +0100 |
commit | fa2ac93c3a7bbb7b368b4ea3ab3ede75396b14ac (patch) | |
tree | c919388184728cdfd36be62efcc7c8a60cbc96db | |
parent | 757146efe3b77d0f2fa47604a2c5dfd47550c12a (diff) | |
download | micropython-fa2ac93c3a7bbb7b368b4ea3ab3ede75396b14ac.tar.gz micropython-fa2ac93c3a7bbb7b368b4ea3ab3ede75396b14ac.zip |
cc3200/mpthreadport: Move mem alloc outside the thread_mutex lock.
Otherwise there could be a deadlock, with the GC's mutex and
thread_mutex.
-rw-r--r-- | cc3200/mpthreadport.c | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/cc3200/mpthreadport.c b/cc3200/mpthreadport.c index b4b2fa10a9..064aa6ba10 100644 --- a/cc3200/mpthreadport.c +++ b/cc3200/mpthreadport.c @@ -120,22 +120,21 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) { *stack_size = 2048; // minimum stack size } + // 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)); + thread_t *th = m_new_obj(thread_t); + 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); + TaskHandle_t id = xTaskCreateStatic(freertos_entry, "Thread", *stack_size / sizeof(void*), arg, 2, stack, tcb); if (id == NULL) { mp_thread_mutex_unlock(&thread_mutex); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread")); } - // adjust stack_size to provide room to recover from hitting the limit - *stack_size -= 512; - // 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; @@ -145,6 +144,9 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) { thread = th; mp_thread_mutex_unlock(&thread_mutex); + + // adjust stack_size to provide room to recover from hitting the limit + *stack_size -= 512; } void mp_thread_finish(void) { |