Move altimespec_get and al_nssleep to examples' common code

This commit is contained in:
Chris Robinson
2018-11-10 21:09:54 -08:00
parent 2f42f74418
commit f3ce7bc7dc
5 changed files with 81 additions and 99 deletions
+1 -1
View File
@@ -1675,7 +1675,7 @@ IF(ALSOFT_EXAMPLES)
ADD_EXECUTABLE(alrecord examples/alrecord.c)
TARGET_COMPILE_DEFINITIONS(alrecord PRIVATE ${CPP_DEFS})
TARGET_COMPILE_OPTIONS(alrecord PRIVATE ${C_FLAGS})
TARGET_LINK_LIBRARIES(alrecord PRIVATE ${LINKER_FLAGS} common OpenAL)
TARGET_LINK_LIBRARIES(alrecord PRIVATE ${LINKER_FLAGS} ex-common common OpenAL)
IF(ALSOFT_INSTALL)
INSTALL(TARGETS alrecord
-72
View File
@@ -174,21 +174,6 @@ int althrd_join(althrd_t thr, int *res)
return althrd_success;
}
int althrd_sleep(const struct timespec *ts, struct timespec* UNUSED(rem))
{
DWORD msec;
if(ts->tv_sec < 0 || ts->tv_sec >= (0x7fffffff / 1000) ||
ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
return -2;
msec = (DWORD)(ts->tv_sec * 1000);
msec += (DWORD)((ts->tv_nsec+999999) / 1000000);
Sleep(msec);
return 0;
}
int almtx_init(almtx_t *mtx, int type)
{
@@ -381,27 +366,6 @@ void altss_delete(altss_t tss_id)
}
int altimespec_get(struct timespec *ts, int base)
{
static_assert(sizeof(FILETIME) == sizeof(ULARGE_INTEGER),
"Size of FILETIME does not match ULARGE_INTEGER");
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 alcall_once(alonce_flag *once, void (*callback)(void))
{
LONG ret;
@@ -447,7 +411,6 @@ void althrd_thread_detach(void)
#endif
extern inline int althrd_sleep(const struct timespec *ts, struct timespec *rem);
extern inline void alcall_once(alonce_flag *once, void (*callback)(void));
extern inline void althrd_deinit(void);
@@ -713,39 +676,4 @@ void altss_delete(altss_t tss_id)
pthread_key_delete(tss_id);
}
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;
}
#endif
void al_nssleep(unsigned long nsec)
{
struct timespec ts, rem;
ts.tv_sec = nsec / 1000000000ul;
ts.tv_nsec = nsec % 1000000000ul;
while(althrd_sleep(&ts, &rem) == -1)
ts = rem;
}
-26
View File
@@ -34,21 +34,11 @@ typedef int (*althrd_start_t)(void*);
typedef void (*altss_dtor_t)(void*);
#define AL_TIME_UTC 1
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifndef HAVE_STRUCT_TIMESPEC
struct timespec {
time_t tv_sec;
long tv_nsec;
};
#endif
typedef DWORD althrd_t;
typedef CRITICAL_SECTION almtx_t;
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
@@ -62,7 +52,6 @@ typedef LONG alonce_flag;
#define AL_ONCE_FLAG_INIT 0
int althrd_sleep(const struct timespec *ts, struct timespec *rem);
void alcall_once(alonce_flag *once, void (*callback)(void));
void althrd_deinit(void);
@@ -171,17 +160,6 @@ inline void althrd_yield(void)
sched_yield();
}
inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
{
int ret = nanosleep(ts, rem);
if(ret != 0)
{
ret = ((errno==EINTR) ? -1 : -2);
errno = 0;
}
return ret;
}
inline int almtx_lock(almtx_t *mtx)
{
@@ -257,10 +235,6 @@ int alsem_trywait(alsem_t *sem);
int altss_create(altss_t *tss_id, altss_dtor_t callback);
void altss_delete(altss_t tss_id);
int altimespec_get(struct timespec *ts, int base);
void al_nssleep(unsigned long nsec);
#ifdef __cplusplus
} // extern "C"
+69
View File
@@ -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
+11
View File
@@ -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 */