2021-06-27 21:25:29 +00:00
|
|
|
/***
|
2024-01-29 14:09:59 +00:00
|
|
|
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2023-03-15 00:35: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
|
|
|
|
|
2023-07-09 22:33:12 +00:00
|
|
|
static auto const kFutexBitWake = 256u; // 2^8
|
|
|
|
static auto const kFutexBitWait = 512u; // 2^9, next byte over
|
2023-04-23 18:23:10 +00:00
|
|
|
|
2023-03-15 00:35:29 +00:00
|
|
|
|
|
|
|
inline HANDLE gKeyedEventHandle { INVALID_HANDLE_VALUE };
|
|
|
|
|
2023-07-10 12:12:17 +00:00
|
|
|
// Actually NT5.x
|
2023-03-15 00:35:29 +00:00
|
|
|
struct NT4Mutex
|
|
|
|
{
|
2023-03-15 16:06:58 +00:00
|
|
|
volatile AuUInt32 uWaitCount {}; // yields while bits are high, dec to release one from the semaphore yield
|
2023-03-15 00:35:29 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2023-07-10 12:12:17 +00:00
|
|
|
auline bool TryLockNoSpin();
|
2023-08-19 17:14:28 +00:00
|
|
|
auline bool TryLockHeavy();
|
2023-07-10 12:12:17 +00:00
|
|
|
|
2023-03-16 18:25:23 +00:00
|
|
|
#if !defined(AURORA_FORCE_SRW_LOCKS)
|
2023-03-15 00:35:29 +00:00
|
|
|
NT4Mutex lock_;
|
2023-03-16 18:25:23 +00:00
|
|
|
#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()
|
|
|
|
{
|
2024-01-27 09:15:11 +00:00
|
|
|
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
|