8000 esp32/mpthreadport.c: fix an issue in cleaning up user threads during… · pycom/pycom-micropython-sigfox@97bf7be · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 97bf7be

Browse files
husigezaiwahdan88
authored and
iwahdan88
committed
esp32/mpthreadport.c: fix an issue in cleaning up user threads during of soft-reset (#14)
Fixes issue [PYFW-197] # Conflicts: # esp32/mpthreadport.c
1 parent 194cdf4 commit 97bf7be

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

esp32/mpthreadport.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ STATIC mp_thread_mutex_t thread_mutex;
7575
STATIC thread_t thread_entry0;
7676
STATIC thread_t *thread; // root pointer, handled by mp_thread_gc_others
7777

78+
STATIC bool during_soft_reset = false;
79+
7880
void mp_thread_preinit(void *stack, uint32_t stack_len) {
7981
mp_thread_set_state(&mp_state_ctx.thread);
8082
// create first entry in linked list of all threads
@@ -219,7 +221,19 @@ void mp_thread_finish(void) {
219221

220222
void vPortCleanUpTCB (void *tcb) {
221223
thread_t *prev = NULL;
222-
mp_thread_mutex_lock(&thread_mutex, 1);
224+
225+
/* If we are performing soft-reset the mutex has been already taken by mp_thread_deinit,
226+
* do not try to take it here again as it causes deadlock.
227+
* As the mutex is already taken, performing actions on the "thread" list is safe. */
228+
if(during_soft_reset != true)
229+
{
230+
if(thread_mutex.handle == NULL)
231+
{
232+
mp_thread_mutex_init(&thread_mutex);
233+
}
234+
mp_thread_mutex_lock(&thread_mutex, 1);
235+
}
236+
223237
for (thread_t *th = thread; th != NULL; prev = th, th = th->next) {
224238
// unlink the node from the list
225239
if (th->tcb == tcb) {
@@ -229,7 +243,7 @@ void vPortCleanUpTCB (void *tcb) {
229243
// move the start pointer
230244
thread = th->next;
231245
}
232-
// explicitely release all its memory
246+
// explicitly release all its memory
233247
if (esp_get_revision() > 0) {
234248
free(th->tcb);
235249
free(th->stack);
@@ -242,7 +256,10 @@ void vPortCleanUpTCB (void *tcb) {
242256
break;
243257
}
244258
}
245-
mp_thread_mutex_unlock(&thread_mutex);
259+
260+
if(during_soft_reset != true) {
261+
mp_thread_mutex_unlock(&thread_mutex);
262+
}
246263
}
247264

248265
mp_obj_thread_lock_t *mp_thread_new_thread_lock(void) {
@@ -270,13 +287,17 @@ void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
270287

271288
void mp_thread_deinit(void) {
272289
mp_thread_mutex_lock(&thread_mutex, 1);
290+
during_soft_reset = true;
291+
273292
for (thread_t *th = thread; th != NULL; th = th->next) {
274293
// don't delete the current task
275294
if (th->id == xTaskGetCurrentTaskHandle()) {
276295
continue;
277296
}
278297
vTaskDelete(th->id);
279298
}
299+
300+
during_soft_reset = false;
280301
mp_thread_mutex_unlock(&thread_mutex);
281302
// allow FreeRTOS to clean-up the threads
282303
vTaskDelay(2);

0 commit comments

Comments
 (0)
0