Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Semaphore was taken although it was never given

Posted by sarahaka on February 24, 2019

In my main function, I create a semaphore as so: ~~~ vSemaphoreCreateBinary(xSemaphoreISR);

if (xSemaphoreISR == NULL){ //log error printf("t Failed to obtain semaphore for ISR... nr"); } ~~~

After I create the tasks, the tasks run. In one of the task I do the following: ~~~ while(1){

if( xSemaphoreTake( xSemaphoreISR, portMAX_DELAY ) == pdPASS ){
      printf("\n\r Safe Mode: Semaphore succesfully taken from ISR \n\r");

} ~~~

On the first run, surprisingly it does obtain the semaphore even though non of the other tasks gave it. How come?

Semaphore was taken although it was never given

Posted by richard_damon on February 24, 2019
Binary Semaphores are created either full or empty depending on which function you use to create them. The vSemaphoreCreateBinary leaves the semaphore set on creation, so it is expected that the first take will work. This behavior works right if the semaphore is being used as a resource protection mechanism. If you use xSemaphore create binary, then the Semaphore is created empty, which is what you want for a syncronization semaphore like you are using.

The first usage may be better using a mutex to handle the priority inversion issue, the second can sometimes use the direct to task notification system if it will always be a particular task being notificed.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS v10.2.1 is available for immediate download. MIT licensed, includes 64-bit RISC-V, NXP Cortex-M33 demo, Nuvoton Cortex-M23 demo & STM32H745 dual core (AMP) demo.

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Cadence Tensilica Cortes

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers





STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner