Multiple calls of OSSemInit()
-
- Posts: 2
- Joined: Tue Mar 15, 2011 9:53 am
Multiple calls of OSSemInit()
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.
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.
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: Multiple calls of OSSemInit()
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...
-
- Posts: 2
- Joined: Tue Mar 15, 2011 9:53 am
Re: Multiple calls of OSSemInit()
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:
In some circumstances this function is called periodically and I think this is the cause of our software´s instability.
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;
}
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: Multiple calls of OSSemInit()
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.
-
- Posts: 67
- Joined: Thu Apr 21, 2011 7:06 am
- Location: Pittsburgh, PA
Re: Multiple calls of OSSemInit()
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()
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.
Dan Ciliske
Project Engineer
Netburner, Inc
Project Engineer
Netburner, Inc
Re: Multiple calls of OSSemInit()
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..
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..
-
- Posts: 67
- Joined: Thu Apr 21, 2011 7:06 am
- Location: Pittsburgh, PA
Re: Multiple calls of OSSemInit()
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.
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: Multiple calls of OSSemInit()
Take a look at C:\nburn\docs\NetworkProgrammersGuide\nnDKProgManual.pdf
Also at C:\nburn\examples\RTOS\OSSemaphore
Also at C:\nburn\examples\RTOS\OSSemaphore
Re: Multiple calls of OSSemInit()
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.
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.