DSPRelated.com
Forums

Semaphores in DSP/BIOS

Started by Randy Yates May 28, 2010
OK, this may be a stupid question but I'm going to go ahead and ask
it. I seem to be missing something very basic in the use of semaphores.

The SEM module in DSP/BIOS maintains a non-negative count of the number
of times it has been "posted". Then when a pend occurs, the process
either a) blocks if count = 0, or b) decrements count and resumes.

I have one task T1 that must run to completion before other tasks (T2,
..., TN) run.  It *seems* this would be a good use of a semaphore;
create a semaphore SEM_T1, then have each task T2, ..., TN pend on
SEM_T1. Then when T1 completes, it posts to SEM_T1.

However, this won't work with DSP/BIOS semaphores. What will happen is
that the first task that pended, say, T2, will get unblocked when T1
completes, but since there was only one pend by T1, none of the other
T3-TN will unblock.

How would you solve this problem in DSP/BIOS?
-- 
Randy Yates                      % "Watching all the days go by...    
Digital Signal Labs              %  Who are you and who am I?"
mailto://yates@ieee.org          % 'Mission (A World Record)', 
http://www.digitalsignallabs.com % *A New World Record*, ELO
On May 28, 10:24&#4294967295;am, Randy Yates <ya...@ieee.org> wrote:
> completes, but since there was only one pend by T1
Should have been "...since there was only one POST by T1"...
On 28 Maj, 16:24, Randy Yates <ya...@ieee.org> wrote:
> OK, this may be a stupid question but I'm going to go ahead and ask > it. I seem to be missing something very basic in the use of semaphores. > > The SEM module in DSP/BIOS maintains a non-negative count of the number > of times it has been "posted". Then when a pend occurs, the process > either a) blocks if count = 0, or b) decrements count and resumes. > > I have one task T1 that must run to completion before other tasks (T2, > ..., TN) run. &#4294967295;It *seems* this would be a good use of a semaphore; > create a semaphore SEM_T1, then have each task T2, ..., TN pend on > SEM_T1. Then when T1 completes, it posts to SEM_T1. > > However, this won't work with DSP/BIOS semaphores. What will happen is > that the first task that pended, say, T2, will get unblocked when T1 > completes, but since there was only one pend by T1, none of the other > T3-TN will unblock. > > How would you solve this problem in DSP/BIOS? > -- > Randy Yates &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% "Watching all the days go by... &#4294967295; &#4294967295; > Digital Signal Labs &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% &#4294967295;Who are you and who am I?" > mailto://ya...@ieee.org &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% 'Mission (A World Record)',http://www.digitalsignallabs.com% *A New World Record*, ELO
eh ... call SEM_post(SEM_T1) N times?
On 05/28/2010 07:24 AM, Randy Yates wrote:
> OK, this may be a stupid question but I'm going to go ahead and ask > it. I seem to be missing something very basic in the use of semaphores. > > The SEM module in DSP/BIOS maintains a non-negative count of the number > of times it has been "posted". Then when a pend occurs, the process > either a) blocks if count = 0, or b) decrements count and resumes. > > I have one task T1 that must run to completion before other tasks (T2, > ..., TN) run. It *seems* this would be a good use of a semaphore; > create a semaphore SEM_T1, then have each task T2, ..., TN pend on > SEM_T1. Then when T1 completes, it posts to SEM_T1. > > However, this won't work with DSP/BIOS semaphores. What will happen is > that the first task that pended, say, T2, will get unblocked when T1 > completes, but since there was only one pend by T1, none of the other > T3-TN will unblock. > > How would you solve this problem in DSP/BIOS?
http://en.wikipedia.org/wiki/Semaphore_%28programming%29 It appears that your T1 has the highest priority of all the other tasks, since it must run to completion before the others start. If it may interrupt the others should it gets ready to run while another is running, then it is, indeed, the highest priority task. Normally you would deal with this by making T1 the highest priority task in the OS, and it pend on whatever event makes it ready (i.e. an ADC read, or a timer tick). That way it'll wake up when it should, do it's job, and go to sleep. Because it's high priority it'll automatically trump the other tasks for processor access, and because it pends when it's done the other tasks will automatically get their chance when it's done. The semaphore that you have T1 pend on could be a regular binary semaphore if they are supported, or it could be a counting semaphore with you taking care about what happens if T1 ever fails to service it often enough. You may want to look at the book "The Art of Embedded Programming" by Jack Ganssle. It looks like he only has one chapter on using real-time operating systems, so you may have to decide if you want to read that book or if you want to find a whole book on real-time OS usage (not that I can find one in a hurry). Some background: http://en.wikipedia.org/wiki/Real_time_operating_system I started to try write you a "RTOS usage in a nutshell", but realized that it would run over 1000 words and take me most of the day -- so I didn't. You may want to try searching the web, or go straight to Embedded.com and see if they have any articles on this subject. I know that just about all that I have learned on the subject of RTOS usage has come from on-the-job training and from Embedded.com, with just a smattering of lectures at the Embedded Systems Conference thrown in (Michael Barr's talk on Rate Monotonic Scheduling is a must-see after you understand the nuts and bolts of an RTOS; he's got an article on Embedded.com) The real trick is that you want to identify your tasks, prioritize them, and let the OS do it's job. For the most part, an event-driven real-time application doesn't have a lot of tasks playing with semaphores: they are posted in the interrupt service routines that respond to the external events and pended on by the _one_ task that services that event, and the "task A must pend on task B" stuff is taken care of with priorities. Only when you are doing something advanced* like granting access to a serial port to more than one task must you use counting semaphores, and then you run into all sorts of potential trouble** that you have to mitigate. The job of prioritizing the tasks is where Rate Monotonic Scheduling comes in -- once you strip off all the math explaining what's happening under the hood, the actual procedure is easy and gives a pretty concrete yea or nay to whether the underlying assumptions are being met. I hope this helps. * I.e. stupid ** Like priority inversion. Which is why those advanced methods are stupid if you can find a simple way to avoid them, like a _single_ task that talks on that serial port, and handles messages from or to any other tasks that may need to talk. -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
Randy Yates  <yates@ieee.org> wrote:

