This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Semaphore question

Guru 15580 points

The bios 6 user guide says:

Counting semaphores keep an internal count of the number of corresponding resources available.

What "resources" is this referring to?

The online help says:

Counting semaphores keep track of the number of times the semaphore has been posted with post().

I was under the impression that when a counting semaphore was posted, that its count increased by 1. However, when running the "log" example project and watching the value of sem0 in ROV, its value does not increment when it gets posted. Am I misunderstanding something?

Thx,

MikeH

 

  • Hi Mike, The term "resource" is generic here. It's really whatever the user of the semaphore is protecting. For example, in the Mailbox module, when a message is put into a Mailbox, the semaphore is incremented. Here the resource is a message in the Mailbox. A count of zero in a Semaphore means that no resource is available (e.g. no one has posted the Semaphore yet). When a Semaphore_post occurs, a counting Semaphore is incremented by 1. A binary Semaphore is set to 1. If the count is 0, a Semaphore_pend blocks for the duration that is specified or until a Semaphore_post occurs. So what you thought is correct. So why don't you see the count go to 1 after the Semaphore_post? I ran the Log example and put a breakpoint at the Semaphore_post and Semaphore_pend. The Semaphore_pend code was hit first. The count was zero (as expected). I continued to run the program and hit the Semaphore_post breakpoint. The count was still zero (as expected). There was now a pending Task (as expected). Then I stepped over the Semaphore_post in the debugger. The count was still zero! This is correct since the step over actually released the Task that was blocked on the Semaphore_pend. Internally in the Semaphore_post, it saw there was a pending task. So instead of incrementing the count, it scheduled the pending Task to run. Since the pending Task was higher priority, it actually ran and completed before the debugger stopped at the line after Semaphore_post. For fun, I added an second Semaphore_post(sem0) in the example. When the second Semaphore_post ran, with no pending Tasks, the count was incremented. Todd
  • Todd, Thanks for taking the time to look into this. In fact, that is exactly what I ended up doing (adding a second sem). It's (once again) marginal documentation that had me confused. I appreciate your feedback and well-written response. MikeH