/*=-=-=-=-=-=-=-=-=-=-  RCCL/RCI Notice of Copyright  -=-=-=-=-=-=-=-=-=-=-=*
 *									    *
 *	Copyright (C) 1987 by Mike Parker. Permission to use, distribute    *
 *	and copy is granted for non-commercial and research purposes only,  *
 *	providing the authors are acknowledged and this copyright notice is *
 *	displayed. The authors provide this software in hope that it will   *
 *	useful, but do not assume responsibility for its use.		    *
 *									    *
 *	This software was developed using the facilities of the McGill 	    *
 *	University Research Center for Intelligent Machines, Montreal,	    *
 *	Canada, for a project undertaken and supported by the RCA Advanced  *
 *	Technology Laboratories, Moorestown, New Jersey.		    *
 *									    *
 *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

#include <sys/time.h>
#include "../h/cdefs.h"

#ifndef VXWORKS_CFG

nap(sec)
float sec;
{
 struct timeval tv;
 struct timeval now;
 struct timeval then;
#ifdef LINUX_CFG
 fd_set fds;
#else
 int fds;
#endif


 int n;
 extern int errno;

 gettimeofday(&now,0);
 tv.tv_sec = (int) sec;
 tv.tv_usec = (int) (1000000 * (sec - tv.tv_sec));
 then.tv_sec = now.tv_sec + tv.tv_sec;
 then.tv_usec = now.tv_usec + tv.tv_usec;
 while (then.tv_usec >= 1000000)
  { then.tv_usec -= 1000000;
    then.tv_sec ++;
  }
 while (1)
  { 

#ifdef LINUX_CFG
    FD_ZERO(&fds);
#else
    fds = 0;
#endif

    errno = 0;
    n = select(32,&fds,0,0,&tv);
    gettimeofday(&now,0);
    tv.tv_sec = then.tv_sec - now.tv_sec;
    tv.tv_usec = then.tv_usec - now.tv_usec;
    while (tv.tv_usec < 0)
     { tv.tv_usec += 1000000;
       tv.tv_sec --;
     }
    if (tv.tv_sec < 0)
     { return;
     }
  }
}

getSystemTime (tv)
struct timeval *tv;
{
	gettimeofday (tv, (struct timezone*)0);
}

#else

nap(sec)
float sec;
{
	taskDelay (MAX((int)(sec*sysClkRateGet()),1));
}

getSystemTime(tv)
struct timeval *tv;
{
	int ticks;
	int rate;

	ticks = tickGet();
	rate = sysClkRateGet();
	tv->tv_sec = ticks/rate;
	tv->tv_usec = (ticks%rate)*1000000.0/rate;
}

#endif
