Page 1 of 3
Multiple calls of OSSemInit()
Posted: Tue Mar 15, 2011 10:10 am
by angelo.emanuelli
Hi!
Can I call OSSemInit() multiple times to the same struct OS_SEM ?
The source code that I´m taking a look does this to "reset" the Semaphore.
My doubt is if this operation consumes extra resources from my uC.
Thank´s in advance.
Re: Multiple calls of OSSemInit()
Posted: Tue Mar 15, 2011 2:28 pm
by Ridgeglider
I'll defer to Larry or Paul, but I tend to call it OSSemInit() once/semaphore at the start of the app. If you realy need to clean out all the pending events, why not put an OSSemPend() call in a while loop and exit when it times out. if you do this, you'd have to elevate the priority of the task doing this cleanup to a higher priority than the task posting to the semaphore to prevent the posting task from adding to the event list during the cleanup. Not sure why you'd post events that you don't want though...
Re: Multiple calls of OSSemInit()
Posted: Wed Mar 16, 2011 6:03 am
by angelo.emanuelli
Thanks for the response, Ridgeglider.
In fact we are using OSSemPend() but when the function was writen the
semaphore is always initialized with OSSemInit() as following:
Code: Select all
u8 foo(void)
{
OSSemInit(&sem,0);
...
...
...
st = OSSemPend(&sem,timeout); //Waiting for other task to post
return st;
}
In some circumstances this function is called periodically and I think this is the cause of our software´s instability.
Re: Multiple calls of OSSemInit()
Posted: Wed Mar 16, 2011 6:40 am
by Ridgeglider
Typically the OSSemInit would be done once, usually shortly after boot when things are getting set up. The OSSemPend() call is usually in a freestanding task created to handle the response to a semaphore posted by some other task or ISR in reponse to an event. Usually the form of this pending task is a while (1) loop that runs forever. At the top of this loop is a call to OSSemPend(). This call allows the code to either quickly respond to the semaphore, or to "block" allowing other tasks to run if there is no semaphore. In other words, if there is no semaphore posted, nothing happens. Once again, take a look at the NNDK programmer's manual to see how to set up these tasks and how to use semaphores. They're easy and very useful.
Re: Multiple calls of OSSemInit()
Posted: Wed Jan 16, 2013 6:55 am
by DBrunermer
As a follow-up question, I understand you should only need to initialize the semaphore once, but can you call OSSemInit more than once on the same signal? I have a case where I think I'm going to have one extra Post, and I'd like to clear it before I restart my sequence. Is that OK, or does it fundamentally cause problems?
Re: Multiple calls of OSSemInit()
Posted: Wed Jan 16, 2013 7:55 am
by dciliske
Short answer: no, it will not consume additional resources. Open up 'ucos.c' in the system directory to find the source for OSSemInit() and you'll understand why. That said, unless you're doing a full reset on some large submodule, I'm not convinced that using it in this manner is correct. Even then, it still feels wrong. The reason is that when you call OSSemInit it essentially resets the semaphore to nothing, wiping out any of scheduling data on the semaphore, but without restarting the pending tasks. To determine exactly what would happen, I'd have to do some digging in the scheduling system, but it's deeply connected enough to convince me that it's probably not a smart thing to do.
Re: Multiple calls of OSSemInit()
Posted: Wed Jan 16, 2013 8:47 am
by pbreed
If no tasks are pending on the semaphore, it does no harm.
However from a code maintenance standpoint I see it as a bad idea, because in the future you may never be sure
no tasks are pending..
Re: Multiple calls of OSSemInit()
Posted: Wed Jan 16, 2013 12:37 pm
by DBrunermer
So I did what you said and looked at the UCOS.C file. It looks like in the case I was hoping to prevent it would fail anyway. But, looking at the other functions, I have a new question. What is / How can I find out more about the interplay of Semaphores and Task Priorities? Can I have more than one semaphore in an application? I'm having a problem where I have a function in a task at priority Main-3, and it's Pending. The function that calls the OSSemPost (INT5) is definitely getting called, but the task that's Pending never moves forward. I can post the code if you think that'd help. Thanks! Dan B.
Re: Multiple calls of OSSemInit()
Posted: Wed Jan 16, 2013 1:02 pm
by Ridgeglider
Take a look at C:\nburn\docs\NetworkProgrammersGuide\nnDKProgManual.pdf
Also at C:\nburn\examples\RTOS\OSSemaphore
Re: Multiple calls of OSSemInit()
Posted: Wed Jan 16, 2013 3:16 pm
by rnixon
If the init is in your function:
u8 foo(void)
{
OSSemInit(&sem,0);
...
...
...
st = OSSemPend(&sem,timeout); //Waiting for other task to post
return st;
}
How do you guarantee the OSSemPost() isn't called before foo() and the initialization? That is the reason I initialize early on in the program.