Main Site Documentation

Timer haltet


#1

I am runing a timer every 10ms, but when I start to do big math calculations in main thread it only fires every 20ms.

Is there any explanation for this behavior?


#2

Can you show us some code?


#3

The code is to complex but in short: I am doing RSA calculation with BigInteger class. As soon as I call ModPow calc the timer is delayed with 10ms.

Does the Timer Interrupt have high prio? Could it be that it takes 10ms to move stuff to the stack/heap when the intrrupt is fired?


#4

20ms is an interesting number!

The MF dispatcher is not pre-emptive. Once a thread has the CPU it may hold it for up to 20ms before the
dispatcher takes the CPU away.

A timer interrupt handler does not receive the hardware interrupt directly. The internal interrupt handler
queues each timer request for execution by a worker thread.

If the main thread is in a tight loop, then the worker thread will only be dispatched every 20ms.

The way to fix this is to insert a Thread.Sleep(0) into your tight loops. This will allow the
dispatcher to execute a queued interrupt if there is one ready, otherwise the CPU is returned
to the main thread.


#5

One important thing to remember is that the NETMF Timer class does NOT use hardware timers! A Timer interrupt will run the next time NETMF gets around to it, which could be up to 20ms AFTER it should have happened. NETMF is NOT real-time!


#6

okey, is possible to generate an interrupt from the HW timers?


#7

The only way you are going to get a timer interrupt handler dispatched very fast, regardless of what else is running, is to use RLP. You then have access to the hardware timers directly.

I would not recommend you start with RLP until you get a good understanding of how .NET MF works.

If there was nothing else running on the processor, a timer interrupt would get to your handler within 2ms, or better depending upon the processor. I am not sure that the Timer class is not driven off a hardware timer within the MF core.

In most cases, you should be able to balance the handling of background tasks with interrupt handlers, in managed code, with an understanding of thread dispatching and thread priorities. You have to do “cooperative multitasking”.


#8

I have already done RLP so thats no problem (been proffesional C/C++ & C# developer for more than 10 years). I will try to find a solution to this problem. To add Sleep(0) could solve the problem, but it is rather ugly to put a Sleep in a Class (bigint) that doesn’t need to know anything about the rest of the code.