Updated Jun 2025
xSemaphoreGiveFromISR
semphr. h
1xSemaphoreGiveFromISR2 (3 SemaphoreHandle_t xSemaphore,4 signed BaseType_t *pxHigherPriorityTaskWoken5 )
Macro to release a semaphore. The semaphore must have previously been created with a call to xSemaphoreCreateBinary() or xSemaphoreCreateCounting().
Mutex type semaphores (those created using a call to xSemaphoreCreateMutex()) must not be used with this macro.
This macro can be used from an ISR.
Parameters:
-
xSemaphore
A handle to the semaphore being released. This is the handle returned when the semaphore was created.
-
pxHigherPriorityTaskWoken
xSemaphoreGiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task to unblock, and the unblocked task has a priority higher than the currently running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then a context switch should be requested before the interrupt is exited.From FreeRTOS V7.3.0 pxHigherPriorityTaskWoken is an optional parameter and can be set to NULL.
Returns:
pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
Example usage:
Note the functionality shown below can often be achieved in a more efficient way by using a direct to task notification in place of a semaphore.
1#define LONG_TIME 0xffff2#define TICKS_TO_WAIT 1034SemaphoreHandle_t xSemaphore = NULL;56/* Repetitive task. */7void vATask( void * pvParameters )8{9 /* We are using the semaphore for synchronisation so we create a binary10 semaphore rather than a mutex. We must make sure that the interrupt11 does not attempt to use the semaphore before it is created! */12 xSemaphore = xSemaphoreCreateBinary();1314 for( ;; )15 {16 /* We want this task to run every 10 ticks of a timer. The semaphore17 was created before this task was started.1819 Block waiting for the semaphore to become available. */20 if( xSemaphoreTake( xSemaphore, LONG\_TIME ) == pdTRUE )21 {22 /* It is time to execute. */2324 ...2526 /* We have finished our task. Return to the top of the loop where27 we will block on the semaphore until it is time to execute28 again. Note when using the semaphore for synchronisation with an29 ISR in this manner there is no need to 'give' the semaphore30 back. */31 }32 }33}3435/* Timer ISR */36void vTimerISR( void * pvParameters )37{38static unsigned char ucLocalTickCount = 0;39BaseType_t xHigherPriorityTaskWoken = pdFALSE;4041 /* A timer tick has occurred. */4243 ... Do other time functions.4445 /* Is it time for vATask() to run? */46 xHigherPriorityTaskWoken = pdFALSE;47 ucLocalTickCount++;48 if( ucLocalTickCount >= TICKS_TO_WAIT )49 {50 /* Unblock the task by releasing the semaphore. */51 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );5253 /* Reset the count so we release the semaphore again in 10 ticks54 time. */55 ucLocalTickCount = 0;56 }5758 /* Yield if xHigherPriorityTaskWoken is true. The59 actual macro used here is port specific. */60 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );61}