Q: How do mbed libraries do threading/preemption?

12 Feb 2011

How do mbed libraries do threading/preemption?

Example

main() does an infinite loop with a while(1) {}; But the user's Timer function is called at the proper time. Probably not called directly by the interrupt handler, else chaos.

Is there some sort of underlying thread manager? Perhaps a single-stack scheduler like Protothreads or equivalent?

12 Feb 2011

The Timer class does use interrupts.

12 Feb 2011

hmm, OK, something's polling a CPU timer chip's value?

My Q was really how does a user function get called from the libraries given main() is doing a while(1)

12 Feb 2011
13 Feb 2011

Quote:

hmm, OK, something's polling a CPU timer chip's value?

No, it uses the Timer3 of LPC1768, set up to trigger every ms (AFAIK). The timer callback is executed in interrupt context.

13 Feb 2011

Igor Skochinsky wrote:

Quote:

hmm, OK, something's polling a CPU timer chip's value?

No, it uses the Timer3 of LPC1768, set up to trigger every ms (AFAIK). The timer callback is executed in interrupt context.

Wow. So if the user's timer callback calls a slow routine, is interrupt latency affected for the timer and/or other lower priority interrupts? And if the callback in turn calls clib or mbed lib routines that aren't reentrant, does the system integrity fail as it would in a straight C program's ISR behavior?

Is this ISR callback also true for things such as ethernet packet arrivals?

13 Feb 2011

Yes, doing slow stuff (like printf) in callbacks is generally not a good idea. Better just set update some vars and do the real work in main(). Reentrancy is probaly less of an issue, as all interrupts are set to the same priority by default (AFAIK), so they shouldn't interrupt one another.

You can assume that everything not called from main (directly or indirectly) is called from an interrupt handler.

13 Feb 2011

thanks.

I wasn't sure if there was a thread scheduler. The ISR call-backs apparently can access C++ global static variables (as volatile). Not sure I'd want to call a C++ function from the ISR's call-back.

13 Feb 2011

You might want to look at Real-Time Operating Systems there are examples for scmRTOS and FreeRTOS on here.

13 Feb 2011

Thanks. I don't need an RTOS at the moment; I've used FreeRTOS in the past. More appealing for small micros are the cooperative task schedulers that use one stack, unlike a full preemptive RTOS.

I started this thread to ensure I understood what the mbed libraries are doing in these asynchronous events.