In this tutorial, we will see one of the demerits of binary semaphore ie. Non-Recursive Locks.
Here we will see what happens if a task tries to acquire same binary semaphore more than ones.

Teminologies

  1. LPT: Low Priority Task
  2. MPT: Medium Priority Task
  3. HPT: High Priority Task


Prerequisites

Please check this tutorial for detailed explanation on Semaphores.

API Details

Below is the list of the API's used in this tutorial.

1.xTaskCreate(): This interface is used to create a new Task, if the task is successfully created then it returns pdPass(1) or else errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY(-1). Check this link for more details.

2.vTaskDelay(): This function is used to delay/block the task for specified delay time(ticks). INCLUDE_vTaskDelay needs to be set to 1 in FreeRtosConfig.h file for using this function. Check this link for more details.

3.vTaskDelete():This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.
To delete the own task we should pass NULL as parameter.
Please check this link for details.

4.vSemaphoreCreateBinary(): This interface is used to create a binary semaphore. We need to pass a handle of type SemaphoreHandle_t to this function.
Please check this link for details.

5.xSemaphoreTake(): This interface is used to acquire a binary semaphore. We need to pass a semaphore handle and the amount of time to wait for semaphore to become available.
Please check this link for details.

6.xSemaphoreGive(): This interface is used to release a binary semaphore. We need to pass a semaphore handle to this function.
Please check this link for details.

7.vSemaphoreDelete (): This interface is used to delete a binary semaphore. We need to pass a semaphore handle to this function.
Please check this link for details.


Example

In this example, we will be creating an LPT. LPT will acquire a semaphore and creates an HPT. HPT will preempt LPT as starts running. HPT tries to acquire a semaphore used by LPT and goes to blocked state resulting in priority inversion. Now LPT starts running and requests for the same semaphore.

/***************************************************************************************************
ExploreEmbedded Copyright Notice
****************************************************************************************************
* File: BinarySemaphoreNonRecursiveLock
* Version: 16.0
* Author: ExploreEmbedded
* Website: http://www.exploreembedded.com/wiki
* Description: Program to demonstrate Non-Recursive Locks in Binary semaphore.
This code has been developed and tested on ExploreEmbedded boards.
We strongly believe that the library works on any of development boards for respective controllers.
Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT.
ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider
buying the ExploreEmbedded boards.
The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license).
See also: http://www.opensource.org/licenses/bsd-license.php
EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR
INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION
RELATED TO UPDATES.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose
and without fee is hereby granted, provided that this copyright notices appear in all copies
and that both those copyright notices and this permission notice appear in supporting documentation.
**************************************************************************************************/
#include <Arduino_FreeRTOS.h>
TaskHandle_t LPT_TaskHandle;
TaskHandle_t HPT_TaskHandle;
SemaphoreHandle_t binSemaphore_A = NULL;
#define printMsg(taskhandle,str) {\
Serial.print(F("Priority "));\ // Print task priority
Serial.print(uxTaskPriorityGet(taskhandle));\
Serial.print(F(" : "));\
Serial.println(F(str));\ // Print user string
}
void setup()
{
Serial.begin(9600);
Serial.println(F("In Setup function, Creating Binary Semaphore"));
vSemaphoreCreateBinary(binSemaphore_A); /* Create binary semaphore */
if(binSemaphore_A != NULL)
{
Serial.println(F("Creating low priority task"));
xTaskCreate(LPT_Task, "LPT_Task", 100, NULL, 1, &LPT_TaskHandle);
}
else
{
Serial.println(F("Failed to create Semaphore"));
}
}
void loop()
{ // Hooked to Idle Task, will run when CPU is Idle
Serial.println(F("Loop function"));
delay(50);
}
/*LPT: Low priority task*/
void LPT_Task(void* pvParameters)
{
printMsg(LPT_TaskHandle,"LPT_Task Acquiring semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(LPT_TaskHandle,"LPT_Task Creating HPT");
xTaskCreate(HPT_Task, "HPT_Task", 100, NULL, 3, &HPT_TaskHandle);
printMsg(LPT_TaskHandle,"LPT_Task Trying to take sempahore again");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(LPT_TaskHandle,"LPT_Task Finally Exiting");
vTaskDelete(LPT_TaskHandle);
}
/*HPT: High priority task*/
void HPT_Task(void* pvParameters)
{
printMsg(HPT_TaskHandle,"HPT_Task Trying to Acquire the semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(HPT_TaskHandle,"HPT_Task Acquired the semaphore");
printMsg(HPT_TaskHandle,"HPT_Task Releasing the semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(HPT_TaskHandle,"HPT_Task About to Exit");
vTaskDelete(HPT_TaskHandle);
}

BinarySemaphoreNonRecursiveLock.png

  1. LPT starts running and acquires the semaphore.
  2. Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
  3. LPT starts executing again and tries to take the same semaphore. Since the semaphore is not available, it gets blocked.
  4. Finally the scheduler is left out with the idle task and it keeps running. The LPT and HPT will in blocked state until someone releases the semaphore.


Have an opinion, suggestion , question or feedback about the article let it out here!

Setting Up FreeRTOS on Arduino

In this tutorial, we will see how to setup the Arduino IDE for FreeRTOS. After setting up the IDE, we will run the first FreeRTOS example on Arduino Uno. Setting Up Download...

Task Switching

In this tutorial, we will see what is a task and its different states. Later we will see how to create a FreeRTOS tasks and finally we will write a simple example to see how the task works and task...

Task Idle Hook

In the earlier example, we created 2 high priority task and an Idle task. In this tutorial, we will see how to use the scheduler Idle task to run the user function. This can be used to monitor...

Deleting a Task

In earlier tutorials, we saw how to create tasks and used. In this tutorial, we will see how to delete a task once its job is done. API Details Here we will discuss...