>OK, this may be a stupid question but I'm going to go ahead and ask >it. I seem to be missing something very basic in the use of semaphores. > >The SEM module in DSP/BIOS maintains a non-negative count of the number >of times it has been "posted". Then when a pend occurs, the process >either a) blocks if count = 0, or b) decrements count and resumes. > >I have one task T1 that must run to completion before other tasks (T2, >..., TN) run. It *seems* this would be a good use of a semaphore; >create a semaphore SEM_T1, then have each task T2, ..., TN pend on >SEM_T1. Then when T1 completes, it posts to SEM_T1. > >However, this won't work with DSP/BIOS semaphores. What will happen is >that the first task that pended, say, T2, will get unblocked when T1 >completes, but since there was only one pend by T1, none of the other >T3-TN will unblock. > >How would you solve this problem in DSP/BIOS?
I find the following very useful in an RTOS: Partition those tasks which you wish to initiated from interrupt events into a finite set of priority levels (the fewer the better). Within each level each task is preceded with common code which implements the following sequence of operations (which must be made uninterruptable): (1) Is there another task of the same level already running? (2) If so, place the current task at the end of the queue for this level, and return from interrupt. (3) If not, lower priority and start running the current task. And at the end of the task: (4) Raise priority (5) If another task for this level is queued, execute it. Otherwise, if the queue is empty, return from interrupt. Whether you do this with semaphores is an implementation detail. What you don't want to do is have tasks queueing or executing other tasks which are from a _different_ priority level. Applying this to your example, T1 is higher priority than T2, T3 etc. which are all at the same (but lower) priority level. So, when T1 gets to step (3), it lowers priority enough to enough to allow other T1-level tasks to run - but not T2, T3 etc. tasks. Then after T1 gets to step (5), all tasks queued at the T2, T3 level can potentially run. (Note that if another T1 task does interrupt T1, it only gets queued, it does not pre-empt T1.) Fundamentally you need a queue of tasks at each priority level, rather than individual tasks reaching across levels to start or stop things. Steve
On 05/28/2010 12:41 PM, Steve Pope wrote:
> Randy Yates<yates@ieee.org> wrote: > >> OK, this may be a stupid question but I'm going to go ahead and ask >> it. I seem to be missing something very basic in the use of semaphores. >> >> The SEM module in DSP/BIOS maintains a non-negative count of the number >> of times it has been "posted". Then when a pend occurs, the process >> either a) blocks if count = 0, or b) decrements count and resumes. >> >> I have one task T1 that must run to completion before other tasks (T2, >> ..., TN) run. It *seems* this would be a good use of a semaphore; >> create a semaphore SEM_T1, then have each task T2, ..., TN pend on >> SEM_T1. Then when T1 completes, it posts to SEM_T1. >> >> However, this won't work with DSP/BIOS semaphores. What will happen is >> that the first task that pended, say, T2, will get unblocked when T1 >> completes, but since there was only one pend by T1, none of the other >> T3-TN will unblock. >> >> How would you solve this problem in DSP/BIOS? > > I find the following very useful in an RTOS: > > Partition those tasks which you wish to initiated from interrupt > events into a finite set of priority levels (the fewer the better). > > Within each level each task is preceded with common code which > implements the following sequence of operations (which must be made > uninterruptable): > > (1) Is there another task of the same level already running? > > (2) If so, place the current task at the end of the queue for > this level, and return from interrupt. > > (3) If not, lower priority and start running the current task. > > And at the end of the task: > > (4) Raise priority > > (5) If another task for this level is queued, execute it. > Otherwise, if the queue is empty, return from interrupt. > > Whether you do this with semaphores is an implementation detail. > > What you don't want to do is have tasks queueing or executing other > tasks which are from a _different_ priority level. > > Applying this to your example, T1 is higher priority than T2, > T3 etc. which are all at the same (but lower) priority level. > > So, when T1 gets to step (3), it lowers priority enough to > enough to allow other T1-level tasks to run - but not T2, T3 > etc. tasks. Then after T1 gets to step (5), all tasks queued > at the T2, T3 level can potentially run. > > (Note that if another T1 task does interrupt T1, it only > gets queued, it does not pre-empt T1.) > > Fundamentally you need a queue of tasks at each priority level, > rather than individual tasks reaching across levels to > start or stop things.
This sounds way complicated. Are you doing this within the context of an RTOS? If so, why in heck can't you just use as many fixed priorities as you need to get the job done? In general, I've found that if you have to do much (or any) changing of priorities as a task executes that's an indication that you're confused, that you're really doing too much with that task, and that you need to split it up or otherwise refactor your code to better match your problem. Only if you're accessing one physical resource from multiple tasks, and you have truly different levels of priority for your interactions with that resource, do you need to play priority games -- and those are well documented under the "priority inheritance" rubric. -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
Tim Wescott  <tim@seemywebsite.now> wrote:

