2023-07-05 12:39:43 +00:00
|
|
|
// NOTE: [$INTR] Intrinsics ========================================================================
|
2023-07-04 14:04:53 +00:00
|
|
|
#if !defined(DQN_OS_ARM64)
|
|
|
|
#if defined(DQN_COMPILER_GCC) || defined(DQN_COMPILER_CLANG)
|
|
|
|
#include <cpuid.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Dqn_CPUIDRegisters Dqn_CPUID(int function_id)
|
|
|
|
{
|
|
|
|
Dqn_CPUIDRegisters result = {};
|
2023-08-26 07:27:37 +00:00
|
|
|
#if defined(DQN_COMPILER_MSVC)
|
2023-07-04 14:04:53 +00:00
|
|
|
__cpuid(DQN_CAST(int *)result.array, function_id);
|
2023-08-16 11:59:38 +00:00
|
|
|
#elif defined(DQN_COMPILER_GCC) || defined(DQN_COMPILER_CLANG)
|
2023-07-04 14:04:53 +00:00
|
|
|
__get_cpuid(function_id, &result.array[0] /*eax*/, &result.array[1] /*ebx*/, &result.array[2] /*ecx*/ , &result.array[3] /*edx*/);
|
2023-08-16 11:59:38 +00:00
|
|
|
#else
|
2023-07-04 14:04:53 +00:00
|
|
|
#error "Compiler not supported"
|
2023-08-16 11:59:38 +00:00
|
|
|
#endif
|
2023-07-04 14:04:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // !defined(DQN_OS_ARM64)
|
|
|
|
|
2023-07-05 12:39:43 +00:00
|
|
|
// NOTE: [$TMUT] Dqn_TicketMutex ===================================================================
|
2023-07-04 14:04:53 +00:00
|
|
|
DQN_API void Dqn_TicketMutex_Begin(Dqn_TicketMutex *mutex)
|
|
|
|
{
|
|
|
|
unsigned int ticket = Dqn_Atomic_AddU32(&mutex->ticket, 1);
|
|
|
|
Dqn_TicketMutex_BeginTicket(mutex, ticket);
|
|
|
|
}
|
|
|
|
|
|
|
|
DQN_API void Dqn_TicketMutex_End(Dqn_TicketMutex *mutex)
|
|
|
|
{
|
|
|
|
Dqn_Atomic_AddU32(&mutex->serving, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
DQN_API Dqn_uint Dqn_TicketMutex_MakeTicket(Dqn_TicketMutex *mutex)
|
|
|
|
{
|
|
|
|
Dqn_uint result = Dqn_Atomic_AddU32(&mutex->ticket, 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
DQN_API void Dqn_TicketMutex_BeginTicket(Dqn_TicketMutex const *mutex, Dqn_uint ticket)
|
|
|
|
{
|
|
|
|
DQN_ASSERTF(mutex->serving <= ticket,
|
|
|
|
"Mutex skipped ticket? Was ticket generated by the correct mutex via MakeTicket? ticket = %u, "
|
|
|
|
"mutex->serving = %u",
|
|
|
|
ticket,
|
|
|
|
mutex->serving);
|
|
|
|
while (ticket != mutex->serving) {
|
|
|
|
// NOTE: Use spinlock intrinsic
|
|
|
|
_mm_pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DQN_API bool Dqn_TicketMutex_CanLock(Dqn_TicketMutex const *mutex, Dqn_uint ticket)
|
|
|
|
{
|
|
|
|
bool result = (ticket == mutex->serving);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-26 07:27:37 +00:00
|
|
|
#if defined(DQN_COMPILER_MSVC) || defined(DQN_COMPILER_CLANG_CL)
|
2023-07-04 14:04:53 +00:00
|
|
|
#if !defined(DQN_CRT_SECURE_NO_WARNINGS_PREVIOUSLY_DEFINED)
|
|
|
|
#undef _CRT_SECURE_NO_WARNINGS
|
|
|
|
#endif
|
|
|
|
#endif
|