Skip to content

Commit 4930934

Browse files
Saiiijchanwangfei_chenaggarg
authored
timer: prevent infinite loop on one-shot timer start (#38)
* timer: prevent infinite loop on one-shot timer start One-shot timers that expire after a single tick could immediately transition to a dormant state, despite xTimerCommandSent being pdTRUE. This situation resulted in a potential deadlock due to an infinite loop in the timer activation process. Signed-off-by: wangfei_chen <[email protected]> * Code review suggestions Signed-off-by: Gaurav Aggarwal <[email protected]> --------- Signed-off-by: wangfei_chen <[email protected]> Signed-off-by: Gaurav Aggarwal <[email protected]> Co-authored-by: wangfei_chen <[email protected]> Co-authored-by: Gaurav Aggarwal <[email protected]>
1 parent ae1d3f0 commit 4930934

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_timer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct timer_internal
5454
StaticTimer_t xTimerBuffer; /**< Memory that holds the FreeRTOS timer. */
5555
struct sigevent xTimerEvent; /**< What to do when this timer expires. */
5656
TickType_t xTimerPeriod; /**< Period of this timer. */
57+
UBaseType_t uxTimerCallbackInvocations; /**< Number of times the timer callback has been invoked. */
5758
} timer_internal_t;
5859

5960
/*-----------------------------------------------------------*/
@@ -94,6 +95,7 @@ void prvTimerCallback( TimerHandle_t xTimer )
9495
pxTimer->xTimerEvent.sigev_value.sival_ptr );
9596
}
9697
}
98+
pxTimer->uxTimerCallbackInvocations++;
9799
}
98100

99101
/*-----------------------------------------------------------*/
@@ -273,6 +275,9 @@ int timer_settime( timer_t timerid,
273275
}
274276
}
275277

278+
/* Set uxTimerCallbackInvocations before timer start. */
279+
pxTimer->uxTimerCallbackInvocations = 0
280+
276281
/* If xNextTimerExpiration is still 0, that means that it_value specified
277282
* an absolute timeout in the past. Per POSIX spec, a notification should be
278283
* triggered immediately. */
@@ -286,7 +291,9 @@ int timer_settime( timer_t timerid,
286291
xTimerCommandSent = xTimerChangePeriod( xTimer, xNextTimerExpiration, xNextTimerExpiration );
287292

288293
/* Wait until the timer start command is processed. */
289-
while( ( xTimerCommandSent != pdFAIL ) && ( xTimerIsTimerActive( xTimer ) == pdFALSE ) )
294+
while( ( xTimerCommandSent != pdFAIL ) &&
295+
( xTimerIsTimerActive( xTimer ) == pdFALSE ) &&
296+
( pxTimer->uxTimerCallbackInvocations == 0 ) )
290297
{
291298
vTaskDelay( 1 );
292299
}

0 commit comments

Comments
 (0)