mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(kernel): lazily initialize the adaptive static mutex initializer
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -20,8 +20,12 @@ PthreadMutex destroyedMutex() {
|
||||
return reinterpret_cast<PthreadMutex>(std::uintptr_t{2});
|
||||
}
|
||||
|
||||
PthreadMutex adaptiveInitializer() {
|
||||
return reinterpret_cast<PthreadMutex>(std::uintptr_t{1});
|
||||
}
|
||||
|
||||
bool isInitializedMutex(PthreadMutex mutex) {
|
||||
return mutex && mutex != destroyedMutex() && reinterpret_cast<std::uintptr_t>(mutex) != 1;
|
||||
return mutex && mutex != destroyedMutex() && mutex != adaptiveInitializer();
|
||||
}
|
||||
|
||||
PthreadMutex resolveMutex(PthreadMutex* mutex, bool initialize) {
|
||||
@@ -34,13 +38,13 @@ PthreadMutex resolveMutex(PthreadMutex* mutex, bool initialize) {
|
||||
const auto current = slot.load(std::memory_order_acquire);
|
||||
if (current == destroyedMutex())
|
||||
throw std::runtime_error("Mutex has been destroyed");
|
||||
if (reinterpret_cast<std::uintptr_t>(current) == 1)
|
||||
throw std::runtime_error("Adaptive mutex initializer is unsupported");
|
||||
if (current)
|
||||
if (current && current != adaptiveInitializer())
|
||||
return current;
|
||||
if (!initialize)
|
||||
throw std::runtime_error("Mutex is not initialized");
|
||||
auto* created = new PthreadMutexPrivate();
|
||||
if (current == adaptiveInitializer())
|
||||
created->_type = MutexType::Adaptive;
|
||||
slot.store(created, std::memory_order_release);
|
||||
return created;
|
||||
}
|
||||
@@ -144,8 +148,10 @@ int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex) {
|
||||
std::lock_guard lock(initializationMutex);
|
||||
if (*mutex == destroyedMutex())
|
||||
throw std::runtime_error("Mutex has already been destroyed");
|
||||
if (reinterpret_cast<std::uintptr_t>(*mutex) == 1)
|
||||
throw std::runtime_error("Adaptive mutex initializer is unsupported");
|
||||
if (*mutex == adaptiveInitializer()) {
|
||||
std::atomic_ref<PthreadMutex>(*mutex).store(destroyedMutex(), std::memory_order_release);
|
||||
return 0;
|
||||
}
|
||||
if (*mutex && (*mutex)->_owner.load(std::memory_order_acquire) != std::thread::id{})
|
||||
throw std::runtime_error("Cannot destroy a locked mutex");
|
||||
delete *mutex;
|
||||
|
||||
@@ -28,6 +28,7 @@ int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* a
|
||||
int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex);
|
||||
int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex);
|
||||
int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex);
|
||||
int APS5_VABI scePthreadMutexTrylock(PthreadMutex* mutex);
|
||||
}
|
||||
|
||||
static constexpr int SCE_OK = 0;
|
||||
@@ -36,6 +37,7 @@ static constexpr int SCE_KERNEL_ERROR_EBADF = static_cast<int>(0x80020009);
|
||||
static constexpr int SCE_KERNEL_ERROR_EDEADLK = static_cast<int>(0x8002000B);
|
||||
static constexpr int SCE_KERNEL_ERROR_EFAULT = static_cast<int>(0x8002000E);
|
||||
static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast<int>(0x80020016);
|
||||
static constexpr int SCE_KERNEL_ERROR_EBUSY = static_cast<int>(0x80020010);
|
||||
static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = static_cast<int>(0x8002003C);
|
||||
static constexpr int MUTEX_TYPE_ERRORCHECK = 1;
|
||||
static constexpr int PRIO_NONE = 0;
|
||||
@@ -108,6 +110,21 @@ int main() {
|
||||
Require(scePthreadMutexUnlock(&adaptive) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&adaptive) == SCE_OK);
|
||||
|
||||
PthreadMutex adaptiveStatic = reinterpret_cast<PthreadMutex>(std::uintptr_t{1});
|
||||
Require(scePthreadMutexLock(&adaptiveStatic) == SCE_OK);
|
||||
Require(adaptiveStatic != reinterpret_cast<PthreadMutex>(std::uintptr_t{1}));
|
||||
Require(scePthreadMutexLock(&adaptiveStatic) == SCE_KERNEL_ERROR_EDEADLK);
|
||||
Require(scePthreadMutexTrylock(&adaptiveStatic) == SCE_KERNEL_ERROR_EBUSY);
|
||||
Require(scePthreadMutexUnlock(&adaptiveStatic) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&adaptiveStatic) == SCE_OK);
|
||||
adaptiveStatic = reinterpret_cast<PthreadMutex>(std::uintptr_t{1});
|
||||
Require(scePthreadMutexTrylock(&adaptiveStatic) == SCE_OK);
|
||||
Require(scePthreadMutexTrylock(&adaptiveStatic) == SCE_KERNEL_ERROR_EBUSY);
|
||||
Require(scePthreadMutexUnlock(&adaptiveStatic) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&adaptiveStatic) == SCE_OK);
|
||||
adaptiveStatic = reinterpret_cast<PthreadMutex>(std::uintptr_t{1});
|
||||
Require(scePthreadMutexDestroy(&adaptiveStatic) == SCE_OK);
|
||||
|
||||
Require(scePthreadMutexattrInit(&attr) == SCE_OK);
|
||||
PthreadMutex defaulted = nullptr;
|
||||
Require(scePthreadMutexInit(&defaulted, &attr, nullptr) == SCE_OK);
|
||||
|
||||
Reference in New Issue
Block a user