mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #156 from Pacuka/fix/pthread-mutex-types
fix(libkernel): default mutexes to error checking and add the adaptive type
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
namespace {
|
||||
|
||||
constexpr int POSIX_EINVAL = 22;
|
||||
constexpr int POSIX_MUTEX_ADAPTIVE_NP = 4;
|
||||
constexpr int POSIX_PRIO_PROTECT = 2;
|
||||
constexpr std::uintptr_t POSIX_ADAPTIVE_MUTEX_INITIALIZER = 1;
|
||||
|
||||
@@ -29,7 +28,7 @@ void _initializeStatic(PthreadMutex* mutex, const char* funcName) {
|
||||
PthreadMutex current = slot.load(std::memory_order_acquire);
|
||||
if (!_isStaticInitializer(current)) return;
|
||||
auto* created = new PthreadMutexPrivate();
|
||||
created->_type = reinterpret_cast<std::uintptr_t>(current) == POSIX_ADAPTIVE_MUTEX_INITIALIZER ? MutexType::Normal : MutexType::ErrorCheck;
|
||||
created->_type = reinterpret_cast<std::uintptr_t>(current) == POSIX_ADAPTIVE_MUTEX_INITIALIZER ? MutexType::Adaptive : MutexType::ErrorCheck;
|
||||
if (!slot.compare_exchange_strong(current, created, std::memory_order_acq_rel, std::memory_order_acquire))
|
||||
delete created;
|
||||
}
|
||||
@@ -96,11 +95,6 @@ int APS5_VABI pthread_mutexattr_setprotocol_nid_postfix(PthreadMutexattr* attr,
|
||||
}
|
||||
|
||||
int APS5_VABI pthread_mutexattr_settype_nid_postfix(PthreadMutexattr* attr, int type) {
|
||||
if (type == POSIX_MUTEX_ADAPTIVE_NP) {
|
||||
if (!attr || !*attr) throw std::runtime_error("pthread_mutexattr_settype: null attr");
|
||||
(*attr)->type = MutexType::Normal;
|
||||
return 0;
|
||||
}
|
||||
return PosixThread::ToErrno(scePthreadMutexattrSettype(attr, type));
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ enum class MutexType : std::uint32_t {
|
||||
ErrorCheck = 1,
|
||||
Recursive = 2,
|
||||
Normal = 3,
|
||||
Adaptive = 4,
|
||||
};
|
||||
|
||||
struct PthreadMutexattrPrivate {
|
||||
@@ -30,7 +31,7 @@ struct PthreadMutexPrivate {
|
||||
std::atomic<std::thread::id> _owner;
|
||||
int _count;
|
||||
|
||||
PthreadMutexPrivate() : _type(MutexType::Normal), _owner(std::thread::id{}), _count(0) {}
|
||||
PthreadMutexPrivate() : _type(MutexType::ErrorCheck), _owner(std::thread::id{}), _count(0) {}
|
||||
};
|
||||
|
||||
struct PthreadRwlockattrPrivate {
|
||||
|
||||
@@ -52,7 +52,7 @@ int acquireMutex(PthreadMutex mutex, TAcquire acquire, int unavailable, bool try
|
||||
if (owned && mutex->_type != MutexType::Recursive) {
|
||||
if (tryOnly)
|
||||
return sceBusy;
|
||||
if (mutex->_type == MutexType::ErrorCheck)
|
||||
if (mutex->_type == MutexType::ErrorCheck || mutex->_type == MutexType::Adaptive)
|
||||
return sceDeadlock;
|
||||
throw std::runtime_error("Mutex is already owned by the current thread");
|
||||
}
|
||||
@@ -90,7 +90,7 @@ extern "C" {
|
||||
int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr) {
|
||||
if (!attr)
|
||||
throw std::invalid_argument("Mutex attribute pointer is null");
|
||||
*attr = new PthreadMutexattrPrivate{MutexType::Normal};
|
||||
*attr = new PthreadMutexattrPrivate{MutexType::ErrorCheck};
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type) {
|
||||
case 1: (*attr)->type = MutexType::ErrorCheck; break;
|
||||
case 2: (*attr)->type = MutexType::Recursive; break;
|
||||
case 3: (*attr)->type = MutexType::Normal; break;
|
||||
case 4: (*attr)->type = MutexType::Normal; break;
|
||||
case 4: (*attr)->type = MutexType::Adaptive; break;
|
||||
default: throw std::invalid_argument("Invalid mutex type");
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -14,6 +14,13 @@ int APS5_VABI sceKernelDeleteEqueue(KernelEqueue eq);
|
||||
int APS5_VABI sceKernelWaitEqueue(KernelEqueue eq, KernelEvent* ev, int num, int* out, const KernelUseconds* timo);
|
||||
int APS5_VABI sceKernelDeleteUserEvent(KernelEqueue eq, int id);
|
||||
int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr);
|
||||
int APS5_VABI pthread_mutexattr_init_nid_postfix(PthreadMutexattr* attr);
|
||||
int APS5_VABI pthread_mutexattr_settype_nid_postfix(PthreadMutexattr* attr, int type);
|
||||
int APS5_VABI pthread_mutexattr_destroy_nid_postfix(PthreadMutexattr* attr);
|
||||
int APS5_VABI pthread_mutex_init_nid_postfix(PthreadMutex* mutex, const PthreadMutexattr* attr);
|
||||
int APS5_VABI pthread_mutex_lock_nid_postfix(PthreadMutex* mutex);
|
||||
int APS5_VABI pthread_mutex_unlock_nid_postfix(PthreadMutex* mutex);
|
||||
int APS5_VABI pthread_mutex_destroy_nid_postfix(PthreadMutex* mutex);
|
||||
int APS5_VABI scePthreadMutexattrDestroy(PthreadMutexattr* attr);
|
||||
int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type);
|
||||
int APS5_VABI scePthreadMutexattrSetprotocol(PthreadMutexattr* attr, int protocol);
|
||||
@@ -34,6 +41,8 @@ static constexpr int MUTEX_TYPE_ERRORCHECK = 1;
|
||||
static constexpr int PRIO_NONE = 0;
|
||||
static constexpr int PRIO_INHERIT = 1;
|
||||
static constexpr int PRIO_PROTECT = 2;
|
||||
static constexpr int MUTEX_TYPE_ADAPTIVE = 4;
|
||||
static constexpr int POSIX_EDEADLK = 11;
|
||||
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
|
||||
@@ -88,4 +97,46 @@ int main() {
|
||||
Require(scePthreadMutexLock(&mutex) == SCE_KERNEL_ERROR_EDEADLK);
|
||||
Require(scePthreadMutexUnlock(&mutex) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&mutex) == SCE_OK);
|
||||
|
||||
Require(scePthreadMutexattrInit(&attr) == SCE_OK);
|
||||
Require(scePthreadMutexattrSettype(&attr, MUTEX_TYPE_ADAPTIVE) == SCE_OK);
|
||||
PthreadMutex adaptive = nullptr;
|
||||
Require(scePthreadMutexInit(&adaptive, &attr, nullptr) == SCE_OK);
|
||||
Require(scePthreadMutexattrDestroy(&attr) == SCE_OK);
|
||||
Require(scePthreadMutexLock(&adaptive) == SCE_OK);
|
||||
Require(scePthreadMutexLock(&adaptive) == SCE_KERNEL_ERROR_EDEADLK);
|
||||
Require(scePthreadMutexUnlock(&adaptive) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&adaptive) == SCE_OK);
|
||||
|
||||
Require(scePthreadMutexattrInit(&attr) == SCE_OK);
|
||||
PthreadMutex defaulted = nullptr;
|
||||
Require(scePthreadMutexInit(&defaulted, &attr, nullptr) == SCE_OK);
|
||||
Require(scePthreadMutexattrDestroy(&attr) == SCE_OK);
|
||||
Require(scePthreadMutexLock(&defaulted) == SCE_OK);
|
||||
Require(scePthreadMutexLock(&defaulted) == SCE_KERNEL_ERROR_EDEADLK);
|
||||
Require(scePthreadMutexUnlock(&defaulted) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&defaulted) == SCE_OK);
|
||||
|
||||
PthreadMutex unattributed = nullptr;
|
||||
Require(scePthreadMutexInit(&unattributed, nullptr, nullptr) == SCE_OK);
|
||||
Require(scePthreadMutexLock(&unattributed) == SCE_OK);
|
||||
Require(scePthreadMutexLock(&unattributed) == SCE_KERNEL_ERROR_EDEADLK);
|
||||
Require(scePthreadMutexUnlock(&unattributed) == SCE_OK);
|
||||
Require(scePthreadMutexDestroy(&unattributed) == SCE_OK);
|
||||
|
||||
Require(pthread_mutexattr_init_nid_postfix(&attr) == 0);
|
||||
Require(pthread_mutexattr_settype_nid_postfix(&attr, MUTEX_TYPE_ADAPTIVE) == 0);
|
||||
PthreadMutex posixAdaptive = nullptr;
|
||||
Require(pthread_mutex_init_nid_postfix(&posixAdaptive, &attr) == 0);
|
||||
Require(pthread_mutexattr_destroy_nid_postfix(&attr) == 0);
|
||||
Require(pthread_mutex_lock_nid_postfix(&posixAdaptive) == 0);
|
||||
Require(pthread_mutex_lock_nid_postfix(&posixAdaptive) == POSIX_EDEADLK);
|
||||
Require(pthread_mutex_unlock_nid_postfix(&posixAdaptive) == 0);
|
||||
Require(pthread_mutex_destroy_nid_postfix(&posixAdaptive) == 0);
|
||||
|
||||
auto staticAdaptive = reinterpret_cast<PthreadMutex>(std::uintptr_t{1});
|
||||
Require(pthread_mutex_lock_nid_postfix(&staticAdaptive) == 0);
|
||||
Require(pthread_mutex_lock_nid_postfix(&staticAdaptive) == POSIX_EDEADLK);
|
||||
Require(pthread_mutex_unlock_nid_postfix(&staticAdaptive) == 0);
|
||||
Require(pthread_mutex_destroy_nid_postfix(&staticAdaptive) == 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user