OSSemPendNoWait

Discussion to talk about software related topics only.
Post Reply
ckoehler
Posts: 81
Joined: Sat Mar 13, 2010 9:04 pm

OSSemPendNoWait

Post by ckoehler »

Hi,

When I use OSSemPendNoWait, and the semaphore is 0, i.e. has not been posted to, it will return OS_TIMEOUT, right? And then move on?

I want to use it in a loop and have some other task post to the semaphore when I need to break out of the loop.

Just checking, thanks!


Christoph
User avatar
pbreed
Posts: 1081
Joined: Thu Apr 24, 2008 3:58 pm

Re: OSSemPendNoWait

Post by pbreed »

That is correct.

You can just use a boolean rather than a sem.
If the variable is touched by more than one task you MUST mark it volatile.
ie

volatile BOOL bRunUntilThisIsFalse;

If you don't mark it volatile the compiler will note that nothing in your while loop changes it and
optimize out the check.
ckoehler
Posts: 81
Joined: Sat Mar 13, 2010 9:04 pm

Re: OSSemPendNoWait

Post by ckoehler »

Interesting, I didn't think about that. I guess when I hear "task" I think "semaphore/critical section/etc". So there won't be any problems with just a volatile boolean if two tasks want to access it? Nice!
User avatar
pbreed
Posts: 1081
Joined: Thu Apr 24, 2008 3:58 pm

Re: OSSemPendNoWait

Post by pbreed »

Semaphores etc are good if you want one task to go to sleep waiting on the other.
The boolean will not make the other task sleep, but we have a flat memory model so as long as its declared volatile then
two tasks can access it.

The things to watch out for:

1)The variable has to be atomic, IE changed in one access IE smaller than 32 bits. A double, or char * pointer are not good candidates for this.

2)The variable needs to be modified by one task and monitored by another. does not work well if both are modifying the variable.

3)A ring/circular buffer can work like this.
The key is that one side modifies the GET pointer and another modifies the PUT pointer.
The contents of the buffer need to be modified BEFORE the PUT pointer is moved.
When a ring buffer is Full and you try to add something you normally have two choices, bump the get pointer (loosing the oldest element) or toss the new addition.
If you are doing the two task (or interrupt and task) buffer then your only choice is to toss the new addition as the put side can't touch the get pointer.

4)A linked list does not work well this way it has to be protected with a MUTEX or Critical Section.
Post Reply