Avoid relying on struct timespec

This commit is contained in:
Chris Robinson 2018-11-26 23:45:04 -08:00
parent d7d99adc91
commit 2530370ff2
4 changed files with 39 additions and 57 deletions

View File

@ -204,13 +204,6 @@ IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS}")
ENDIF()
# Check if we have a proper timespec declaration
CHECK_STRUCT_HAS_MEMBER("struct timespec" tv_sec time.h HAVE_STRUCT_TIMESPEC)
IF(HAVE_STRUCT_TIMESPEC)
# Define it here so we don't have to include config.h for it
SET(CPP_DEFS ${CPP_DEFS} HAVE_STRUCT_TIMESPEC)
ENDIF()
# Some systems may need libatomic for C11 atomic functions to work
SET(OLD_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
SET(CMAKE_REQUIRED_LIBRARIES ${OLD_REQUIRED_LIBRARIES} atomic)

View File

@ -452,7 +452,7 @@ int main(int argc, char **argv)
EFX_REVERB_PRESET_CARPETEDHALLWAY,
EFX_REVERB_PRESET_BATHROOM
};
struct timespec basetime;
unsigned int basetime;
ALCdevice *device = NULL;
ALCcontext *context = NULL;
ALuint effects[2] = { 0, 0 };
@ -633,14 +633,14 @@ int main(int argc, char **argv)
assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
/* Get the current time as the base for timing in the main loop. */
altimespec_get(&basetime, AL_TIME_UTC);
basetime = altime_get();
loops = 0;
printf("Transition %d of %d...\n", loops+1, MaxTransitions);
/* Play the sound for a while. */
alSourcePlay(source);
do {
struct timespec curtime;
unsigned int curtime;
ALfloat timediff;
/* Start a batch update, to ensure all changes apply simultaneously. */
@ -649,9 +649,8 @@ int main(int argc, char **argv)
/* Get the current time to track the amount of time that passed.
* Convert the difference to seconds.
*/
altimespec_get(&curtime, AL_TIME_UTC);
timediff = (ALfloat)(curtime.tv_sec - basetime.tv_sec);
timediff += (ALfloat)(curtime.tv_nsec - basetime.tv_nsec) / 1000000000.0f;
curtime = altime_get();
timediff = (ALfloat)(curtime - basetime) / 1000.0f;
/* Avoid negative time deltas, in case of non-monotonic clocks. */
if(timediff < 0.0f)
@ -669,7 +668,7 @@ int main(int argc, char **argv)
* time to start a new cycle.
*/
timediff -= 8.0f;
basetime.tv_sec += 8;
basetime += 8000;
}
}

View File

@ -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;
}
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);
return 0;
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;
#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
}
static unsigned int start_time = 0u;
unsigned int cur_time;
return 0;
#if _POSIX_TIMERS > 0
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;
int ret = gettimeofday(&tv, NULL);
if(ret != 0) return 0;
cur_time = tv.tv_sec*1000 + tv.tv_usec/1000;
#endif
if(!start_time)
start_time = cur_time;
return cur_time - start_time;
}
void al_nssleep(unsigned long nsec)

View File

@ -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