From a4e007e75563b0b3cdef0ee2460f9466f4a2655f Mon Sep 17 00:00:00 2001 From: Reda Maher Date: Tue, 29 Sep 2020 10:16:14 +0200 Subject: [PATCH] Posix: Fix no task switching issue if a task ended When the main function of a task exits, no task switching happened. This is because all the remaining tasks are waiting on the condition variable. The fix is to trigger a task switch and mark the exiting task as "Dying" to be suspened and exited properly from the scheduler. --- portable/ThirdParty/GCC/Posix/port.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/portable/ThirdParty/GCC/Posix/port.c b/portable/ThirdParty/GCC/Posix/port.c index 086ad487a..39fed3cc3 100644 --- a/portable/ThirdParty/GCC/Posix/port.c +++ b/portable/ThirdParty/GCC/Posix/port.c @@ -66,6 +66,7 @@ /*-----------------------------------------------------------*/ #define SIG_RESUME SIGUSR1 +#define portTASK_ENDED_DELAY ( 500 ) typedef struct THREAD { @@ -421,6 +422,14 @@ Thread_t *pxThread = pvParams; /* Call the task's entry point. */ pxThread->pxCode( pxThread->pvParams ); + /* The task has finished, we need to trigger a task switch here + * to avoid exiting while all tasks are waiting to be signaled. + * So we will mark the task as Dying here then delay the task + * with any value to trigger a task switch where the task will + * be suspended and exited. */ + pxThread->xDying = pdTRUE; + vTaskDelay( portTASK_ENDED_DELAY ); + return NULL; } /*-----------------------------------------------------------*/