Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions core/libs/prx/libkernel/Pthread/src/Mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex) {
}

int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex) {
auto* current = resolveMutex(mutex, false);
if (!current)
if (!mutex)
throw std::invalid_argument("Mutex pointer is null");
auto* current = std::atomic_ref<PthreadMutex>(*mutex).load(std::memory_order_acquire);
if (current == destroyedMutex())
return SCE_KERNEL_ERROR_EINVAL;
if (current->_owner.load(std::memory_order_acquire) != std::this_thread::get_id())
if (!isInitializedMutex(current) || current->_owner.load(std::memory_order_acquire) != std::this_thread::get_id())
return sceNotPermitted;
bool released = true;
if (current->_type == MutexType::Recursive) {
Expand Down
16 changes: 16 additions & 0 deletions core/libs/tests/GuestPthreadDestroyedObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int APS5_VABI pthread_cond_destroy_nid_postfix(PthreadCond* cond);
}

static constexpr int SCE_OK = 0;
static constexpr int SCE_KERNEL_ERROR_EPERM = static_cast<int>(0x80020001u);
static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast<int>(0x80020016u);
static constexpr int POSIX_EINVAL = 22;

Expand All @@ -34,6 +35,21 @@ int main() {
Require(pthread_mutex_lock_nid_postfix(&mutex) == POSIX_EINVAL);
Require(mutex == reinterpret_cast<PthreadMutex>(std::uintptr_t{2}));

PthreadMutex uninitialized = nullptr;
Require(scePthreadMutexUnlock(&uninitialized) == SCE_KERNEL_ERROR_EPERM);
Require(uninitialized == nullptr);
Require(scePthreadMutexLock(&uninitialized) == SCE_OK);
Require(scePthreadMutexUnlock(&uninitialized) == SCE_OK);
Require(scePthreadMutexUnlock(&uninitialized) == SCE_KERNEL_ERROR_EPERM);
Require(scePthreadMutexDestroy(&uninitialized) == SCE_OK);

PthreadMutex adaptive = reinterpret_cast<PthreadMutex>(std::uintptr_t{1});
Require(scePthreadMutexUnlock(&adaptive) == SCE_KERNEL_ERROR_EPERM);
Require(adaptive == reinterpret_cast<PthreadMutex>(std::uintptr_t{1}));
Require(scePthreadMutexLock(&adaptive) == SCE_OK);
Require(scePthreadMutexUnlock(&adaptive) == SCE_OK);
Require(scePthreadMutexDestroy(&adaptive) == SCE_OK);

PthreadCond cond = nullptr;
Require(scePthreadCondInit(&cond, nullptr, "destroyed") == SCE_OK);
Require(scePthreadCondDestroy(&cond) == SCE_OK);
Expand Down
Loading