Re: Dumb RTOS Question



galt_57@xxxxxxxxxxx wrote:

Starting on my first RTOS project and can't quite see the logic to the
interprocess communications. The Zilog RTOS offers message queues, but
if the queue is empty the reading process is suspended until something
shows up in the queue. Well sorry, that isn't good because my process
has multiple things it needs to monitor periodically. It can't get hung
on just one message queue. I thought a message queue would work or have
an option to work like a ring buffer but that does not seem to be the
case. My concept is to have this process check several "queues" and
then suspend for a safe amount of time before looping back to check
them again. Any sage advice appreciated. Thanks.


Dave

I'm looking at this thread, and I'm thinking that you need to go digging for a basic tutorial on using an RTOS. They're very handy things, but it sounds like you're misusing the features.

Here's my quick, dirty, and probably wrong summation:

1. Never write a task loop inside a task. Use one RTOS task per
'real' task.

2. Think about how things need to be synchronized, and why.
2a. A message handler should be event-driven, because there's
nothing to do between messages.
2b. If an out-of-date message is useless, don't bother using a
queue -- just write to a spot in memory ('register') and set
a semaphore.
2c. Some things are inherently periodic, like motion control
loops or video interrupt handlers. There's no point in
using a queue here -- just set a semaphore from your timer
interrupt (or use a periodic semaphore if your OS provides it).

3. Don't synchronize things that don't need to be. If your task
polls X, and your task is guaranteed to repeat fast enough to
catch X in the act of doing anything interesting, just read X
inside your task and be happy.

4. Think about OS resources. OS messages queues can be very
expensive in terms of processor time and memory, so if you
know you're going to get a ton of messages each time your task
has a chance to run don't use a message queue.

An example of this is serial handling -- for handling serial
messages I'll have the ISR dump stuff into a ring buffer and set
a semaphore. The (possibly low priority) task that services
the ring buffer my not come on until there's 50 characters in
the buffer, and it'll chew through all of them before going back
to sleep.

Better yet, if I can the ISR will dump stuff into a ring buffer
and only set a semaphore on a delimiter (end-of-packet, or
carriage return if it's ASCII). This _really_ trims down the
clock cycles.

5. And don't forget that the 'RT' in RTOS means "real time" --
make sure you make your deadlines!

I should mention that after my experience with the Worst Software Ever that I detailed in my other post I keep a string of garlic and a crucifix handy, and I wave them around whenever anyone mentions queues for anything other than strict message handling. So you can take my input as slightly biased...

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

Posting from Google? See http://cfaj.freeshell.org/google/

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
.



Relevant Pages

  • Re: How to get synchronized efficiently
    ... A semaphore, behaving as a mutex, is one way to protect access to a queue. ... The choice of the best sync model ALWAYS depends on the design of your ... Let's set aside IOCP and consider the semaphore case. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Multithreaded queue with wait event
    ... if two threads release the mutex and are just about to ... See, if that happens, one thread will receive the event and dequeue. ... your queue can indeed return no item when there is no timeout ... I have two implementations, one using the semaphore, the other using ...
    (comp.programming.threads)
  • Re: Multithreaded queue with wait event
    ... I have two implementations, one using the semaphore, the other using ...   if queue size> semaphore max, release mutex, return failure ... is also a simple boolean "shutdown" flag, used to prevent too many new ...
    (comp.programming.threads)
  • Re: Explain this about threads
    ... There is usually no reason to sit and spin in the meantime, so normally the remainder of B's time slice would be yielded to the scheduler in hopes that some other task has a use for the CPU resource. ... A semaphore can be viewed as a data structure which represents a resource or event. ... One key aspect of a classic OS semaphore structure is a queue to which tasks awaiting the resource or event can chain their task control blocks. ... But this requires a task switch somewhere to go from user mode to kernel mode. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Clean-up in OnDestroy
    ... I use the semaphore to process queued items. ... PostQueuedCompletionStatus to implement IOCP based queue processing. ... simply terminate a thread using already-exist object, ... call EndDialog but then the OnClose event is not occured. ...
    (microsoft.public.vc.mfc)