AuroraRuntime/Source/Threading/Primitives/AuConditionMutex.NT.hpp

81 lines
1.9 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2021-06-27 21:25:29 +00:00
File: AuConditionMutex.NT.hpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
#pragma once
2022-11-17 07:46:07 +00:00
#include "AuIConditionMutexEx.hpp"
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERICCM)
namespace Aurora::Threading::Primitives
{
2023-04-01 08:53:00 +00:00
// Note: these magic numbers are powers of two such that you can use them as add/sub operations
// in place of masking.
// ie: add = or
// sub = and
// assuming the bits are unset/set respectively
static auto const kFutexBitWake = 256u; // 2^8
static auto const kFutexBitWait = 512u; // 2^9, next byte over
inline HANDLE gKeyedEventHandle { INVALID_HANDLE_VALUE };
// Actually NT5.x
struct NT4Mutex
{
volatile AuUInt32 uWaitCount {}; // yields while bits are high, dec to release one from the semaphore yield
};
2023-08-21 16:34:24 +00:00
struct Win32ConditionMutex final
2021-06-27 21:25:29 +00:00
{
Win32ConditionMutex();
~Win32ConditionMutex();
2023-08-21 16:34:24 +00:00
bool TryLock();
2024-11-21 17:17:09 +00:00
// TODO: these two aulines used to give me link errors. !todo: check against different msvc releases(, if i can be bothered)!!!
auline void Lock();
auline void Unlock();
2023-08-21 16:34:24 +00:00
AuUInt GetOSHandle();
2021-06-27 21:25:29 +00:00
auline bool TryLockNoSpin();
auline bool TryLockHeavy();
#if !defined(AURORA_FORCE_SRW_LOCKS)
NT4Mutex lock_;
#else
SRWLOCK lock_;
#endif
2021-06-27 21:25:29 +00:00
};
2023-08-21 16:34:24 +00:00
using ConditionMutexInternal = Win32ConditionMutex;
struct ConditionMutexImpl final : IConditionMutexEx
{
auline bool TryLock()
{
return mutex.TryLock();
}
auline void Lock()
{
mutex.Lock();
2023-08-21 16:34:24 +00:00
}
auline void Unlock()
{
mutex.Unlock();
}
inline AuUInt GetOSHandle()
{
return mutex.GetOSHandle();
}
ConditionMutexInternal mutex;
};
2021-06-27 21:25:29 +00:00
}
#endif