#ifndef RTCLOCK_H
#define RTCLOCK_H

/******************************************************************************/
/******************************************************************************/
/*****************     Copyright 2004, 2005 Chronolytics Inc.   ***************/
/*****************     All Right Reserved                       ***************/
/******************************************************************************/
/******************************************************************************/

#include <sys/time.h>  /* struct timeval */

static volatile const char __attribute__ ((unused)) rcsid_RTCLOCK_H[] = "$Id: RTClock.h,v 1.8 2005/06/29 15:53:29 dave Exp $";

extern double RTClock_Ticks_per_uSec;

typedef unsigned long long RTClockCount;
typedef unsigned long RTClockMicroSecs;  /* time in microseconds */

static __inline__ RTClockCount RTClock_ReadClockCounter() {
  RTClockCount tsc;
  __asm__ __volatile__("rdtsc" : "=A" (tsc));
  return tsc;
}

typedef unsigned long RTClock_milliSecondTime;

extern RTClock_milliSecondTime RTTimer_Current();

static __inline__ RTClock_milliSecondTime RTClock_Current() {
  return RTTimer_Current();
}

   /* ctime_r with microseconds rather than timezone */
   /* buf needs to be 40 chars */
char  *RTClock_Micro_ctime_r(struct timeval *tv, char *buf);

#define RTCLOCK_CTIME_BUFSIZE   (40)  /* sufficient buf size */

#ifndef RTCLOCK_NEEDS_REAL_GETTIMEOFDAY
#define gettimeofday "Use RTClock_GetTimeOfDay for clock sync gettimeofday"
#endif
 /* this gettimeofday is based on ClockCount from epoch base time */
struct timeval *RTClock_GetTimeOfDay(struct timeval *tv, struct timezone *tz);
 /* this version uses passed in arbitrary ClockCount time offset */
struct timeval *RTClock_GetTimeOfDay_ClockCount(struct timeval *tv, RTClockCount ticks);

void RTClock_SetBaseTimeOfDay(struct timeval *tv);  /* set base time */
void RTClock_GetBaseTimeOfDay(struct timeval *tv);

void RTClock_Sync(RTClockCount *now, struct timeval *tv);

#define TICKS_TO_USECS(ticks) ((ticks) / RTClock_Ticks_per_uSec)
#define USECS_TO_TICKS(usec)   ((usec) * RTClock_Ticks_per_uSec)

#endif /* RTCLOCK_H */