>On 05/28/2010 12:41 PM, Steve Pope wrote:
>> I find the following very useful in an RTOS: >> >> Partition those tasks which you wish to initiated from interrupt >> events into a finite set of priority levels (the fewer the better). >> >> Within each level each task is preceded with common code which >> implements the following sequence of operations (which must be made >> uninterruptable): >> >> (1) Is there another task of the same level already running? >> >> (2) If so, place the current task at the end of the queue for >> this level, and return from interrupt. >> >> (3) If not, lower priority and start running the current task. >> >> And at the end of the task: >> >> (4) Raise priority >> >> (5) If another task for this level is queued, execute it. >> Otherwise, if the queue is empty, return from interrupt. >> >> Whether you do this with semaphores is an implementation detail. >> [...] Fundamentally you need a queue of tasks at each priority level, >> rather than individual tasks reaching across levels to >> start or stop things.
>This sounds way complicated. Are you doing this within the context of >an RTOS? If so, why in heck can't you just use as many fixed priorities >as you need to get the job done?
>In general, I've found that if you have to do much (or any) changing of >priorities as a task executes that's an indication that you're confused, >that you're really doing too much with that task, and that you need to >split it up or otherwise refactor your code to better match your >problem.
Here's what you wrote earlier: "The real trick is that you want to identify your tasks, prioritize them, and let the OS do it's job." I'm saying the same thing, I believe, except perhaps describing what is being done at a lower level. The problem as stated was that Randy was trying to use a task at one priority level to start a lower priority task; that for me is problematical. Your scheduler (whether part of the OS or not) should be starting that lower priority task. We may have a slight difference of philosophy in that you may be stating to prioritize all tasks (N tasks, N priorities) whereas I am more in favor of partitioning them into the smallest possible number of priorities. By all means use the features of your RTOS to do what I described above. Assuming those features are there. (They were not, back when I was working in this area, but I assume things have vastly improved... a modern RTOS will queue up equal-priority event-driven tasks and run them sequentially...right? Steve
On May 28, 3:24&#4294967295;pm, Randy Yates <ya...@ieee.org> wrote:
> OK, this may be a stupid question but I'm going to go ahead and ask > it. I seem to be missing something very basic in the use of semaphores. > > The SEM module in DSP/BIOS maintains a non-negative count of the number > of times it has been "posted". Then when a pend occurs, the process > either a) blocks if count = 0, or b) decrements count and resumes. > > I have one task T1 that must run to completion before other tasks (T2, > ..., TN) run. &#4294967295;It *seems* this would be a good use of a semaphore; > create a semaphore SEM_T1, then have each task T2, ..., TN pend on > SEM_T1. Then when T1 completes, it posts to SEM_T1. > > However, this won't work with DSP/BIOS semaphores. What will happen is > that the first task that pended, say, T2, will get unblocked when T1 > completes, but since there was only one pend by T1, none of the other > T3-TN will unblock. > > How would you solve this problem in DSP/BIOS? > -- > Randy Yates &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% "Watching all the days go by... &#4294967295; &#4294967295; > Digital Signal Labs &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% &#4294967295;Who are you and who am I?" > mailto://ya...@ieee.org &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;% 'Mission (A World Record)',http://www.digitalsignallabs.com% *A New World Record*, ELO
If it's of any help, elaborate RTOS-style synchronization used to confuse me too. At some point, I found that all this malarkey can be reduced to a combination of 2/4-phase asynchronous handshaking protocol. Then only thing you have left to do is hook these up with boolean expressions. -Momo
Hi Steve,

