# Counting semaphore - my wrong application

**URL:** <https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634>\
**Category:** Kernel\
**Created:** [January 22, 2019, 8:08pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634 "2019-01-22T20:08:23Z")\
**Posts on this page:** 7\
**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:** [January 22, 2019, 8:08pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/1 "2019-01-22T20:08:23Z")

</div>

**[yevpator75](https://sourceforge.net/u/yevpator75/)** wrote on [Tuesday, January 22, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c):

Hi,

I believe I am improperly using the counting sempahore which is the reason for the problem I am facing now,

I implemented a circular queue by myself and have 8 instances of such a queue, which are 2 queues per each of 4 tasks. 8 = 2 x 4.

I wanted every task to wake up once a new message from other tasks arives at any of its 2 queues. It checks then each of these 2 queues according to predefined priority , pulls a new message and starts processing it.

I assigned a counting sempahore to each of these 4 tasks (4 semaphores). Every time a new message arrives at any of the 2 queues I thought to “give” the semaphore with xSemaphoreGive, which in fact increments it, and in each of these 4 tasks I intended to block with xSemaphoreTake until the semaphore count increases .

My problem is when the count of the semaphore is 0, xSemaphoreTake returns with 0 and then I get configASSERT(0). I get it actually immediately after initialization, because the initial semaphore value is 0.

The question is whether my application is wrong or using of semaphore is not applicable here?

Thanks!

---

<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:** [January 22, 2019, 9:17pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/2 "2019-01-22T21:17:41Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, January 22, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c/f66d):

Can you please show the code where you are blocking on the semaphore.

---

<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:** [January 22, 2019, 9:19pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/3 "2019-01-22T21:19:36Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, January 22, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c/164d):

As an aside…another alternative is to just use a single queue and no  
semaphores. If the data held in the queue says what the data is and/or  
where it came from, in addition to whatever the value of the data is,  
then often a single queue is all that is needed. See the 'Alternatives  
to using queue sets" section on this page:  
[https://www.freertos.org/Pend-on-multiple-rtos-objects.html](https://www.freertos.org/Pend-on-multiple-rtos-objects.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 23, 2019, 4:37pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/4 "2019-01-23T16:37:57Z")

</div>

**[yevpator75](https://sourceforge.net/u/yevpator75/)** wrote on [Wednesday, January 23, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c/164d/def5):

Thank you Richard,

I get a real data of a variable size from an upper layer of some protocol. The data can be very small or big. If I use the FreeRTOS queue to puch there the datga of the maximal size, it will be not efficient, since in the most cases the data length is short. If I queue only the pointer , I still need some queue to be implemented by myself according to my specific requirements. I did that and looked only for a means to wakeup the pending task.  
Becausue the counting semaphore didn’t work for me , finally I did use the queue. While for now it is not principal for me if the waking up method is counting semaphore or queue, I thought queue is indeed a better option for me baring a certain feature in mind. I still will answer you other question since I am learning now and it is important for me to understand the stuff.

Just currious whether you see a merit in some new features for FreeRTOS, like a queue with variable size. I don’t know if other RTOS do provide something like that, will check that later.

Best regards,  
Eli

---

<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:** [January 23, 2019, 4:51pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/5 "2019-01-23T16:51:13Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, January 23, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c/164d/def5/75db):

> Just currious whether you see a merit in some new features for FreeRTOS,  
> like a queue with variable size. I don’t know if other RTOS do provide  
> something like that, will check that later.

FreeRTOS Message buffers allow variable size data to be [effectively]  
queued, but only in single sender single receiver scenarios.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 23, 2019, 4:52pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/6 "2019-01-23T16:52:31Z")

</div>

**[yevpator75](https://sourceforge.net/u/yevpator75/)** wrote on [Wednesday, January 23, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c/f66d/9137):

Sure.

```auto
static void * gp_countSemUartTxSom1Handle;
gp_countSemUartTxSom1Handle = xSemaphoreCreateCounting (10,0); 

```

“Upper Protocol Layer”, which is a task , which wants to pass a data to one of the mentioned 4 tasks, adds it to my circular queue and then gives the semaphore (increase the counter):

```auto
res = xSemaphoreGive (gp_countSemUartTxSom1Handle);

```

Each of the 4 tasks pends on its own counting semaphore

```auto
task1
{
    for()
    {
        uint32_t sem = xSemaphoreTake (gp_countSemUartTxSom1Handle, SEM_NO_TIMEOUT);
        configASSERT (pdPASS == sem);
        // proces the new data
        }
}

```

In the beginning , before the sempahore is “given”, xSemaphoreTake returns 0 and the following configASSERT fails. I wanted to wakeup only when the semaphore count is higher than 0.

What is wrong here?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 23, 2019, 4:55pm UTC](https://forums.freertos.org/t/counting-semaphore-my-wrong-application/7634/7 "2019-01-23T16:55:03Z")

</div>

**[yevpator75](https://sourceforge.net/u/yevpator75/)** wrote on [Wednesday, January 23, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/38e7a4444f/#670c/164d/def5/75db/aece):

Yes, I saw this kernel object. I thought about Message buffer , but as an item inside a queue. Again, I am not sure if this is something that makes sense to be included in RTOS.
