Pumpkin, Inc.

Pumpkin User Forums

Semaphores

If you can't make Salvo do what you want it to do, post it here.

Semaphores

Postby aek » Mon Jun 25, 2001 9:18 am

I wouldn't recommend that approach ... I'll explain why below.

If only a single task is waiting the semaphore, then

code:
...
OS_WaitSem(SEM, label1);
OS_WaitSem(SEM, label2);
...

would certainly work. But

code:
...
OS_WaitSem(SEM, label);
if ( OSTrySem(SEM) == x) { ... }
...

would be much faster and would require less ROM. You may be able to detect an "error" via the return value of OSTrySem().

NOTE: The problem you may run into is that interrupts are enabled between the first and second calls in both cases above, because interrupts are disabled at the beginning of OS_WaitSem() and are re-enabled at the end of OS_WaitSem(), since OS_WaitSem() is a critical section of code. This means that OSSignalSem() can occur (if called from an ISR) after the first decrement of the sem and before the second. IOW, by using two consecutive operations to decrement the semaphore, you no longer have an "atomic" situation, and it's likely to cause problems -- it violates the basic rules of using semaphores.

There is, however, a solution to this in Salvo -- use the 'a' ('a' for "OSFROM_ANYWHERE") series PIC libraries, i.e. the ones compiled for calls to OSSignalXyz() from both the foreground and the background. These libraries (and the associated macros OSEnableNestedInts() and OSDisableNestedInts()) keep track of the state of interrupts and restore the "previous state". You could use them like this:

code:
...
OSDisableInts();
OS_WaitSem(SEM, label);
OSTrySem(SEM);
OSEnableInts();
...

In this case, interrupts will remain disabled throughout OS_WaitSem() and OSTrySem().

------------------

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Re: Semaphores

Postby luben » Mon Jun 25, 2001 9:53 am

Hello,

Semaphores are like counters and after every signalling they increment their value. If I have a task that process semaphores and need to decrement their value by 2, not by 1, what I shopuld do - to use twice OS_WaitSemaphore() or OSTrySemaphore().
Or there is other way to decrement values of semaphores by any value? Well, I agree, that the "SALVO style" recommend to use semaphores like counters that could be increment/decrement by 1. But you know - sometimes we have to break the rules....

Regards
Luben

luben
 
Posts: 324
Joined: Sun Nov 19, 2000 12:00 am
Location: Sofia, Bulgaria

Re: Semaphores

Postby luben » Mon Jun 25, 2001 10:13 am

Hello,

I think that your approach is the right one.

Or other solution is to use a bit flag to enable signaling of the semaphore. The problem is if semahores signaled from ISR - then you should care for the case if event occured, but flag disables signaling of the semaphore.... you have to try signal it after some time, that usually is made in timer routines.

Regards
Luben

luben
 
Posts: 324
Joined: Sun Nov 19, 2000 12:00 am
Location: Sofia, Bulgaria


Return to Coding

Who is online

Users browsing this forum: No registered users and 2 guests

cron