diff options
author | Chris Robinson <[email protected]> | 2018-11-26 23:45:04 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-26 23:45:04 -0800 |
commit | 2530370ff29009fccad472c9e9194ebdd561a398 (patch) | |
tree | a3a41655bb1b2c37a91d10f77f112d36d726af0d /examples/common | |
parent | d7d99adc915583416dd8c70491ec7fc4ac71a543 (diff) |
Avoid relying on struct timespec
Diffstat (limited to 'examples/common')
-rw-r--r-- | examples/common/alhelpers.c | 61 | ||||
-rw-r--r-- | examples/common/alhelpers.h | 11 |
2 files changed, 31 insertions, 41 deletions
diff --git a/examples/common/alhelpers.c b/examples/common/alhelpers.c index 6a583497..fec51e6b 100644 --- a/examples/common/alhelpers.c +++ b/examples/common/alhelpers.c @@ -28,6 +28,7 @@ * finding an appropriate buffer format, and getting readable strings for * channel configs and sample types. */ +#include <time.h> #include <stdio.h> #include <errno.h> #include <string.h> @@ -123,22 +124,21 @@ const char *FormatName(ALenum format) #include <windows.h> #include <mmsystem.h> -int altimespec_get(struct timespec *ts, int base) +unsigned int altime_get(void) { - if(base == AL_TIME_UTC) - { - union { - FILETIME ftime; - ULARGE_INTEGER ulint; - } systime; - GetSystemTimeAsFileTime(&systime.ftime); - /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */ - ts->tv_sec = systime.ulint.QuadPart/10000000; - ts->tv_nsec = (systime.ulint.QuadPart%10000000) * 100; - return base; - } - - return 0; + static unsigned int start_time = 0; + unsigned int cur_time; + union { + FILETIME ftime; + ULARGE_INTEGER ulint; + } systime; + GetSystemTimeAsFileTime(&systime.ftime); + /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */ + cur_time = (unsigned int)(systime.ulint.QuadPart/10000); + + if(!start_time) + start_time = cur_time; + return cur_time - start_time; } void al_nssleep(unsigned long nsec) @@ -151,27 +151,26 @@ void al_nssleep(unsigned long nsec) #include <sys/time.h> #include <time.h> -int altimespec_get(struct timespec *ts, int base) +unsigned int altime_get(void) { - if(base == AL_TIME_UTC) - { - int ret; + static unsigned int start_time = 0u; + unsigned int cur_time; + #if _POSIX_TIMERS > 0 - ret = clock_gettime(CLOCK_REALTIME, ts); - if(ret == 0) return base; + struct timespec ts; + int ret = clock_gettime(CLOCK_REALTIME, ts); + if(ret != 0) return 0; + cur_time = ts.ts_sec*1000 + ts.ts_nsec/1000000; #else /* _POSIX_TIMERS > 0 */ - struct timeval tv; - ret = gettimeofday(&tv, NULL); - if(ret == 0) - { - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000; - return base; - } + struct timeval tv; + int ret = gettimeofday(&tv, NULL); + if(ret != 0) return 0; + cur_time = tv.tv_sec*1000 + tv.tv_usec/1000; #endif - } - return 0; + if(!start_time) + start_time = cur_time; + return cur_time - start_time; } void al_nssleep(unsigned long nsec) diff --git a/examples/common/alhelpers.h b/examples/common/alhelpers.h index 14edf5d9..4193e86f 100644 --- a/examples/common/alhelpers.h +++ b/examples/common/alhelpers.h @@ -1,8 +1,6 @@ #ifndef ALHELPERS_H #define ALHELPERS_H -#include <time.h> - #include "AL/alc.h" #include "AL/al.h" #include "AL/alext.h" @@ -19,14 +17,7 @@ int InitAL(char ***argv, int *argc); void CloseAL(void); /* Cross-platform timeget and sleep functions. */ -#ifndef HAVE_STRUCT_TIMESPEC -struct timespec { - time_t tv_sec; - long tv_nsec; -}; -#endif -#define AL_TIME_UTC 1 -int altimespec_get(struct timespec *ts, int base); +unsigned int altime_get(void); void al_nssleep(unsigned long nsec); #ifdef __cplusplus |