Sunday, May 17, 2009

Watchdog Timer

Hi guys, consider the following (simplified) code for a wdt.

while(wdt->run)
{
cur_time = get_cur_time();

elapsed_time = cur_time - last_report_time;

if(elapsed_time > timeout)
{
dogbite();
}

sleep(granularity);
}

It was written keeping in mind that wdt must not wait for any resources (so no mutex etc here), and with taking benifit of the fact that integer assignments were atomic.

now watched thread is free to update last_report_time whenever they want.

This code has a problem. If thread updates last_report_time after call to get_cur_time() but before elapsed_time is calculated, the elapsed time is seen as very large. (2's complement negative in unsigned)

This is complicated by the fact that all numbers storing time are 32 bits, and unit of time is 1 ms. So wrap-arounds in time are unavoidable, and must be handled.

Any suggestions/workarounds for this? Deterministic solution is required, i.e. every iteration of loop must be correctly able to identify if there was any timeout observed.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.