Skip to end of banner
Go to start of banner

Resource Locking

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Introduction

This chapter will give you an introduction into Semaphores and Queues to prevent task competition, enable task synchronization and intertask communication.

Semaphores

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

Mutual Exclusion: A shared resource can only be accessed by one task. This prevents processes from competing with each other.

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.


Binary Semaphore are your choice for task synchronization purposes!

Producer-Consumer example:

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().

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

Counting Semaphore

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().

  • No labels