Quality RTOS & Embedded Software

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


Loading

What happens if semaphore is given before it is taken?

Posted by bupthebroker on March 24, 2016

Hi, what happens with binary and counting semaphores in the following scenario:

1) Task1 gives semaphore 2) Task 2 tries to take semaphore

Task1 gave the semaphore before task2 tried to take it. Does task2 succceed in taking the semaphore or does task1 need to give it again?

Thanks!


What happens if semaphore is given before it is taken?

Posted by richard_damon on March 24, 2016

There is a bit of a differnce on the type of semaphore, but for a binary semaphore, when Task1 gives the smeaphore it goes into the ready to be taken state (even if it is already there). With a counting semaphore, the ready count increases (unless it is at max).

When Task2 takes the semaphore, it will be ready so Task2 will proceed.

This is one of the big differences between using a semaphore and having task2 suspend itself and task1 resume it. That method does require that Task2 has done the suspend before Task1 does the Resume.


What happens if semaphore is given before it is taken?

Posted by bupthebroker on March 24, 2016

Okay, thanks for the clarification. Is this written in some of the freertos docs as well? I did not find it explicitly stated anywhere that once a semaphore is given it can be taken EVEN if the taker was not trying to take it at the time of giving.


What happens if semaphore is given before it is taken?

Posted by heinbali01 on March 25, 2016

This is a property of semaphores (and message queues) in every OS. At the moment a semaphore is given, there does not have to be a taker. The semaphore is patient and will keep its (given) state until some task will take it again. When two or more tasks try to take a binary semaphore, the task with the highest priority will get it.


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


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

Latest News

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

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

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


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

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