Skip to content
Snippets Groups Projects
Commit a3e175a5 authored by deuce's avatar deuce
Browse files

Added msclock() and MSCLOCKS_PER_SEC - this is a realtime clock with a

frequency of MSCLOCKS_PER_SEC... the initial value is undefined.
parent 83b4d9ef
No related branches found
No related tags found
No related merge requests found
...@@ -334,3 +334,18 @@ char* DLLCALL asctime_r(const struct tm *tm, char *buf) ...@@ -334,3 +334,18 @@ char* DLLCALL asctime_r(const struct tm *tm, char *buf)
} }
#endif /* !defined(__unix__) */ #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
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#if defined(__unix__) #if defined(__unix__)
#include <sched.h> /* sched_yield */ #include <sched.h> /* sched_yield */
#include <time.h> /* clock_t */
#include <sys/time.h> /* struct timeval */ #include <sys/time.h> /* struct timeval */
#include <strings.h> /* strcasecmp() */ #include <strings.h> /* strcasecmp() */
#include <unistd.h> /* usleep */ #include <unistd.h> /* usleep */
...@@ -268,6 +269,13 @@ DLLEXPORT int DLLCALL xp_random(int); ...@@ -268,6 +269,13 @@ DLLEXPORT int DLLCALL xp_random(int);
DLLEXPORT char* DLLCALL os_version(char *str); DLLEXPORT char* DLLCALL os_version(char *str);
DLLEXPORT char* DLLCALL lastchar(const char* str); DLLEXPORT char* DLLCALL lastchar(const char* str);
DLLEXPORT int DLLCALL safe_snprintf(char *dst, size_t size, char *fmt, ...); 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) #if defined(__cplusplus)
} }
#endif #endif
......
...@@ -49,6 +49,7 @@ int main() ...@@ -49,6 +49,7 @@ int main()
int fd; int fd;
int fd2; int fd2;
int canrelock=0; int canrelock=0;
clock_t ticks;
/* Show platform details */ /* Show platform details */
DESCRIBE_COMPILER(compiler); DESCRIBE_COMPILER(compiler);
...@@ -171,8 +172,9 @@ int main() ...@@ -171,8 +172,9 @@ int main()
t=time(NULL); t=time(NULL);
printf("sleeping... "); printf("sleeping... ");
fflush(stdout); fflush(stdout);
ticks=msclock();
SLEEP(5000); 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 */ /* Thread SLEEP test */
printf("\nThread SLEEP(5 second) test\n"); printf("\nThread SLEEP(5 second) test\n");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment