# Return value of xSemaphoreGive()

**URL:** <https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552>\
**Category:** Kernel\
**Created:** [April 17, 2020, 12:46am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552 "2020-04-17T00:46:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiuhao2019](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jiuhao2019/32/39_2.png) [@jiuhao2019](https://forums.freertos.org/u/jiuhao2019)\
**Post date:** [April 17, 2020, 12:46am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/1 "2020-04-17T00:46:50Z")

</div>

hi, I am confused by the words of RM10:  
BaseType\_t xSemaphoreGive( SemaphoreHandle\_t xSemaphore );  
pdPASS: The semaphore ‘give’ operation was successful.  
pdFAIL: The semaphore ‘give’ operation was not successful because the task calling  
xSemaphoreGive() is not the semaphore holder. A task must successfully ‘take’ a  
semaphore before it can successfully ‘give’ it back.  
I was thinking like this: if it return fail, it mean the semaphore was full already. because every task can give to a semaphore, even it hasn’t take any of the same semaphore before ,when the semaphore is not full.am I right?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 17, 2020, 1:01am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/2 "2020-04-17T01:01:27Z")

</div>

I don’t know what RM10 is. This is the (rather old) documentation for xSemaphoreGive() [https://www.freertos.org/a00123.html](https://www.freertos.org/a00123.html)

---

<div class="post-metadata">

**Author:** ![Xavier](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@Xavier](https://forums.freertos.org/u/Xavier)\
**Post date:** [April 17, 2020, 1:16am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/3 "2020-04-17T01:16:19Z")

</div>

A task cannot “give” a semaphore that didn’t “take” it. The task TA takes the semaphore SA, but if TA tries to give a semaphore other than SA, then the function will return pdFAIL.

---

<div class="post-metadata">

**Author:** ![jiuhao2019](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jiuhao2019/32/39_2.png) [@jiuhao2019](https://forums.freertos.org/u/jiuhao2019)\
**Post date:** [April 17, 2020, 1:19am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/4 "2020-04-17T01:19:19Z")

</div>

thank you for reply fast.  
RM10 refer to “FreeRTOS\_Reference\_Manual\_V10.0.0.pdf”  
you give the link, use a example of mutex. but BaseType\_t xSemaphoreGive( SemaphoreHandle\_t xSemaphore ); can use with binary and counting semaphores.  
the “fail” reason just say using mutex,not give clearly what’s the reason if the situation of using binary and counting semaphore.

---

<div class="post-metadata">

**Author:** ![jiuhao2019](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jiuhao2019/32/39_2.png) [@jiuhao2019](https://forums.freertos.org/u/jiuhao2019)\
**Post date:** [April 17, 2020, 1:28am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/5 "2020-04-17T01:28:21Z")

</div>

yes ,if using a mutex, but if using a binary and counting semaphore. if it return pdFAIL, can not say that it is must because the giving task is not a holder, it can because the semaphore already be full.

---

<div class="post-metadata">

**Author:** ![Xavier](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@Xavier](https://forums.freertos.org/u/Xavier)\
**Post date:** [April 17, 2020, 1:44am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/6 "2020-04-17T01:44:46Z")

</div>

It doesn’t matter who created the semaphore. From the documentation:

> The semaphore must have previously been created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or xSemaphoreCreateCounting().

A counting semaphore can get full only if it’s “taken” so many times. A counting semaphore can get empty only if it’s “given” so many times.

From the documentation (again). Look for the last part (in bold):

> xSemaphoreGive( SemaphoreHandle\_t xSemaphore );
> 
> Macro to release a semaphore. The semaphore must have previously been created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or xSemaphoreCreateCounting().
> 
> **This must not be used from an ISR**. See xSemaphoreGiveFromISR() for an alternative which can be used from an ISR.
> 
> This macro must also not be used on semaphores created using xSemaphoreCreateRecursiveMutex().
> 
> Parameters:  
> xSemaphore A handle to the semaphore being released. This is the handle returned when the semaphore was created.
> 
> Returns:
> 
> **pdTRUE if the semaphore was released. pdFALSE if an error occurred. Semaphores are implemented using queues. An error can occur if there is no space on the queue to post a message – indicating that the semaphore was not first obtained correctly**.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 17, 2020, 1:53am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/7 "2020-04-17T01:53:23Z")

</div>

I just downloaded the reference manual and you are right - the description is only valid for mutex type semaphores, so without that qualification is basically wrong.

---

<div class="post-metadata">

**Author:** ![jiuhao2019](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jiuhao2019/32/39_2.png) [@jiuhao2019](https://forums.freertos.org/u/jiuhao2019)\
**Post date:** [April 17, 2020, 4:22am UTC](https://forums.freertos.org/t/return-value-of-xsemaphoregive/9552/8 "2020-04-17T04:22:43Z")

</div>

Thanks for confirm this.
