linuxkernelgettimeofdaylivelockxenomai

Is there a safe way to call gettimeofday() from a Xenomai real time thread?


I'm running a Xenomai real time thread that sometimes needs to call gettimeofday(), in order to find out what the current time is according to ptpd.

However, doing that appears to be unsafe: in particular, it occasionally puts the Xenomai thread and the Linux kernel into a "livelock" situation, causing gettimeofday() to spin the CPU and never return, as described here.

My question is, is there a safe way to get gettimeofday()'s information from a Xenomai real time thread? I'm considering adding my own version of gettimeofday() to my Linux kernel (my version would fail if read_seqretry() returns true, unlike the regular version which will loop forever when that happens). However, I'd just as soon not start customizing the Linux kernel if there is a better way to do it.


Solution

  • Update Oct 2012
    For anyone else stumbling across this thread...

    Check the Xenomai API:
    http://www.xenomai.org/documentation/trunk/html/api/group__clock.html

    Here's a code snippet for you, tested with Xenomai 2.6:

    // Get system time in nanoseconds (real-time safe)
    // Time is usually related to GMT, because Xenomai syncs time during
    // bootup, so you might get a different time offset to gettimeofday()
    // which is based on your current timezone. 
    double time = (double)rt_timer_read(); 
    time /= 1000000000; // convert to seconds