Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


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?


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 ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists