Semaphore Flush Function?

Hey all, first time posting.

I was wondering if FreeRTOS had a semaphore flush function, where it unblocks every task pended on a semaphore? I know it’s present in VxWorks and RTEMS, and wondering if there is a similar function (or method of implementation) available.


I think you could use xTaskAbortDelay to forcefully unblock tasks.
I’m not aware of a way to do so for tasks blocked on a specific semaphore.

Unless there is a way to generate a list of tasks which is pending on a specific semaphore, I won’t be able to use xTaskAbortDelay unfortunately.

Thanks for the help!

Flushing doesn’t change the value of the semaphore, maybe a workaround is to decrease until empty, then reset to its original value? However, I’m not sure what issues could arise with that method.

It is possible to reset a queue (which also works for semaphores as they use the same underlying mechanism) but I don’t think that is what you want. Also, just removing a task from a semaphore’s blocked list is not enough as the task will re-block on the same semaphore unless its block time has expired. Hence really you need a combination of the queue reset and abort delay functionality - and that doesn’t currently exist. (abort delay lets the task know not to re-block).

We would be happy to consider a pull request that does this if you have an implementation :slight_smile:

Actually, I’m still trying to decipher a use case for that functionality. After all, a sempahore protectes something, such as a two track railway section from being entered by three or more trains, right? So if there was an outside logic to let the tasks resume to their code past waiting for the protection “green light,” wouldn’t that translate to allowing more than two trains (which by definition are already in the section) to enter the section?

Edit: Assuming a valid use case (which I’m sure you’ll have), I’d argue that the best (since most generic) addition to the API would be an equivalent to Windows’ WaitForMultipleObject(,WAIT_EITHER,) semantics which we discussed here a few weeks ago.

Also if the semaphore isn’t used as protection but as signaling mechanism I’m not aware of a use case with multiple concurrent waiters for a single semaphore.
What exactly do you want to achieve ?

For context, I’m currently attempting to port a library from RTEMS/VxWorks to FreeRTOS, and one of the library functionalities included flushing a binary semaphore. I’m not quite too sure about how it will be used since it’s out of the scope of my work, but I might be able to just leave it for now until it’ll actually be used.

The big question would be do semaphores work the same what there or is that more of an event group like operation, allowing multiple task to wait for. A single event at which point they all get released.

Hopefully you don’t need to port it :slight_smile: I think this feature is pretty specific to those 2 OSs.
At least I don’t think it’s supported by other OSs I had to deal with in the past.
However, in case you need it it’s possible to add it as Richard explained:

Yea, really hoping it becomes a case of it never gets used, if not, I’ll be sure to return with a working implementation, and maybe get a pull request going!

Thanks all for the help, really appreciate the support :slight_smile: