Skip to content

Commit d011e8a

Browse files
authored
add a task completion semaphore to guarantee async_context task is finished before async_context cleanup finishes (#2587)
1 parent c650739 commit d011e8a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/rp2_common/pico_async_context/async_context_freertos.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static void async_context_task(__unused void *vself) {
7373
async_context_freertos_release_lock(&self->core);
7474
__sev(); // it is possible regular code is waiting on a WFE on the other core
7575
} while (!self->task_should_exit);
76+
xSemaphoreGive(self->task_complete_sem);
7677
vTaskDelete(NULL);
7778
}
7879

@@ -113,6 +114,7 @@ bool async_context_freertos_init(async_context_freertos_t *self, async_context_f
113114
assert(config->task_stack);
114115
self->lock_mutex = xSemaphoreCreateRecursiveMutexStatic(&self->lock_mutex_buf);
115116
self->work_needed_sem = xSemaphoreCreateBinaryStatic(&self->work_needed_sem_buf);
117+
self->task_complete_sem = xSemaphoreCreateBinaryStatic(&self->task_complete_sem_buf);
116118
self->timer_handle = xTimerCreateStatic( "async_context_timer", // Just a text name, not used by the kernel.
117119
portMAX_DELAY,
118120
pdFALSE, // The timers will auto-reload themselves when they expire.
@@ -129,6 +131,7 @@ bool async_context_freertos_init(async_context_freertos_t *self, async_context_f
129131
#else
130132
self->lock_mutex = xSemaphoreCreateRecursiveMutex();
131133
self->work_needed_sem = xSemaphoreCreateBinary();
134+
self->task_complete_sem = xSemaphoreCreateBinary();
132135
self->timer_handle = xTimerCreate( "async_context_timer", // Just a text name, not used by the kernel.
133136
portMAX_DELAY,
134137
pdFALSE, // The timers will auto-reload themselves when they expire.
@@ -171,6 +174,9 @@ void async_context_freertos_deinit(async_context_t *self_base) {
171174
async_context_freertos_t *self = (async_context_freertos_t *)self_base;
172175
if (self->task_handle) {
173176
async_context_execute_sync(self_base, end_task_func, self_base);
177+
if (self->task_complete_sem) {
178+
xSemaphoreTake(self->task_complete_sem, portMAX_DELAY);
179+
}
174180
}
175181
if (self->timer_handle) {
176182
xTimerDelete(self->timer_handle, 0);
@@ -181,6 +187,9 @@ void async_context_freertos_deinit(async_context_t *self_base) {
181187
if (self->work_needed_sem) {
182188
vSemaphoreDelete(self->work_needed_sem);
183189
}
190+
if (self->task_complete_sem) {
191+
vSemaphoreDelete(self->task_complete_sem);
192+
}
184193
memset(self, 0, sizeof(*self));
185194
}
186195

src/rp2_common/pico_async_context/include/pico/async_context_freertos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ struct async_context_freertos {
7575
async_context_t core;
7676
SemaphoreHandle_t lock_mutex;
7777
SemaphoreHandle_t work_needed_sem;
78+
SemaphoreHandle_t task_complete_sem;
7879
TimerHandle_t timer_handle;
7980
TaskHandle_t task_handle;
8081
#if configSUPPORT_STATIC_ALLOCATION
8182
StaticSemaphore_t lock_mutex_buf;
8283
StaticSemaphore_t work_needed_sem_buf;
84+
StaticSemaphore_t task_complete_sem_buf;
8385
StaticTimer_t timer_buf;
8486
StaticTask_t task_buf;
8587
#endif

0 commit comments

Comments
 (0)