# xSempahoreTake Returns pdTRUE without Give?

**URL:** <https://forums.freertos.org/t/xsempahoretake-returns-pdtrue-without-give/2290>\
**Category:** Kernel\
**Created:** [March 22, 2012, 5:38pm UTC](https://forums.freertos.org/t/xsempahoretake-returns-pdtrue-without-give/2290 "2012-03-22T17:38:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 22, 2012, 5:38pm UTC](https://forums.freertos.org/t/xsempahoretake-returns-pdtrue-without-give/2290/1 "2012-03-22T17:38:05Z")

</div>

**[phenomx](https://sourceforge.net/u/phenomx/)** wrote on [Thursday, March 22, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/69ff4b06/#cf2e):

Hi guys,

I just recently started using FreeRTOS with the MSP430, and I don’t understand why my implementation of a binary semaphore is not behaving as expected. From my understand, I first create the binary semaphore which is initially blocked until a xSemaphoreGive is done.

In the code below, I want to loop until reset\_count becomes 4 within the timer interrupt vector. Only then I want to break from the while loop. the xSemaphoreTake() returns pdTRUE a give has not been given. Why is this happening? Is my understanding of semaphores wrong?

[code  
while( !reset\_flag )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Checking if there any SemGive of the user semaphore \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( pdTRUE == xSemaphoreTake(xUserSemaphore, 0xFFFF))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

```
// Timer A1 TAxCCR0 Interrupt
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR( void )
{ /* CCR0 will fire this interrupt */
	/* set flag for which timer interrupt is being used */
        signed portBASE_TYPE xHigherPriorityTaskWoken;
        
        if( ++reset_count == 4 )
        {
            xHigherPriorityTaskWoken = pdFALSE;
            xSemaphoreGiveFromISR(xUserSemaphore,
                                              &xHigherPriorityTaskWoken);
            reset_flag = 1;
        }
}

```

Thanks,  
-Adrian

---

<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:** [March 22, 2012, 8:01pm UTC](https://forums.freertos.org/t/xsempahoretake-returns-pdtrue-without-give/2290/2 "2012-03-22T20:01:17Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, March 22, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/69ff4b06/#ad9d):

Binary semaphores are created with the semaphore available.&nbsp; Therefore the first take will succeed no matter what.&nbsp; Add a call to xSemaphoreTake(xUserSemaphore, 0) immediately after the semaphore is created.&nbsp; That way the semaphore will not be available until the ISR has given it, which is I think what you want.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 22, 2012, 9:44pm UTC](https://forums.freertos.org/t/xsempahoretake-returns-pdtrue-without-give/2290/3 "2012-03-22T21:44:47Z")

</div>

**[phenomx](https://sourceforge.net/u/phenomx/)** wrote on [Thursday, March 22, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/69ff4b06/#9b2c):

Thank you for the clarification,

That was just what I needed!
