From a3e175a56b995fc10e67a2ed97abd92c5fac607a Mon Sep 17 00:00:00 2001 From: deuce <> Date: Wed, 1 Oct 2003 23:45:40 +0000 Subject: [PATCH] Added msclock() and MSCLOCKS_PER_SEC - this is a realtime clock with a frequency of MSCLOCKS_PER_SEC... the initial value is undefined. --- src/xpdev/genwrap.c | 15 +++++++++++++++ src/xpdev/genwrap.h | 8 ++++++++ src/xpdev/wraptest.c | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/xpdev/genwrap.c b/src/xpdev/genwrap.c index cfaceafd2b..818b54f59d 100644 --- a/src/xpdev/genwrap.c +++ b/src/xpdev/genwrap.c @@ -334,3 +334,18 @@ char* DLLCALL asctime_r(const struct tm *tm, char *buf) } #endif /* !defined(__unix__) */ + +/********************************************/ +/* Hi-res real-time clock implementation. */ +/********************************************/ +#ifdef __unix__ +clock_t DLLCALL msclock(void) +{ + long long int usecs; + struct timeval tv; + if(gettimeofday(&tv,NULL)==1) + return(-1); + usecs=tv.tv_sec*1000000+tv.tv_usec; + return((clock_t)(usecs/(1000000/MSCLOCKS_PER_SEC))); +} +#endif diff --git a/src/xpdev/genwrap.h b/src/xpdev/genwrap.h index 6e29e2aa31..7fbfed27ec 100644 --- a/src/xpdev/genwrap.h +++ b/src/xpdev/genwrap.h @@ -44,6 +44,7 @@ #if defined(__unix__) #include <sched.h> /* sched_yield */ + #include <time.h> /* clock_t */ #include <sys/time.h> /* struct timeval */ #include <strings.h> /* strcasecmp() */ #include <unistd.h> /* usleep */ @@ -268,6 +269,13 @@ DLLEXPORT int DLLCALL xp_random(int); DLLEXPORT char* DLLCALL os_version(char *str); DLLEXPORT char* DLLCALL lastchar(const char* str); DLLEXPORT int DLLCALL safe_snprintf(char *dst, size_t size, char *fmt, ...); + +#if defined(_WIN32) +#define msclock() clock() +#else +#define MSCLOCKS_PER_SEC 1000 +clock_t msclock(void); +#endif #if defined(__cplusplus) } #endif diff --git a/src/xpdev/wraptest.c b/src/xpdev/wraptest.c index 80b6d0df1d..ec9a49545e 100644 --- a/src/xpdev/wraptest.c +++ b/src/xpdev/wraptest.c @@ -49,6 +49,7 @@ int main() int fd; int fd2; int canrelock=0; + clock_t ticks; /* Show platform details */ DESCRIBE_COMPILER(compiler); @@ -171,8 +172,9 @@ int main() t=time(NULL); printf("sleeping... "); fflush(stdout); + ticks=msclock(); SLEEP(5000); - printf("slept %ld seconds\n",time(NULL)-t); + printf("slept %ld seconds (%ld according to msclock)\n",time(NULL)-t,(msclock()-ticks)/MSCLOCKS_PER_SEC); /* Thread SLEEP test */ printf("\nThread SLEEP(5 second) test\n"); -- GitLab