Quality RTOS & Embedded Software

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




Loading

Why vSemaphoreCreateBinary macro 'give' immediately after created the semaphore?

Posted by rainbowsnow on November 30, 2015

I found the below definition in the source code of FreeRTOS v7.0.2.

I have known that vSemaphoreCreateBinary has been replaced by xSemaphoreCreateBinary now and won't 'give' the semaphore after created it, but I still want to know why vSemaphoreCreateBinary does this.

~~~~

define vSemaphoreCreateBinary( xSemaphore )
{																																			\
	( xSemaphore ) = xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE );	\
	if( ( xSemaphore ) != NULL )																											\
	{																																		\
		( void ) xSemaphoreGive( ( xSemaphore ) );																									\
	}																																		\
}

~~~~

Thanks!


Why vSemaphoreCreateBinary macro 'give' immediately after created the semaphore?

Posted by rtel on November 30, 2015

Probably because the [now defunct, as you point out] macro is very old, and dates back to a time before there was a separate mutex object - meaning binary semaphores were sometimes used as a mutexes. When a semaphore is used for mutual exclusion, you assume the resource being protected starts in the available state, so the mutex starts in the available state. Now there are separate binary semaphore and mutex objects, so when the very old semaphore macro was replaced by a function (so its semantics were consistent with the newer functions) it is created in the not available state - which is more logical when the semaphore is used for signalling. In more recent versions of FreeRTOS it is preferable to use direct to task notifications for signalling, as they are faster and use less RAM, so binary semaphores don't get much use in new applications.


Why vSemaphoreCreateBinary macro 'give' immediately after created the semaphore?

Posted by rainbowsnow on December 2, 2015

Got it, thanks very much ^_^


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




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

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

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