aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-11-10 21:09:54 -0800
committerChris Robinson <[email protected]>2018-11-10 21:09:54 -0800
commitf3ce7bc7dcf20275d93974755c42486d812d771f (patch)
treee18bb582b09b658ee6e955e8994a2fddf42fa6d4
parent2f42f74418f079e2ef8f081a7faf915c5eb131b4 (diff)
Move altimespec_get and al_nssleep to examples' common code
-rw-r--r--CMakeLists.txt2
-rw-r--r--common/threads.c72
-rw-r--r--common/threads.h26
-rw-r--r--examples/common/alhelpers.c69
-rw-r--r--examples/common/alhelpers.h11
5 files changed, 81 insertions, 99 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6079c25b..dc3bad12 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
diff --git a/common/threads.c b/common/threads.c
index e8301297..de9fc452 100644
--- a/common/threads.c
+++ b/common/threads.c
@@ -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;
-}
diff --git a/common/threads.h b/common/threads.h
index 7fbe20cd..c2168cdf 100644
--- a/common/threads.h
+++ b/common/threads.h
@@ -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"
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 */