c11: Implement c11/time.h with c11/impl/time.c

Create c11/time.h instead of put timespec_get in `c11/threads.h`

Creating impl folder is used to avoid `#include <time.h>` point the c11/time.h file

Detecting if `struct timespec` present with meson
Define TIME_UTC in `c11/time.h` instead `c11/threads.h`
Define `struct timespec` in `c11/time.h` when not present.
Implement timespec_get in c11/impl/time.c instead threads.h

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15497>
This commit is contained in:
Yonggang Luo
2022-02-19 13:19:08 +08:00
committed by Marge Bot
parent fe01757ddf
commit b2ddec4e98
13 changed files with 144 additions and 53 deletions

View File

@@ -29,11 +29,7 @@
#ifndef EMULATED_THREADS_H_INCLUDED_
#define EMULATED_THREADS_H_INCLUDED_
#include <time.h>
#ifndef TIME_UTC
#define TIME_UTC 1
#endif
#include "c11/time.h"
/*---------------------------- types ----------------------------*/
typedef void (*tss_dtor_t)(void*);

View File

@@ -378,19 +378,3 @@ tss_set(tss_t key, void *val)
{
return (pthread_setspecific(key, val) == 0) ? thrd_success : thrd_error;
}
/*-------------------- 7.25.7 Time functions --------------------*/
// 7.25.6.1
#ifndef HAVE_TIMESPEC_GET
static inline int
timespec_get(struct timespec *ts, int base)
{
if (!ts) return 0;
if (base == TIME_UTC) {
clock_gettime(CLOCK_REALTIME, ts);
return base;
}
return 0;
}
#endif

View File

@@ -128,7 +128,6 @@ static time_t impl_timespec2msec(const struct timespec *ts)
return (ts->tv_sec * 1000U) + (ts->tv_nsec / 1000000L);
}
#ifdef HAVE_TIMESPEC_GET
static DWORD impl_abs2relmsec(const struct timespec *abs_time)
{
const time_t abs_ms = impl_timespec2msec(abs_time);
@@ -138,7 +137,6 @@ static DWORD impl_abs2relmsec(const struct timespec *abs_time)
const DWORD rel_ms = (abs_ms > now_ms) ? (DWORD)(abs_ms - now_ms) : 0;
return rel_ms;
}
#endif
#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE
struct impl_call_once_param { void (*func)(void); };
@@ -253,14 +251,10 @@ cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *abs_time)
assert(cond != NULL);
assert(mtx != NULL);
assert(abs_time != NULL);
#ifdef HAVE_TIMESPEC_GET
const DWORD timeout = impl_abs2relmsec(abs_time);
if (SleepConditionVariableCS(cond, mtx, timeout))
return thrd_success;
return (GetLastError() == ERROR_TIMEOUT) ? thrd_timedout : thrd_error;
#else
return thrd_error;
#endif
}
// 7.25.3.6
@@ -312,7 +306,6 @@ mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
{
assert(mtx != NULL);
assert(ts != NULL);
#ifdef HAVE_TIMESPEC_GET
while (mtx_trylock(mtx) != thrd_success) {
if (impl_abs2relmsec(ts) == 0)
return thrd_timedout;
@@ -320,9 +313,6 @@ mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
thrd_yield();
}
return thrd_success;
#else
return thrd_error;
#endif
}
// 7.25.4.5
@@ -502,20 +492,3 @@ tss_set(tss_t key, void *val)
{
return TlsSetValue(key, val) ? thrd_success : thrd_error;
}
/*-------------------- 7.25.7 Time functions --------------------*/
// 7.25.6.1
#ifndef HAVE_TIMESPEC_GET
static inline int
timespec_get(struct timespec *ts, int base)
{
assert(ts != NULL);
if (base == TIME_UTC) {
ts->tv_sec = time(NULL);
ts->tv_nsec = 0;
return base;
}
return 0;
}
#endif