Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Jun 2025

xSemaphoreGiveFromISR

[Semaphores]

TIP: In many usage scenarios it is faster and more memory efficient to use a direct to task notification instead of a semaphore

semphr. h

1xSemaphoreGiveFromISR
2 (
3 SemaphoreHandle_t xSemaphore,
4 signed BaseType_t *pxHigherPriorityTaskWoken
5 )

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 0xffff
2#define TICKS_TO_WAIT 10
3
4SemaphoreHandle_t xSemaphore = NULL;
5
6/* Repetitive task. */
7void vATask( void * pvParameters )
8{
9 /* We are using the semaphore for synchronisation so we create a binary
10 semaphore rather than a mutex. We must make sure that the interrupt
11 does not attempt to use the semaphore before it is created! */
12 xSemaphore = xSemaphoreCreateBinary();
13
14 for( ;; )
15 {
16 /* We want this task to run every 10 ticks of a timer. The semaphore
17 was created before this task was started.
18
19 Block waiting for the semaphore to become available. */
20 if( xSemaphoreTake( xSemaphore, LONG\_TIME ) == pdTRUE )
21 {
22 /* It is time to execute. */
23
24 ...
25
26 /* We have finished our task. Return to the top of the loop where
27 we will block on the semaphore until it is time to execute
28 again. Note when using the semaphore for synchronisation with an
29 ISR in this manner there is no need to 'give' the semaphore
30 back. */
31 }
32 }
33}
34
35/* Timer ISR */
36void vTimerISR( void * pvParameters )
37{
38static unsigned char ucLocalTickCount = 0;
39BaseType_t xHigherPriorityTaskWoken = pdFALSE;
40
41 /* A timer tick has occurred. */
42
43 ... Do other time functions.
44
45 /* 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 );
52
53 /* Reset the count so we release the semaphore again in 10 ticks
54 time. */
55 ucLocalTickCount = 0;
56 }
57
58 /* Yield if xHigherPriorityTaskWoken is true. The
59 actual macro used here is port specific. */
60 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
61}