diff options
author | Chris Robinson <[email protected]> | 2018-11-10 21:09:54 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-10 21:09:54 -0800 |
commit | f3ce7bc7dcf20275d93974755c42486d812d771f (patch) | |
tree | e18bb582b09b658ee6e955e8994a2fddf42fa6d4 /examples | |
parent | 2f42f74418f079e2ef8f081a7faf915c5eb131b4 (diff) |
Move altimespec_get and al_nssleep to examples' common code
Diffstat (limited to 'examples')
-rw-r--r-- | examples/common/alhelpers.c | 69 | ||||
-rw-r--r-- | examples/common/alhelpers.h | 11 |
2 files changed, 80 insertions, 0 deletions
diff --git a/examples/common/alhelpers.c b/examples/common/alhelpers.c index fab039e9..657c10d3 100644 --- a/examples/common/alhelpers.c +++ b/examples/common/alhelpers.c @@ -114,3 +114,72 @@ const char *FormatName(ALenum format) } return "Unknown Format"; } + + +#ifdef _WIN32 + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <mmsystem.h> + +int altimespec_get(struct timespec *ts, int base) +{ + 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; +} + +void al_nssleep(unsigned long nsec) +{ + Sleep(nsec / 1000000); +} + +#else + +#include <sys/time.h> +#include <time.h> + +int altimespec_get(struct timespec *ts, int base) +{ + if(base == AL_TIME_UTC) + { + int ret; +#if _POSIX_TIMERS > 0 + ret = clock_gettime(CLOCK_REALTIME, ts); + if(ret == 0) return base; +#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; + } +#endif + } + + return 0; +} + +void al_nssleep(unsigned long nsec) +{ + struct timespec ts, rem; + ts.tv_sec = nsec / 1000000000ul; + ts.tv_nsec = nsec % 1000000000ul; + while(nanosleep(&ts, &rem) == -1 && errno == EINTR) + ts = rem; +} + +#endif diff --git a/examples/common/alhelpers.h b/examples/common/alhelpers.h index 41a7ce58..e3e638ac 100644 --- a/examples/common/alhelpers.h +++ b/examples/common/alhelpers.h @@ -18,6 +18,17 @@ const char *FormatName(ALenum type); 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); +void al_nssleep(unsigned long nsec); + #ifdef __cplusplus } #endif /* __cplusplus */ |