Counting semaphores.... of the second kind....

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Counting semaphores.... of the second kind....

Post by rew »

We have a situation where we want to wait for something to finish. But in fact, we need N sub-parts to finish.

To implement this "neatly" I thought there was a type of counting semaphore that allows me to:

* decrement it below zero without waiting. (N times)
* the "create jobs" process then waits on the semaphore.
* the "doit" process increment it each time it finishes something.
* the "create jobs" process is woken up after N jobs have finished. i.e. after the Nth increment causes the count to reach zero again.

We now count the number of jobs externally and wait N times on the semaphore. I think this is not as neat as can be.

We could also count the number of jobs. Increment it for each job submitted, and decrement it for every job finished. The we only signal the semaphore on finishing the last job. The count now gets modified in both the job-creator, and the job-finisher, leading to race conditions. (or, requiring a critical section for updating the count). Ugly IMHO.

Is this an unusual way to use semaphores? Is there a neat way to implement this?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Counting semaphores.... of the second kind....

Post by Giovanni »

You could use events: chEvtWaitAll().

It exits only when all the specified flags are set (an AND condition).

Giovanni
Post Reply