Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Depending on use case Semaphores or Queues can be usedThis chapter will give you an introduction into Semaphores and Queues to prevent task competition, enable task synchronization and intertask communication.

Semaphores

Semaphores are  are used for both, mutual exclusion and synchronization purposes.

...

Synchronization: A Task has to wait for a specific event to occur.

Binary Semaphore

Binary Semaphore can be thought of a boolean. If the boolean is true - access is granted, if false - access is denied.

Image Modified

Binary Semaphore are your choice for task synchronization!

Producer-Consumer example:

Image Modified

Important freeRTOS commands are: xSemaphoreCreateBinary()xSemaphoreTake() and xSemaphoreGive().

If called from ISR it is important to use xSemaphoreTakeFromISR() and xSemaphoreGiveFromISR().

Mutex

Mutex (hence ‘MUT’ual ‘EX’clusion) is a special Semaphore, an object that blocks resources from being used more than once at the time. It is basically a binary Semaphore including priority inheritance and a Mutex can only be given back by the task that took the Mutex. This prevents concurrency problems like mentioned in the Concurrent Task Execution introduction. Typically, the Mutex is being taken before, and given back after accessing a critical resource.

Important freeRTOS commands are: xSemaphoreCreateMutex()xSemaphoreTake() and xSemaphoreGive().

...

Counting Semaphore is just as a Binary Semaphore but with length greater than one. It is of no interest which Data is stored - only whether Data is received. Counting Semaphores are used for Counting Events and Resource Management.

Important freeRTOS commands are: xSemaphoreCreateCounting()xSemaphoreTake() and xSemaphoreGive().

If called from ISR it is important to use xSemaphoreTakeFromISR() and xSemaphoreGiveFromISR().

Queue

Queues are the primary form of intertask communications. They can be used to send messages between tasks, and between interrupts and tasks. In most cases they are used as thread safe FIFO (First In First Out) buffers with new data being sent to the back of the queue, although data can also be sent to the front.


Important freeRTOS commands are: xQueueCreate()xQueueSend() and xQueueReceive().

If called from ISR it is important to use xQueueReceiveFromISR() and xQueueSendFromISR().