FreeRTOS Semaphores: Synchronization and Mutual Exclusion
Semaphores coordinate access to shared resources between tasks. They prevent race conditions and allow tasks to signal each other.
A semaphore is a signaling mechanism. Binary semaphores are like flags (0 or 1), counting semaphores can track multiple available resources, and mutexes add priority inheritance to prevent priority inversion.
Binary Semaphore (Task Synchronization)
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t xBinarySemaphore;
void Task1(void *pvParameters) {
for (;;) {
// Do some work
vTaskDelay(pdMS_TO_TICKS(1000));
// Signal task 2
xSemaphoreGive(xBinarySemaphore);
}
}
void Task2(void *pvParameters) {
for (;;) {
// Wait for signal from Task1
if (xSemaphoreTake(xBinarySemaphore, portMAX_DELAY) == pdTRUE) {
Serial.println("Task2 received signal from Task1");
}
}
}
void setup() {
Serial.begin(115200);
xBinarySemaphore = xSemaphoreCreateBinary();
xTaskCreate(Task1, "Task1", 128, NULL, 1, NULL);
xTaskCreate(Task2, "Task2", 128, NULL, 1, NULL);
vTaskStartScheduler();
}Mutex (Mutual Exclusion)
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t xMutex;
int sharedCounter = 0;
void TaskIncrement(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(xMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
sharedCounter++;
Serial.print("Incremented: ");
Serial.println(sharedCounter);
xSemaphoreGive(xMutex);
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void TaskDecrement(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(xMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
sharedCounter--;
Serial.print("Decremented: ");
Serial.println(sharedCounter);
xSemaphoreGive(xMutex);
}
vTaskDelay(pdMS_TO_TICKS(15));
}
}
void setup() {
Serial.begin(115200);
xMutex = xSemaphoreCreateMutex();
xTaskCreate(TaskIncrement, "Inc", 128, NULL, 1, NULL);
xTaskCreate(TaskDecrement, "Dec", 128, NULL, 1, NULL);
vTaskStartScheduler();
}Counting Semaphore (Resource Pool)
SemaphoreHandle_t xCountingSemaphore;
const int MAX_RESOURCES = 3;
void TaskUseResource(void *pvParameters) {
int taskId = (int)pvParameters;
for (;;) {
// Acquire a resource
if (xSemaphoreTake(xCountingSemaphore, portMAX_DELAY) == pdTRUE) {
Serial.print("Task ");
Serial.print(taskId);
Serial.println(" acquired resource");
// Use resource
vTaskDelay(pdMS_TO_TICKS(1000));
// Release resource
xSemaphoreGive(xCountingSemaphore);
Serial.print("Task ");
Serial.print(taskId);
Serial.println(" released resource");
}
}
}
void setup() {
Serial.begin(115200);
xCountingSemaphore = xSemaphoreCreateCounting(MAX_RESOURCES, MAX_RESOURCES);
for (int i = 0; i < 5; i++) {
xTaskCreate(TaskUseResource, "UseRes", 128, (void*)i, 1, NULL);
}
vTaskStartScheduler();
}Key Takeaways
xSemaphoreCreateBinary()creates a binary semaphore (0 or 1)xSemaphoreCreateMutex()creates a mutex with priority inheritancexSemaphoreCreateCounting(max, initial)creates a counting semaphorexSemaphoreTake()waits for semaphore (blocks if not available)xSemaphoreGive()releases semaphore- Mutex protects shared resources; binary semaphore signals events; counting semaphore manages resource pools
Resources
- FreeRTOS Semaphores: https://www.freertos.org/Semaphores.html
- Video: FreeRTOS Semaphores Explained - https://www.youtube.com/watch?v=3R8WYoG0Xsc
- FreeRTOS Mutex API: https://www.freertos.org/a00124.html