Detect if CLOCK_MONOTONIC_RAW is supported
This commit is contained in:
+3
-5
@@ -111,9 +111,9 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
|
||||
|
||||
{
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
|
||||
#else
|
||||
os->arena = DN_Arena_InitFromOSHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack);
|
||||
#else
|
||||
os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
|
||||
#endif
|
||||
|
||||
#if defined(DN_PLATFORM_WIN32)
|
||||
@@ -139,9 +139,7 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
|
||||
else
|
||||
DN_LOG_ErrorF("Failed to initialise Windows secure random number generator, error: %d", init_status);
|
||||
#else
|
||||
DN_POSIXCore *posix = DN_CAST(DN_POSIXCore *) os->platform_context;
|
||||
int mutex_init = pthread_mutex_init(&posix->sync_primitive_free_list_mutex, nullptr);
|
||||
DN_Assert(mutex_init == 0);
|
||||
DN_Posix_Init(DN_CAST(DN_POSIXCore *)os->platform_context);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
+27
-13
@@ -281,19 +281,27 @@ DN_API void DN_OS_SleepMs(DN_UInt milliseconds)
|
||||
;
|
||||
}
|
||||
|
||||
DN_API uint64_t DN_OS_PerfCounterFrequency()
|
||||
DN_API DN_U64 DN_OS_PerfCounterFrequency()
|
||||
{
|
||||
// NOTE: On Linux we use clock_gettime(CLOCK_MONOTONIC_RAW) which
|
||||
// NOTE: On Linux we use clock_gettime(CLOCK_MONOTONIC_RAW) (or CLOCK_MONOTONIC) which
|
||||
// increments at nanosecond granularity.
|
||||
uint64_t result = 1'000'000'000;
|
||||
DN_U64 result = 1'000'000'000;
|
||||
return result;
|
||||
}
|
||||
|
||||
DN_API uint64_t DN_OS_PerfCounterNow()
|
||||
static DN_POSIXCore *DN_OS_GetPOSIXCore_()
|
||||
{
|
||||
DN_Assert(g_dn_os_core_ && g_dn_os_core_->platform_context);
|
||||
DN_POSIXCore *result = DN_CAST(DN_POSIXCore *)g_dn_os_core_->platform_context;
|
||||
return result;
|
||||
}
|
||||
|
||||
DN_API DN_U64 DN_OS_PerfCounterNow()
|
||||
{
|
||||
DN_POSIXCore *posix = DN_OS_GetPOSIXCore_();
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
||||
uint64_t result = DN_CAST(uint64_t) ts.tv_sec * 1'000'000'000 + DN_CAST(uint64_t) ts.tv_nsec;
|
||||
clock_gettime(posix->clock_monotonic_raw ? CLOCK_MONOTONIC_RAW : CLOCK_MONOTONIC, &ts);
|
||||
DN_U64 result = DN_CAST(DN_U64) ts.tv_sec * 1'000'000'000 + DN_CAST(DN_U64) ts.tv_nsec;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -997,13 +1005,6 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
|
||||
return result;
|
||||
}
|
||||
|
||||
static DN_POSIXCore *DN_OS_GetPOSIXCore_()
|
||||
{
|
||||
DN_Assert(g_dn_os_core_ && g_dn_os_core_->platform_context);
|
||||
DN_POSIXCore *result = DN_CAST(DN_POSIXCore *)g_dn_os_core_->platform_context;
|
||||
return result;
|
||||
}
|
||||
|
||||
static DN_POSIXSyncPrimitive *DN_OS_U64ToPOSIXSyncPrimitive_(DN_U64 u64)
|
||||
{
|
||||
DN_POSIXSyncPrimitive *result = nullptr;
|
||||
@@ -1290,6 +1291,19 @@ DN_API DN_U32 DN_OS_ThreadID()
|
||||
return DN_CAST(DN_U32) result;
|
||||
}
|
||||
|
||||
DN_API void DN_Posix_Init(DN_POSIXCore *posix)
|
||||
{
|
||||
int mutex_init = pthread_mutex_init(&posix->sync_primitive_free_list_mutex, nullptr);
|
||||
DN_Assert(mutex_init == 0);
|
||||
|
||||
struct timespec ts;
|
||||
posix->clock_monotonic_raw = clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != -1;
|
||||
if (!posix->clock_monotonic_raw) {
|
||||
int get_result = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
DN_AssertF(get_result != -1, "CLOCK_MONOTONIC_RAW and CLOCK_MONOTONIC are not supported by this platform");
|
||||
}
|
||||
}
|
||||
|
||||
DN_API void DN_Posix_ThreadSetName(DN_Str8 name)
|
||||
{
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
|
||||
@@ -70,7 +70,9 @@ struct DN_POSIXCore
|
||||
{
|
||||
DN_POSIXSyncPrimitive *sync_primitive_free_list;
|
||||
pthread_mutex_t sync_primitive_free_list_mutex;
|
||||
bool clock_monotonic_raw;
|
||||
};
|
||||
|
||||
DN_API void DN_Posix_Init(DN_POSIXCore *posix);
|
||||
DN_API void DN_Posix_ThreadSetName(DN_Str8 name);
|
||||
DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus();
|
||||
|
||||
Reference in New Issue
Block a user