[ECOS] Re: Periodic execution of code in a thread context

Jonathan Larmour jifl@jifvik.org
Wed Sep 23 21:02:00 GMT 2009


John Dallaway wrote:
> Steven Clugston wrote:
>>I need to have a function with some control code in it to execute at
>>regular and consistent intervals.
>>The way I've done this is just to create a thread and trap the execution
>>path in an infinite loop using while(1){some code;cyg_thread_delay()} or
>>for(;;){some code; cyg_thread_delay()}.
>>
>>The trouble with this approach is [woes snipped]
> 
> I suggest you post on a semaphore from an alarm handler function. The
> regular thread can then wait on the semaphore each time round the loop
> rather than calling cyg_thread_delay().
> 
> http://ecos.sourceware.org/docs-latest/ref/kernel-semaphores.html

John's right, although you should naturally take care that you can deal 
with pre-emption of that worker thread by higher priority tasks, and/or 
that the operations that thread is then doing aren't ones that can be 
halted by some other thread (e.g. calls involving mutexes or other mutual 
exclusion protection, which includes library calls like C library, POSIX, 
file I/O etc.). Otherwise there's a chance that you take longer than the 
period between alarms. The (counting) semaphores mean you'll still have 
the correct number of events, but they won't be on time.

In other words, be careful with what you do and how long, in the worst 
case, it takes.

Jifl
-- 
--["No sense being pessimistic, it wouldn't work anyway"]-- Opinions==mine

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss



More information about the Ecos-discuss mailing list