Hassan
Hassan

Reputation: 11

Understanding the function of semFlush

What I found about semFlush(semID) function is that it unblocks all tasks waiting for semID without changing the state of the semaphore.

What I don't understand is what does it mean to unblock tasks and what is the point of doing that since the semaphore will not change its state (i.e. still empty or not available so that tasks won't be able to take the semaphore)?

When I say that task are blocked for the semaphore, does that mean that the semaphore has been taken (i.e. not available yet)?

Any help would be appreciated as my English seems to be failing me at the moment.

Upvotes: 1

Views: 455

Answers (1)

haxonek
haxonek

Reputation: 301

This took me a while to figure out too, so I'm just going to put everything here for anyone else reading.

So you have a semaphore. This has an internal counter, and blocks/calls other functions waiting on it. If you semTake, you decrement the counter and if it's non-negative, you continue in the code block. When you semGive, it increments the counter and unblocks any operations that were blocked. It's important to remember these don't just have to operate like locks, but they can be used to have any number of things happen. Some use cases may be in the boot up of an operating system, where you're waiting on a semaphore for a network driver to come online before allowing everything else to run. Or maybe you want to limit 10 items operating at once, you set the start to 10 and each channel will take one away from that until it's a negative number and they're forced to wait, blocked, until they can resume operations.

semFlush (as part of the VxWorks semLib library) will unblock everything and reset the semaphore. The library forbids it from being used on locks, and this is instead intended to be used to synchronize an action (such as the boot-up of an OS), or when you want to allow access to something regardless of everything else (such as flushing out a semaphore before performance or unit testing).

You can read more about it in their official docs or in this other post I found helpful. If anyone in the future has additional insight I'm sure that will be helpful. Lastly you can look up the source code online but it's pretty brutal and not very helpful.

Upvotes: 1

Related Questions