2001-09-09 22:24:39 -04:00
|
|
|
/* timing.c
|
|
|
|
** - Timing functions
|
2004-01-28 20:02:12 -05:00
|
|
|
**
|
|
|
|
** This program is distributed under the GNU General Public License, version 2.
|
|
|
|
** A copy of this license is included with this source.
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
|
|
|
|
2003-03-08 11:05:38 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2001-10-20 17:28:09 -04:00
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
# include <stdint.h>
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2002-02-06 20:04:09 -05:00
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2003-06-05 15:18:00 -04:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
2003-06-05 15:02:36 -04:00
|
|
|
#endif
|
|
|
|
|
2003-10-19 23:08:46 -04:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include "timing.h"
|
|
|
|
|
2003-07-04 19:18:26 -04:00
|
|
|
/* see timing.h for an explanation of _mangle() */
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
/*
|
|
|
|
* Returns milliseconds no matter what.
|
|
|
|
*/
|
2001-10-20 01:03:24 -04:00
|
|
|
uint64_t timing_get_time(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-10-19 23:08:46 -04:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
struct timeb t;
|
|
|
|
|
|
|
|
ftime(&t);
|
2003-10-20 12:32:55 -04:00
|
|
|
return t.time * 1000 + t.millitm;
|
2003-10-19 23:08:46 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
return timeGetTime();
|
2003-10-19 23:08:46 -04:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
struct timeval mtv;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
gettimeofday(&mtv, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return (uint64_t)(mtv.tv_sec) * 1000 + (uint64_t)(mtv.tv_usec) / 1000;
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-10-20 01:03:24 -04:00
|
|
|
void timing_sleep(uint64_t sleeptime)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
struct timeval sleeper;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
sleeper.tv_sec = sleeptime / 1000;
|
|
|
|
sleeper.tv_usec = (sleeptime % 1000) * 1000;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* NOTE:
|
|
|
|
* This should be 0 for the first argument. The linux manpage
|
|
|
|
* says so. The solaris manpage also says this is a legal
|
|
|
|
* value. If you think differerntly, please provide references.
|
|
|
|
*/
|
2004-01-29 19:08:02 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(sleeptime);
|
|
|
|
#else
|
|
|
|
select(1, NULL, NULL, NULL, &sleeper);
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|