Steve Pope wrote:
> Randy Yates <yates@ieee.org> wrote: > >> OK, this may be a stupid question but I'm going to go ahead and ask >> it. I seem to be missing something very basic in the use of semaphores. >> >> The SEM module in DSP/BIOS maintains a non-negative count of the number >> of times it has been "posted". Then when a pend occurs, the process >> either a) blocks if count = 0, or b) decrements count and resumes. >> >> I have one task T1 that must run to completion before other tasks (T2, >> ..., TN) run. It *seems* this would be a good use of a semaphore; >> create a semaphore SEM_T1, then have each task T2, ..., TN pend on >> SEM_T1. Then when T1 completes, it posts to SEM_T1. >> >> However, this won't work with DSP/BIOS semaphores. What will happen is >> that the first task that pended, say, T2, will get unblocked when T1 >> completes, but since there was only one pend by T1, none of the other >> T3-TN will unblock. >> >> How would you solve this problem in DSP/BIOS? > > I find the following very useful in an RTOS: > > Partition those tasks which you wish to initiated from interrupt > events into a finite set of priority levels (the fewer the better). > > Within each level each task is preceded with common code which > implements the following sequence of operations (which must be made > uninterruptable): > > (1) Is there another task of the same level already running? > (2) If so, place the current task at the end of the queue for > this level, and return from interrupt. > (3) If not, lower priority and start running the current task. > > And at the end of the task: > > (4) Raise priority > (5) If another task for this level is queued, execute it. > Otherwise, if the queue is empty, return from interrupt. > > Whether you do this with semaphores is an implementation detail. > > What you don't want to do is have tasks queueing or executing other > tasks which are from a _different_ priority level. > > Applying this to your example, T1 is higher priority than T2, > T3 etc. which are all at the same (but lower) priority level.
I didn't read that in the OP's initial query at all! There was no mention of "priority". Rather, there was a timing dependency of T2..TN on the actions that T1 performed. T1 can have *any* priority relative to the other tasks and there can still be this dependency -- which can be managed with mutexes, semaphores, event flags, etc. I.e., if you claim T2 has lower priority than T1, then what happens if T1 *blocks* for some reason (e.g., waiting on a timer). Now, tasks of lower priorities *can* run (T2, etc.). Yet, the OP explicitly claimed: "I have one task T1 that must run to completion before other tasks (T2, ..., TN) run." Merely rearranging priorities is not going to give the OP this "guarantee". See my post re: how to do this with semaphores, event flags, condition variables, etc.
> So, when T1 gets to step (3), it lowers priority enough to > enough to allow other T1-level tasks to run - but not T2, T3 > etc. tasks. Then after T1 gets to step (5), all tasks queued > at the T2, T3 level can potentially run. > > (Note that if another T1 task does interrupt T1, it only > gets queued, it does not pre-empt T1.) > > Fundamentally you need a queue of tasks at each priority level, > rather than individual tasks reaching across levels to > start or stop things.
D Yuniskis  <not.going.to.be@seen.com> wrote:

>> Applying this to your example, T1 is higher priority than T2, >> T3 etc. which are all at the same (but lower) priority level.
>I didn't read that in the OP's initial query at all! >There was no mention of "priority". Rather, there >was a timing dependency of T2..TN on the actions >that T1 performed. T1 can have *any* priority >relative to the other tasks and there can still be >this dependency -- which can be managed with mutexes, >semaphores, event flags, etc.
Yes, I'm saying don't do this. If you want T1 to complete before T2, then T1 should be higher priority -- if that is workable.
>I.e., if you claim T2 has lower priority than T1, then >what happens if T1 *blocks* for some reason (e.g., >waiting on a timer). Now, tasks of lower priorities >*can* run (T2, etc.). Yet, the OP explicitly claimed:
> "I have one task T1 that must run to completion before > other tasks (T2, ..., TN) run."
>Merely rearranging priorities is not going to give the >OP this "guarantee".
That's a very interesting example. A blocked task normally caused the next task of the same priority to be scheduled. Not a task of a lower priority. But it does get interesting when you have tasks other than the lowest priority subject to blocking...
>See my post re: how to do this with semaphores, event >flags, condition variables, etc.
Yes, I agree it's needed in the case you describe, but I am not at all sure the OP was describing this case. And if so it's the "messier" case. Steve