Say I want to wake up a task after n separate events have occurred. Is it legal to initialize a semaphore to 1 - n, and down() it, so I wake up after each of the events have up()'d it?
I don't think so.
(1) The semephore.count is declared as unsigned int. See semaphore definition:
struct semaphore {
spinlock_t lock;
unsigned int count;
struct list_head wait_list;
};
(2) The down() function will check the count value before decrease it, make sure the count is not negative.
Unless you implement one mechanism, you can not use semaphore directly to accomplish your requirement.
That is not a good idea because it is unsigned. Also, a semaphore is only activated when it is a positive number so having a large number caused by initializing the semaphore to a negative number will cause your semaphore to allow access to something that you would want to be restricted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With