counting semaphores and event flags

bkastel1 wrote on Sunday, September 09, 2007:


i read some time ago a tread about counting semaphores and event flags. Is there something new about event flags and what would be the best way to do the counting semaphore? Thank you.

bkastel1 wrote on Monday, September 10, 2007:


rtel wrote on Monday, September 10, 2007:

Sorry for the delay - other people normally respond before me but I guess I’m the only one that can answer this question.

Event flags are not on the radar just yet, although they will be added when time permits.  Counting semaphores on the other hand can be implemented easily with some small extensions to the binary semaphore implementation.  Binary semaphores are implemented using a queue that can hold 1 item.  Taking the semaphore removes the item from the queue, giving the semaphore places the item back in the queue.  As there is only space for one item the semaphore is binary.  If you created a queue with more spaces, then you have the effect of a counting semaphore.

As an example, if you wanted a counting semaphore of 3, then create the queue with 3 spaces.  Three tasks can then ‘take’ the semaphore before the queue is empty (no more semaphores available).

[see the macro definitions within semphr.h, hopefully you can see how this simple mechanism works]

The V4.5.0 adds a different type of mutex, and some new queue functionality.


bkastel1 wrote on Monday, September 10, 2007:

Thank you. I have also sent you an email before I saw your answer hear. Sorry :slight_smile: I’m checking that macros for binary semaphore and I’m already working on implementing a macro for couting semaphore. Mutexe is also a great feature and I must just say that the work you do one the kernel is really great. I just wish you won’t stop doing it :slight_smile:

Thanks again.


bkastel1 wrote on Monday, September 17, 2007:


one question regarding counting semaphores. When semaphore is created all resources are available in the begining. What must I do to have all resouces taken on the semaphore creation? It is very useful to have both options available, let say:

vSemaphoreCreate( xSemaphoreHandleName, 128, 0 ); 128 is the maximum size and 0 means that none of the resources are available.


vSemaphoreCreate( xSemaphoreHandleName, 128, 128 ); 128 is the maximum size and 128 means that all of the resources are available.

Thank you very much.

Best regards,


davedoors wrote on Tuesday, September 18, 2007:

Just add a loop to ‘take’ the semaphore the required number of times either from you application code after the semaphore is created, or by extending the macro to do this within the create macro itself.  This is what the binary semaphore create macro does, creates the semaphore then takes it before returning.

bkastel1 wrote on Wednesday, September 19, 2007:

Is’t here an easier solution? Like setting something in the queue’s? Or is there too many things to do?

Thank you