2

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.

Jeremy Friesner
  • 57,675
  • 12
  • 103
  • 196
  • FWIW, Gilles from the Xenomai-help mailing list says that Xenomai 2.6 will have a CLOCK_HOSTRT clock feature that will satisfy this need. For earlier versions, one work-around is to add an ioctl to a custom driver that returns the Linux real-time-clock without trying to loop if contention is detected. – Jeremy Friesner Jul 07 '11 at 16:40
  • 1
    can you put that as a detailed answer? – Aquarius_Girl Apr 05 '12 at 08:56

1 Answers1

3

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
David
  • 169
  • 9
  • That is using the Xenomai Native API. There is another call in the Posix API that allows you to get the clock based on the host time. – David Feb 03 '13 at 18:16