mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-12-06 17:41:24 +00:00
1328 lines
50 KiB
C++
1328 lines
50 KiB
C++
#include "prx/libkernel/Pthread/include/ThreadLifecycle.hpp"
|
|
#include "prx/libkernel/Pthread/include/Cancel.hpp"
|
|
#include "prx/libkernel/include/Api.hpp"
|
|
#include "prx/libSceUlt/include/Api.hpp"
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <bit>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <iterator>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#include "SceTypes.hpp"
|
|
#include "prx/libc/include/General.hpp"
|
|
#include "SceUltTypes.hpp"
|
|
#include "prx/libSceUlt/src/ReaderWriterLock.hpp"
|
|
|
|
namespace {
|
|
|
|
std::mutex gMutex;
|
|
std::unordered_map<void*, std::shared_ptr<UltMutexState>> gMutexes;
|
|
std::unordered_map<void*, std::shared_ptr<UltConditionState>> gConditions;
|
|
std::unordered_map<void*, std::shared_ptr<UltSemaphoreState>> gSemaphores;
|
|
std::unordered_map<void*, UltResourcePoolState> gResourcePools;
|
|
std::unordered_map<void*, std::shared_ptr<UltQueueDataPoolState>> gQueueDataPools;
|
|
std::unordered_map<void*, std::shared_ptr<UltQueueState>> gQueues;
|
|
std::unordered_map<void*, UltRuntimeState> gRuntimes;
|
|
std::unordered_map<void*, std::shared_ptr<UltUlthreadState>> gUlthreads;
|
|
|
|
int validateCommonOptParam(const void* optParam) {
|
|
if (optParam == nullptr) return ULT_OK;
|
|
if ((reinterpret_cast<std::uintptr_t>(optParam) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
UltCommonOptParam value;
|
|
std::memcpy(&value, optParam, sizeof(value));
|
|
if (value.header[0] != 0x6C696275 || value.header[1] != 0x6C746D61 || value.header[2] != 0x67696300) return ULT_ERROR_INVALID;
|
|
if (std::any_of(std::begin(value.reserved), std::end(value.reserved), [](std::uint8_t byte) { return byte != 0; })) return ULT_ERROR_INVALID;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int semaphoreGetState(void* semaphore, std::shared_ptr<UltSemaphoreState>* out) {
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gSemaphores.find(semaphore);
|
|
if (it == gSemaphores.end()) {
|
|
return semaphore == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
}
|
|
*out = it->second;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int queueGetState(void* queue, std::shared_ptr<UltQueueState>* out) {
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gQueues.find(queue);
|
|
if (it == gQueues.end()) {
|
|
return queue == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
}
|
|
*out = it->second;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int QueueTransfer(void* queue, void* data, const void* source, bool push, bool blocking) {
|
|
std::shared_ptr<UltQueueState> state;
|
|
if (int result = queueGetState(queue, &state); result != ULT_OK) return result;
|
|
if ((push ? source : data) == nullptr) return ULT_ERROR_NULL;
|
|
auto& pool = *state->_pool;
|
|
std::unique_lock lock(pool._mutex);
|
|
if (!state->_alive) return ULT_ERROR_STATE;
|
|
const auto ready = [&] { return !state->_alive || (push ? pool._free : state->_head) != UINT32_MAX; };
|
|
if (!ready()) {
|
|
if (!blocking) return ULT_ERROR_AGAIN;
|
|
auto& waiters = push ? state->_waitingPushers : state->_waitingPoppers;
|
|
++waiters;
|
|
try {
|
|
(push ? pool._spaceAvailable : state->_dataAvailable).wait(lock, ready);
|
|
} catch (...) {
|
|
--waiters;
|
|
throw;
|
|
}
|
|
--waiters;
|
|
if (!state->_alive) return ULT_ERROR_STATE;
|
|
}
|
|
if (push) {
|
|
const auto slot = pool._free;
|
|
std::memcpy(pool._data.data() + slot * pool._dataSize, source, state->_dataSize);
|
|
pool._free = pool._next[slot];
|
|
pool._next[slot] = UINT32_MAX;
|
|
if (state->_tail == UINT32_MAX) state->_head = slot;
|
|
else pool._next[state->_tail] = slot;
|
|
state->_tail = slot;
|
|
++state->_queuedItems;
|
|
} else {
|
|
const auto slot = state->_head;
|
|
std::memcpy(data, pool._data.data() + slot * pool._dataSize, state->_dataSize);
|
|
state->_head = pool._next[slot];
|
|
if (state->_head == UINT32_MAX) state->_tail = UINT32_MAX;
|
|
pool._next[slot] = pool._free;
|
|
pool._free = slot;
|
|
--state->_queuedItems;
|
|
}
|
|
lock.unlock();
|
|
if (push) state->_dataAvailable.notify_one();
|
|
else pool._spaceAvailable.notify_one();
|
|
return ULT_OK;
|
|
}
|
|
|
|
void ReleaseQueue(UltQueueState& state) {
|
|
auto& pool = *state._pool;
|
|
if (state._tail != UINT32_MAX) {
|
|
pool._next[state._tail] = pool._free;
|
|
pool._free = state._head;
|
|
}
|
|
state._head = state._tail = UINT32_MAX;
|
|
state._queuedItems = 0;
|
|
state._alive = false;
|
|
--pool._queues;
|
|
}
|
|
|
|
thread_local UltUlthreadState* gCurrentUlthread = nullptr;
|
|
|
|
void* APS5_VABI ulthreadRunner(void* arg) {
|
|
auto* state = static_cast<UltUlthreadState*>(arg);
|
|
gCurrentUlthread = state;
|
|
const auto result = state->_entry(state->_arg);
|
|
state->_exited.store(true, std::memory_order_release);
|
|
return reinterpret_cast<void*>(static_cast<std::intptr_t>(result));
|
|
}
|
|
|
|
void APS5_VABI releaseUlthreadJoin(void* argument) {
|
|
auto& state = *static_cast<std::shared_ptr<UltUlthreadState>*>(argument);
|
|
if (!state) return;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
state->_joining = false;
|
|
}
|
|
state.reset();
|
|
}
|
|
|
|
int joinUlthread(void* ulthread, std::int32_t* status, bool blocking) {
|
|
if (ulthread == nullptr) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltUlthreadState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gUlthreads.find(ulthread);
|
|
if (it == gUlthreads.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
if (!blocking && !state->_exited.load(std::memory_order_acquire)) return ULT_ERROR_AGAIN;
|
|
if (state->_joining || state.get() == gCurrentUlthread) return ULT_ERROR_STATE;
|
|
state->_joining = true;
|
|
}
|
|
CleanupHandler cleanup{};
|
|
bool cleanupRegistered = false;
|
|
void* result = nullptr;
|
|
int joined;
|
|
try {
|
|
__pthread_cleanup_push_imp_nid_postfix(releaseUlthreadJoin, &state, &cleanup);
|
|
cleanupRegistered = true;
|
|
joined = scePthreadJoin(state->_thread, &result);
|
|
} catch (...) {
|
|
if (state) {
|
|
if (cleanupRegistered) __pthread_cleanup_pop_imp_nid_postfix(1);
|
|
else releaseUlthreadJoin(&state);
|
|
}
|
|
throw;
|
|
}
|
|
__pthread_cleanup_pop_imp_nid_postfix(0);
|
|
if (joined != 0) {
|
|
releaseUlthreadJoin(&state);
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
if (status != nullptr) *status = static_cast<std::int32_t>(reinterpret_cast<std::intptr_t>(result));
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gUlthreads.find(ulthread);
|
|
if (it != gUlthreads.end() && it->second == state) gUlthreads.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
}
|
|
|
|
UltThreadIdentity UltCurrentThreadIdentity_nid_no_patch() {
|
|
if (gCurrentUlthread != nullptr) return {};
|
|
const auto self = scePthreadSelf();
|
|
std::uintptr_t thread;
|
|
std::memcpy(&thread, self, sizeof(thread));
|
|
return {reinterpret_cast<void*>(thread), 1};
|
|
}
|
|
|
|
bool UltWaitingPoolExists(void* pool) {
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
return gResourcePools.contains(pool);
|
|
}
|
|
|
|
extern "C" {
|
|
|
|
int APS5_VABI sceUltInitialize() {
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltFinalize() {
|
|
std::vector<std::shared_ptr<UltSemaphoreState>> semaphores;
|
|
std::vector<std::shared_ptr<UltMutexState>> mutexes;
|
|
{
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
if (!gUlthreads.empty()) {
|
|
return ULT_ERROR_BUSY;
|
|
}
|
|
semaphores.reserve(gSemaphores.size());
|
|
mutexes.reserve(gMutexes.size());
|
|
for (auto& entry : gSemaphores) {
|
|
auto& state = entry.second;
|
|
std::lock_guard<std::mutex> stateLock(state->_mutex);
|
|
state->_alive = false;
|
|
semaphores.push_back(state);
|
|
}
|
|
gSemaphores.clear();
|
|
for (auto& entry : gMutexes) {
|
|
auto& state = entry.second;
|
|
std::lock_guard<std::mutex> stateLock(state->_mutex);
|
|
state->_alive = false;
|
|
mutexes.push_back(state);
|
|
}
|
|
for (const auto& [object, condition] : gConditions) {
|
|
std::lock_guard stateLock(condition->_mutex->_mutex);
|
|
condition->_alive = false;
|
|
--condition->_mutex->_conditions;
|
|
for (auto* waiter : condition->_queue) waiter->_available.notify_one();
|
|
}
|
|
gConditions.clear();
|
|
gMutexes.clear();
|
|
UltFinalizeReaderWriterLocks();
|
|
gResourcePools.clear();
|
|
for (auto& entry : gQueues) {
|
|
auto& state = *entry.second;
|
|
std::lock_guard stateLock(state._pool->_mutex);
|
|
ReleaseQueue(state);
|
|
state._dataAvailable.notify_all();
|
|
state._pool->_spaceAvailable.notify_all();
|
|
}
|
|
gQueues.clear();
|
|
gQueueDataPools.clear();
|
|
gRuntimes.clear();
|
|
}
|
|
for (const auto& state : semaphores) {
|
|
state->_available.notify_all();
|
|
}
|
|
for (const auto& state : mutexes) {
|
|
state->_available.notify_all();
|
|
}
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltMutexOptParamInitialize(UltMutexOptParam* optParam, std::uint32_t buildVersion) {
|
|
if (optParam == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if ((reinterpret_cast<std::uintptr_t>(optParam) & 7u) != 0) {
|
|
return ULT_ERROR_ALIGNMENT;
|
|
}
|
|
std::memset(optParam, 0, sizeof(*optParam));
|
|
optParam->header[0] = 0x6C696275;
|
|
optParam->header[1] = 0x6C746D61;
|
|
optParam->header[2] = 0x67696300;
|
|
optParam->buildVersion = buildVersion;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltSemaphoreOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
return InitializeOptParam(optParam);
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
return InitializeOptParam(optParam);
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueDataResourcePoolOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
return InitializeOptParam(optParam);
|
|
}
|
|
|
|
int APS5_VABI sceUltWaitingQueueResourcePoolOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
return InitializeOptParam(optParam);
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
return InitializeOptParam(optParam);
|
|
}
|
|
|
|
int APS5_VABI sceUltConditionVariableOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
return InitializeOptParam(optParam);
|
|
}
|
|
|
|
int APS5_VABI _sceUltConditionVariableOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltConditionVariableOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI sceUltMutexCreate(void* mutex, const char* name, void* waitingQueueResourcePool, const UltMutexOptParam* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
if (mutex == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
auto state = std::make_shared<UltMutexState>();
|
|
state->_attribute = optParam != nullptr ? optParam->attribute : 0;
|
|
state->_waitingPool = waitingQueueResourcePool;
|
|
if (name != nullptr) {
|
|
std::strncpy(state->_name, name, sizeof(state->_name) - 1);
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
|
|
return ULT_ERROR_INVALID;
|
|
}
|
|
if (gMutexes.find(mutex) != gMutexes.end()) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
std::memset(mutex, 0, 256);
|
|
gMutexes[mutex] = std::move(state);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltMutexDestroy(void* mutex) {
|
|
if (mutex == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gMutexes.find(mutex);
|
|
if (it == gMutexes.end()) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
auto state = it->second;
|
|
std::lock_guard<std::mutex> stateLock(state->_mutex);
|
|
if (state->_lockCount != 0 || state->_waiters != 0 || state->_conditions != 0 || state->_conditionWaiters != 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
state->_alive = false;
|
|
gMutexes.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltMutexLock(void* mutex) {
|
|
std::shared_ptr<UltMutexState> state;
|
|
{
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gMutexes.find(mutex);
|
|
if (it == gMutexes.end()) {
|
|
return mutex == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
}
|
|
state = it->second;
|
|
}
|
|
const auto self = std::this_thread::get_id();
|
|
std::unique_lock<std::mutex> lock(state->_mutex);
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
if (state->_owner == self) {
|
|
if (state->_attribute == 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
++state->_lockCount;
|
|
return ULT_OK;
|
|
}
|
|
++state->_waiters;
|
|
try {
|
|
state->_available.wait(lock, [&] {
|
|
return !state->_alive || state->_lockCount == 0;
|
|
});
|
|
} catch (...) {
|
|
--state->_waiters;
|
|
throw;
|
|
}
|
|
--state->_waiters;
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
state->_ownerIdentity = UltCurrentThreadIdentity_nid_no_patch();
|
|
state->_owner = self;
|
|
state->_lockCount = 1;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltMutexTryLock(void* mutex) {
|
|
std::shared_ptr<UltMutexState> state;
|
|
{
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gMutexes.find(mutex);
|
|
if (it == gMutexes.end()) {
|
|
return mutex == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
}
|
|
state = it->second;
|
|
}
|
|
const auto self = std::this_thread::get_id();
|
|
std::lock_guard<std::mutex> lock(state->_mutex);
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
if (state->_owner == self) {
|
|
if (state->_attribute == 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
++state->_lockCount;
|
|
return ULT_OK;
|
|
}
|
|
if (state->_lockCount != 0) {
|
|
return ULT_ERROR_BUSY;
|
|
}
|
|
state->_ownerIdentity = UltCurrentThreadIdentity_nid_no_patch();
|
|
state->_owner = self;
|
|
state->_lockCount = 1;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltMutexUnlock(void* mutex) {
|
|
std::shared_ptr<UltMutexState> state;
|
|
{
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gMutexes.find(mutex);
|
|
if (it == gMutexes.end()) {
|
|
return mutex == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
}
|
|
state = it->second;
|
|
}
|
|
const auto self = std::this_thread::get_id();
|
|
std::lock_guard<std::mutex> lock(state->_mutex);
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
if (state->_owner != self) {
|
|
return ULT_ERROR_PERMISSION;
|
|
}
|
|
--state->_lockCount;
|
|
if (state->_lockCount == 0) {
|
|
state->_owner = {};
|
|
state->_ownerIdentity = {};
|
|
state->_available.notify_one();
|
|
}
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltConditionVariableCreate(void* condition, const char* name, void* mutex, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
if (condition == nullptr || mutex == nullptr) return ULT_ERROR_NULL;
|
|
if (optParam != nullptr) throw std::runtime_error("ULT condition variable options are not implemented");
|
|
std::lock_guard lock(gMutex);
|
|
const auto mutexIt = gMutexes.find(mutex);
|
|
if (mutexIt == gMutexes.end()) return ULT_ERROR_INVALID;
|
|
if (gConditions.contains(condition)) return ULT_ERROR_STATE;
|
|
auto state = std::make_shared<UltConditionState>();
|
|
state->_mutex = mutexIt->second;
|
|
state->_mutexObject = mutex;
|
|
if (name != nullptr) {
|
|
std::strncpy(state->_name, name, sizeof(state->_name) - 1);
|
|
}
|
|
std::lock_guard stateLock(state->_mutex->_mutex);
|
|
if (!state->_mutex->_alive) return ULT_ERROR_STATE;
|
|
gConditions.emplace(condition, state);
|
|
++state->_mutex->_conditions;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI _sceUltConditionVariableCreate(void* condition, const char* name, void* mutex, const void* optParam, std::uint32_t buildVersion) {
|
|
return sceUltConditionVariableCreate(condition, name, mutex, optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI sceUltConditionVariableDestroy(void* condition) {
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gConditions.find(condition);
|
|
if (it == gConditions.end()) return condition == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
auto state = it->second;
|
|
std::lock_guard stateLock(state->_mutex->_mutex);
|
|
if (state->_waiters != 0) return ULT_ERROR_BUSY;
|
|
state->_alive = false;
|
|
--state->_mutex->_conditions;
|
|
gConditions.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltConditionVariableSignal(void* condition) {
|
|
if (condition == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(condition) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
std::shared_ptr<UltConditionState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gConditions.find(condition);
|
|
if (it == gConditions.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
std::lock_guard stateLock(state->_mutex->_mutex);
|
|
if (!state->_alive || !state->_mutex->_alive) return ULT_ERROR_STATE;
|
|
if (!state->_queue.empty()) {
|
|
auto* waiter = state->_queue.front();
|
|
state->_queue.pop_front();
|
|
waiter->_signaled = true;
|
|
waiter->_available.notify_one();
|
|
}
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltConditionVariableWait(void* condition) {
|
|
std::shared_ptr<UltConditionState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gConditions.find(condition);
|
|
if (it == gConditions.end()) return condition == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
const auto held = state->_mutex;
|
|
const auto owner = std::this_thread::get_id();
|
|
std::unique_lock lock(held->_mutex);
|
|
if (!state->_alive || !held->_alive || held->_owner != owner || held->_lockCount == 0) return ULT_ERROR_STATE;
|
|
if (held->_lockCount != 1) throw std::runtime_error("ULT condition wait with a recursively held mutex is not implemented");
|
|
const auto ownerIdentity = held->_ownerIdentity;
|
|
UltConditionWaiter waiter;
|
|
state->_queue.push_back(&waiter);
|
|
++state->_waiters;
|
|
++held->_conditionWaiters;
|
|
held->_lockCount = 0;
|
|
held->_owner = {};
|
|
held->_ownerIdentity = {};
|
|
held->_available.notify_one();
|
|
try {
|
|
waiter._available.wait(lock, [&] { return waiter._signaled || !state->_alive || !held->_alive; });
|
|
if (held->_alive && state->_alive) {
|
|
++held->_waiters;
|
|
try {
|
|
held->_available.wait(lock, [&] { return !held->_alive || held->_lockCount == 0; });
|
|
} catch (...) {
|
|
--held->_waiters;
|
|
throw;
|
|
}
|
|
--held->_waiters;
|
|
}
|
|
} catch (...) {
|
|
state->_queue.remove(&waiter);
|
|
--state->_waiters;
|
|
--held->_conditionWaiters;
|
|
throw;
|
|
}
|
|
state->_queue.remove(&waiter);
|
|
--state->_waiters;
|
|
--held->_conditionWaiters;
|
|
if (!held->_alive || !state->_alive) return ULT_ERROR_STATE;
|
|
held->_owner = owner;
|
|
held->_ownerIdentity = ownerIdentity;
|
|
held->_lockCount = 1;
|
|
return ULT_OK;
|
|
}
|
|
|
|
std::uint64_t APS5_VABI sceUltWaitingQueueResourcePoolGetWorkAreaSize(std::uint32_t numThreads, std::uint32_t numSyncObjects) {
|
|
return 32ull * (numSyncObjects + 2u * numThreads + 2u);
|
|
}
|
|
|
|
int APS5_VABI sceUltWaitingQueueResourcePoolCreate(void* pool, const char* name, std::uint32_t numThreads, std::uint32_t numSyncObjects, void* workArea, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)optParam;
|
|
(void)buildVersion;
|
|
if (pool == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
std::memset(pool, 0, 256);
|
|
UltResourcePoolState state{numThreads, numSyncObjects, workArea};
|
|
if (name != nullptr) {
|
|
std::strncpy(state._name, name, sizeof(state._name) - 1);
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
gResourcePools[pool] = state;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltWaitingQueueResourcePoolDestroy(void* pool) {
|
|
if (pool == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gResourcePools.find(pool);
|
|
if (it == gResourcePools.end()) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
for (const auto& entry : gMutexes) {
|
|
if (entry.second->_waitingPool == pool) {
|
|
std::lock_guard<std::mutex> stateLock(entry.second->_mutex);
|
|
if (entry.second->_waiters != 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
}
|
|
}
|
|
for (const auto& entry : gSemaphores) {
|
|
if (entry.second->_waitingPool == pool) {
|
|
std::lock_guard<std::mutex> stateLock(entry.second->_mutex);
|
|
if (entry.second->_waiters != 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
}
|
|
}
|
|
for (const auto& entry : gQueues) {
|
|
if (entry.second->_pool && entry.second->_pool->_waitingPool == pool) {
|
|
std::lock_guard<std::mutex> poolLock(entry.second->_pool->_mutex);
|
|
if (entry.second->_waitingPushers != 0 || entry.second->_waitingPoppers != 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
}
|
|
}
|
|
if (UltReaderWriterLocksWaitOn(pool)) {
|
|
return ULT_ERROR_BUSY;
|
|
}
|
|
gResourcePools.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
std::uint64_t APS5_VABI sceUltQueueDataResourcePoolGetWorkAreaSize(std::uint32_t numData, std::uint64_t dataSize, std::uint32_t numQueueObject) {
|
|
const auto nodeArea = 32ull * (numQueueObject + 2u * numData + 3u);
|
|
if (dataSize != 0 && numData > (UINT64_MAX - nodeArea) / dataSize) {
|
|
throw std::out_of_range("ULT queue work area size overflow");
|
|
}
|
|
return static_cast<std::uint64_t>(numData) * dataSize + nodeArea;
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueDataResourcePoolCreate(void* pool, const char* name, std::uint32_t numData, std::uint64_t dataSize, std::uint32_t numQueueObject, void* waitingQueueResourcePool, void* workArea, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)optParam;
|
|
(void)buildVersion;
|
|
if (pool == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if ((reinterpret_cast<std::uintptr_t>(pool) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (numData == 0 || numQueueObject == 0 || dataSize == 0 ||
|
|
dataSize > std::numeric_limits<std::size_t>::max() / numData) return ULT_ERROR_RANGE;
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
|
|
return ULT_ERROR_INVALID;
|
|
}
|
|
if (gQueueDataPools.contains(pool)) return ULT_ERROR_STATE;
|
|
auto state = std::make_shared<UltQueueDataPoolState>();
|
|
state->_numData = numData;
|
|
state->_dataSize = dataSize;
|
|
state->_numQueueObject = numQueueObject;
|
|
state->_waitingPool = waitingQueueResourcePool;
|
|
state->_workArea = workArea;
|
|
if (name != nullptr) {
|
|
std::strncpy(state->_name, name, sizeof(state->_name) - 1);
|
|
}
|
|
state->_data.resize(numData * dataSize);
|
|
state->_next.resize(numData);
|
|
for (std::uint32_t slot = 0; slot < numData; ++slot) {
|
|
state->_next[slot] = slot + 1u < numData ? slot + 1u : UINT32_MAX;
|
|
}
|
|
state->_free = 0;
|
|
gQueueDataPools.emplace(pool, std::move(state));
|
|
std::memset(pool, 0, 512);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueDataResourcePoolDestroy(void* pool) {
|
|
if (pool == nullptr) return ULT_ERROR_NULL;
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gQueueDataPools.find(pool);
|
|
if (it == gQueueDataPools.end()) return ULT_ERROR_STATE;
|
|
auto state = it->second;
|
|
std::lock_guard stateLock(state->_mutex);
|
|
if (state->_queues != 0) return ULT_ERROR_BUSY;
|
|
gQueueDataPools.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueCreate(void* queue, const char* name, std::uint64_t dataSize, void* waitingQueueResourcePool, void* queueDataResourcePool, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)optParam;
|
|
(void)buildVersion;
|
|
if (queue == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if ((reinterpret_cast<std::uintptr_t>(queue) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (dataSize == 0) return ULT_ERROR_RANGE;
|
|
auto state = std::make_shared<UltQueueState>();
|
|
state->_dataSize = dataSize;
|
|
state->_waitingPool = waitingQueueResourcePool;
|
|
state->_dataPool = queueDataResourcePool;
|
|
if (name != nullptr) {
|
|
std::strncpy(state->_name, name, sizeof(state->_name) - 1);
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto dataPoolIt = gQueueDataPools.find(queueDataResourcePool);
|
|
if (dataPoolIt == gQueueDataPools.end()) {
|
|
return ULT_ERROR_INVALID;
|
|
}
|
|
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
|
|
return ULT_ERROR_INVALID;
|
|
}
|
|
if (gQueues.contains(queue)) return ULT_ERROR_STATE;
|
|
state->_pool = dataPoolIt->second;
|
|
auto& pool = *state->_pool;
|
|
std::lock_guard stateLock(pool._mutex);
|
|
if (dataSize > pool._dataSize || waitingQueueResourcePool != pool._waitingPool) return ULT_ERROR_INVALID;
|
|
if (pool._queues == pool._numQueueObject) return ULT_ERROR_BUSY;
|
|
gQueues.emplace(queue, std::move(state));
|
|
++pool._queues;
|
|
std::memset(queue, 0, 512);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltQueuePush(void* queue, const void* data) {
|
|
return QueueTransfer(queue, nullptr, data, true, true);
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueTryPush(void* queue, const void* data) {
|
|
return QueueTransfer(queue, nullptr, data, true, false);
|
|
}
|
|
|
|
int APS5_VABI sceUltQueuePop(void* queue, void* data) {
|
|
return QueueTransfer(queue, data, nullptr, false, true);
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueTryPop(void* queue, void* data) {
|
|
return QueueTransfer(queue, data, nullptr, false, false);
|
|
}
|
|
|
|
int APS5_VABI sceUltQueueDestroy(void* queue) {
|
|
if (queue == nullptr) return ULT_ERROR_NULL;
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gQueues.find(queue);
|
|
if (it == gQueues.end()) return ULT_ERROR_STATE;
|
|
auto state = it->second;
|
|
auto& pool = *state->_pool;
|
|
{
|
|
std::lock_guard stateLock(pool._mutex);
|
|
if (state->_waitingPushers != 0 || state->_waitingPoppers != 0) return ULT_ERROR_STATE;
|
|
ReleaseQueue(*state);
|
|
gQueues.erase(it);
|
|
}
|
|
pool._spaceAvailable.notify_all();
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltSemaphoreCreate(void* semaphore, const char* name, std::uint32_t numInitialResource, void* waitingQueueResourcePool, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)optParam;
|
|
(void)buildVersion;
|
|
if (semaphore == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if ((reinterpret_cast<std::uintptr_t>(semaphore) & 7u) != 0) {
|
|
return ULT_ERROR_ALIGNMENT;
|
|
}
|
|
auto state = std::make_shared<UltSemaphoreState>();
|
|
state->_resources = std::bit_cast<std::int32_t>(numInitialResource);
|
|
state->_waitingPool = waitingQueueResourcePool;
|
|
if (name != nullptr) {
|
|
std::strncpy(state->_name, name, sizeof(state->_name) - 1);
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
|
|
return ULT_ERROR_INVALID;
|
|
}
|
|
if (gSemaphores.find(semaphore) != gSemaphores.end()) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
std::memset(semaphore, 0, 256);
|
|
gSemaphores[semaphore] = std::move(state);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltSemaphoreAcquire(void* semaphore, std::int32_t numResource) {
|
|
if (semaphore == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(semaphore) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (numResource <= 0) {
|
|
return ULT_ERROR_RANGE;
|
|
}
|
|
std::shared_ptr<UltSemaphoreState> state;
|
|
if (int ret = semaphoreGetState(semaphore, &state); ret != ULT_OK) {
|
|
return ret;
|
|
}
|
|
std::unique_lock<std::mutex> lock(state->_mutex);
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
state->_waiters++;
|
|
state->_available.wait(lock, [&] { return !state->_alive || state->_resources >= numResource; });
|
|
state->_waiters--;
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
state->_resources -= numResource;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltSemaphoreTryAcquire(void* semaphore, std::int32_t numResource) {
|
|
if (semaphore == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(semaphore) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (numResource <= 0) {
|
|
return ULT_ERROR_RANGE;
|
|
}
|
|
std::shared_ptr<UltSemaphoreState> state;
|
|
if (int ret = semaphoreGetState(semaphore, &state); ret != ULT_OK) {
|
|
return ret;
|
|
}
|
|
std::lock_guard<std::mutex> lock(state->_mutex);
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
if (state->_resources < numResource) {
|
|
return ULT_ERROR_AGAIN;
|
|
}
|
|
state->_resources -= numResource;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltSemaphoreRelease(void* semaphore, std::int32_t numResource) {
|
|
if (semaphore == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(semaphore) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (numResource <= 0) {
|
|
return ULT_ERROR_RANGE;
|
|
}
|
|
std::shared_ptr<UltSemaphoreState> state;
|
|
if (int ret = semaphoreGetState(semaphore, &state); ret != ULT_OK) {
|
|
return ret;
|
|
}
|
|
{
|
|
std::lock_guard<std::mutex> lock(state->_mutex);
|
|
if (!state->_alive) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
if (state->_resources > INT32_MAX - numResource) {
|
|
return ULT_ERROR_RANGE;
|
|
}
|
|
state->_resources += numResource;
|
|
}
|
|
state->_available.notify_all();
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltSemaphoreDestroy(void* semaphore) {
|
|
if (semaphore == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(semaphore) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
std::lock_guard<std::mutex> globalLock(gMutex);
|
|
auto it = gSemaphores.find(semaphore);
|
|
if (it == gSemaphores.end()) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
auto state = it->second;
|
|
std::lock_guard<std::mutex> stateLock(state->_mutex);
|
|
if (state->_waiters != 0) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
state->_alive = false;
|
|
gSemaphores.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadRuntimeOptParamInitialize(UltUlthreadRuntimeOptParam* optParam, std::uint32_t buildVersion) {
|
|
if (optParam == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
const std::uint32_t defaults[] = {0x6C696275, 0x6C746D61, 0x67696300, buildVersion, 0x4000, 0, 0x1FFF, 0, 0x2BC, 4};
|
|
std::memset(optParam, 0, sizeof(*optParam));
|
|
std::memcpy(optParam->bytes, defaults, sizeof(defaults));
|
|
return ULT_OK;
|
|
}
|
|
|
|
std::uint64_t APS5_VABI sceUltUlthreadRuntimeGetWorkAreaSize(std::uint32_t maxNumUlthread, std::uint32_t numWorkerThread) {
|
|
const std::uint32_t totalThreads = maxNumUlthread + numWorkerThread;
|
|
const auto threadNodes = 32ull * (2u * maxNumUlthread + 4u);
|
|
const auto threadArea = (threadNodes + 4ull * maxNumUlthread + 7u) & 0x3FFFFFFFF8ull;
|
|
const auto schedulerArea = 32ull * (2u * numWorkerThread + 4u) + 32ull * (2u * totalThreads + 4u);
|
|
return threadArea + schedulerArea + 512ull * numWorkerThread + 8ull * (static_cast<std::uint64_t>(maxNumUlthread) + totalThreads);
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadRuntimeCreate(void* runtime, const char* name, std::uint32_t maxNumUlthread, std::uint32_t numWorkerThread, void* workArea, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)buildVersion;
|
|
if (runtime == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if (optParam != nullptr) {
|
|
std::uint32_t header[3];
|
|
std::memcpy(header, optParam, sizeof(header));
|
|
if (header[0] != 0x6C696275 || header[1] != 0x6C746D61 || header[2] != 0x67696300) return ULT_ERROR_INVALID;
|
|
}
|
|
std::memset(runtime, 0, 4096);
|
|
UltRuntimeState state{maxNumUlthread, numWorkerThread, workArea};
|
|
if (name != nullptr) {
|
|
std::strncpy(state._name, name, sizeof(state._name) - 1);
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
gRuntimes[runtime] = state;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadCreate(void* ulthread, const char* name, UltUlthreadEntry entry, std::uint64_t arg, void* context, std::uint64_t sizeContext, void* runtime, const void* optParam, std::uint32_t buildVersion) {
|
|
(void)context;
|
|
(void)sizeContext;
|
|
(void)optParam;
|
|
(void)buildVersion;
|
|
if (ulthread == nullptr || entry == nullptr || runtime == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
auto state = std::make_shared<UltUlthreadState>();
|
|
state->_entry = entry;
|
|
state->_arg = arg;
|
|
state->_object = ulthread;
|
|
state->_runtime = runtime;
|
|
if (name != nullptr) {
|
|
std::strncpy(state->_name, name, sizeof(state->_name) - 1);
|
|
}
|
|
std::lock_guard lock(gMutex);
|
|
if (!gRuntimes.contains(runtime) || gUlthreads.contains(ulthread)) return ULT_ERROR_STATE;
|
|
const std::string threadName = name != nullptr ? std::string(name).substr(0, 31) : std::string();
|
|
gUlthreads.emplace(ulthread, state);
|
|
std::memset(ulthread, 0, 512);
|
|
try {
|
|
if (scePthreadCreate(&state->_thread, nullptr, ulthreadRunner, state.get(), threadName.c_str()) != 0) {
|
|
gUlthreads.erase(ulthread);
|
|
return ULT_ERROR_AGAIN;
|
|
}
|
|
} catch (...) {
|
|
if (state->_thread == nullptr) gUlthreads.erase(ulthread);
|
|
throw;
|
|
}
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadJoin(void* ulthread, std::int32_t* status) {
|
|
return joinUlthread(ulthread, status, true);
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadTryJoin(void* ulthread, std::int32_t* status) {
|
|
return joinUlthread(ulthread, status, false);
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadGetSelf(void** ulthread) {
|
|
if (ulthread == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if (gCurrentUlthread == nullptr) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
*ulthread = gCurrentUlthread->_object;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadYield() {
|
|
if (gCurrentUlthread == nullptr) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
scePthreadYield();
|
|
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadExit(std::int32_t status) {
|
|
if (gCurrentUlthread == nullptr) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
gCurrentUlthread->_exited.store(true, std::memory_order_release);
|
|
scePthreadExit(reinterpret_cast<void*>(static_cast<std::intptr_t>(status)));
|
|
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadRuntimeDestroy(void* runtime) {
|
|
if (runtime == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
std::lock_guard<std::mutex> lock(gMutex);
|
|
auto it = gRuntimes.find(runtime);
|
|
if (it == gRuntimes.end()) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
for (const auto& entry : gUlthreads) {
|
|
if (entry.second->_runtime == runtime) {
|
|
return ULT_ERROR_STATE;
|
|
}
|
|
}
|
|
gRuntimes.erase(it);
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI _sceUltWaitingQueueResourcePoolOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltWaitingQueueResourcePoolOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltWaitingQueueResourcePoolCreate(void* pool, const char* name, std::uint32_t numThreads, std::uint32_t numSyncObjects, void* workArea, const void* optParam) {
|
|
if (pool == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(pool) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (name == nullptr) return ULT_ERROR_NULL;
|
|
if (const int result = validateCommonOptParam(optParam); result != ULT_OK) return result;
|
|
if (numThreads == 0 || numSyncObjects == 0) return ULT_ERROR_INVALID;
|
|
if ((reinterpret_cast<std::uintptr_t>(workArea) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (workArea == nullptr) return ULT_ERROR_NULL;
|
|
return sceUltWaitingQueueResourcePoolCreate(pool, name, numThreads, numSyncObjects, workArea, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI _sceUltSemaphoreOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltSemaphoreOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltSemaphoreCreate(void* semaphore, const char* name, std::uint32_t numInitialResource, void* waitingQueueResourcePool, const void* optParam) {
|
|
if (semaphore == nullptr || name == nullptr) return ULT_ERROR_NULL;
|
|
if (((reinterpret_cast<std::uintptr_t>(semaphore) | reinterpret_cast<std::uintptr_t>(waitingQueueResourcePool)) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (const int result = validateCommonOptParam(optParam); result != ULT_OK) return result;
|
|
return sceUltSemaphoreCreate(semaphore, name, numInitialResource, waitingQueueResourcePool, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI _sceUltMutexOptParamInitialize(UltMutexOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltMutexOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltMutexCreate(void* mutex, const char* name, void* waitingQueueResourcePool, const UltMutexOptParam* optParam) {
|
|
if (mutex == nullptr || name == nullptr || waitingQueueResourcePool == nullptr) {
|
|
return ULT_ERROR_NULL;
|
|
}
|
|
if (((reinterpret_cast<std::uintptr_t>(mutex) | reinterpret_cast<std::uintptr_t>(optParam) | reinterpret_cast<std::uintptr_t>(waitingQueueResourcePool)) & 7u) != 0) {
|
|
return ULT_ERROR_ALIGNMENT;
|
|
}
|
|
if (optParam != nullptr && (optParam->header[0] != 0x6C696275 || optParam->header[1] != 0x6C746D61 || optParam->header[2] != 0x67696300 || optParam->attribute > 1 || std::any_of(std::begin(optParam->reserved), std::end(optParam->reserved), [](std::uint32_t value) { return value != 0; }))) {
|
|
return ULT_ERROR_INVALID;
|
|
}
|
|
return sceUltMutexCreate(mutex, name, waitingQueueResourcePool, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI _sceUltQueueDataResourcePoolOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltQueueDataResourcePoolOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltQueueDataResourcePoolCreate(void* pool, const char* name, std::uint32_t numData, std::uint64_t dataSize, std::uint32_t numQueueObject, void* waitingQueueResourcePool, void* workArea, const void* optParam) {
|
|
if (pool == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(pool) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (const int result = validateCommonOptParam(optParam); result != ULT_OK) return result;
|
|
if (numData == 0 || dataSize == 0 || numQueueObject == 0) return ULT_ERROR_INVALID;
|
|
if ((reinterpret_cast<std::uintptr_t>(workArea) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
if (workArea == nullptr || name == nullptr) return ULT_ERROR_NULL;
|
|
if ((reinterpret_cast<std::uintptr_t>(waitingQueueResourcePool) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
return sceUltQueueDataResourcePoolCreate(pool, name, numData, dataSize, numQueueObject, waitingQueueResourcePool, workArea, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI _sceUltQueueOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltQueueOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltQueueCreate(void* queue, const char* name, std::uint64_t dataSize, void* waitingQueueResourcePool, void* queueDataResourcePool, const void* optParam) {
|
|
if (queue == nullptr || name == nullptr || queueDataResourcePool == nullptr) return ULT_ERROR_NULL;
|
|
if (((reinterpret_cast<std::uintptr_t>(queue) | reinterpret_cast<std::uintptr_t>(waitingQueueResourcePool) | reinterpret_cast<std::uintptr_t>(queueDataResourcePool)) & 7u) != 0) return ULT_ERROR_ALIGNMENT;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gQueueDataPools.find(queueDataResourcePool);
|
|
if (it == gQueueDataPools.end()) return ULT_ERROR_INVALID;
|
|
if (dataSize == 0 || dataSize > it->second->_dataSize) return ULT_ERROR_INVALID;
|
|
}
|
|
if (const int result = validateCommonOptParam(optParam); result != ULT_OK) return result;
|
|
return sceUltQueueCreate(queue, name, dataSize, waitingQueueResourcePool, queueDataResourcePool, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI _sceUltUlthreadRuntimeOptParamInitialize(UltUlthreadRuntimeOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltUlthreadRuntimeOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltUlthreadRuntimeCreate(void* runtime, const char* name, std::uint32_t maxNumUlthread, std::uint32_t numWorkerThread, void* workArea, const void* optParam) {
|
|
return sceUltUlthreadRuntimeCreate(runtime, name, maxNumUlthread, numWorkerThread, workArea, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI _sceUltUlthreadOptParamInitialize(UltOptParam* optParam, std::uint32_t buildVersion) {
|
|
return sceUltUlthreadOptParamInitialize(optParam, buildVersion);
|
|
}
|
|
|
|
int APS5_VABI _sceUltUlthreadCreate(void* ulthread, const char* name, UltUlthreadEntry entry, std::uint64_t arg, void* context, std::uint64_t sizeContext, void* runtime, const void* optParam) {
|
|
return sceUltUlthreadCreate(ulthread, name, entry, arg, context, sizeContext, runtime, optParam, 0);
|
|
}
|
|
|
|
int APS5_VABI sceUltConditionVariableSignalAll(void* conditionVariable) {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetConditionVariableInfo(void* object, UltConditionVariableInfo* info, UltWaitingThreadInfo* waitingThreads, std::uint32_t capacity) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
if (waitingThreads == nullptr && capacity != 0) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltConditionState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gConditions.find(object);
|
|
if (it == gConditions.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
std::lock_guard stateLock(state->_mutex->_mutex);
|
|
if (!state->_alive) return ULT_ERROR_STATE;
|
|
if (capacity != 0 && !state->_queue.empty()) NotImplemented_nid_no_patch(__func__);
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltConditionVariableInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state->_name, sizeof(info->name));
|
|
info->mutex = state->_mutexObject;
|
|
info->waitingThreads = static_cast<std::uint32_t>(state->_queue.size());
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetMutexInfo(void* object, UltMutexInfo* info, UltWaitingThreadInfo* waitingThreads, std::uint32_t capacity) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
if (waitingThreads == nullptr && capacity != 0) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltMutexState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gMutexes.find(object);
|
|
if (it == gMutexes.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
std::lock_guard stateLock(state->_mutex);
|
|
if (!state->_alive) return ULT_ERROR_STATE;
|
|
for (std::uint32_t index = 0; index < capacity; ++index) {
|
|
if (waitingThreads[index].size != sizeof(UltWaitingThreadInfo)) return ULT_ERROR_INVALID;
|
|
}
|
|
if (capacity != 0 && state->_waiters != 0) NotImplemented_nid_no_patch(__func__);
|
|
if (state->_lockCount != 0 && (state->_attribute != 0 || state->_lockCount > 1 || state->_ownerIdentity.type != 1)) NotImplemented_nid_no_patch(__func__);
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltMutexInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state->_name, sizeof(info->name));
|
|
info->waitingQueuePool = state->_waitingPool;
|
|
info->owner = state->_ownerIdentity.thread;
|
|
info->ownerType = state->_ownerIdentity.type;
|
|
info->attributes = state->_attribute;
|
|
info->waitingThreads = state->_waiters;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetQueueDataResourcePoolInfo(void* object, UltQueueDataResourcePoolInfo* info) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltQueueDataPoolState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gQueueDataPools.find(object);
|
|
if (it == gQueueDataPools.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
std::lock_guard poolLock(state->_mutex);
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltQueueDataResourcePoolInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state->_name, sizeof(info->name));
|
|
info->waitingQueuePool = state->_waitingPool;
|
|
info->dataSize = state->_dataSize;
|
|
info->maxItems = state->_numData;
|
|
info->maxQueues = state->_numQueueObject;
|
|
info->availableQueues = state->_numQueueObject > state->_queues ? state->_numQueueObject - state->_queues : 0;
|
|
std::uint32_t freeCount = 0;
|
|
for (auto next = state->_free; next != UINT32_MAX; next = state->_next[next]) {
|
|
++freeCount;
|
|
}
|
|
info->availableItems = freeCount;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetQueueInfo(void* object, UltQueueInfo* info, UltWaitingThreadInfo* waitingThreads, std::uint32_t capacity) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
if (waitingThreads == nullptr && capacity != 0) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltQueueState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gQueues.find(object);
|
|
if (it == gQueues.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
auto& pool = *state->_pool;
|
|
std::lock_guard poolLock(pool._mutex);
|
|
if (!state->_alive) return ULT_ERROR_STATE;
|
|
if (capacity != 0 && (state->_waitingPushers != 0 || state->_waitingPoppers != 0)) NotImplemented_nid_no_patch(__func__);
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltQueueInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state->_name, sizeof(info->name));
|
|
info->waitingQueuePool = state->_waitingPool;
|
|
info->dataPool = state->_dataPool;
|
|
info->dataSize = state->_dataSize;
|
|
info->queuedItems = state->_queuedItems;
|
|
info->waitingPushers = state->_waitingPushers;
|
|
info->waitingPoppers = state->_waitingPoppers;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetSemaphoreInfo(void* object, UltSemaphoreInfo* info, UltWaitingThreadInfo* waitingThreads, std::uint32_t capacity) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
if (waitingThreads == nullptr && capacity != 0) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltSemaphoreState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gSemaphores.find(object);
|
|
if (it == gSemaphores.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
std::lock_guard stateLock(state->_mutex);
|
|
if (!state->_alive) return ULT_ERROR_STATE;
|
|
if (capacity != 0 && state->_waiters != 0) NotImplemented_nid_no_patch(__func__);
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltSemaphoreInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state->_name, sizeof(info->name));
|
|
info->waitingQueuePool = state->_waitingPool;
|
|
info->resources = static_cast<std::uint32_t>(state->_resources);
|
|
info->waitingThreads = state->_waiters;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetUlthreadInfo(void* object, UltUlthreadInfo* info) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
std::shared_ptr<UltUlthreadState> state;
|
|
{
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gUlthreads.find(object);
|
|
if (it == gUlthreads.end()) return ULT_ERROR_STATE;
|
|
state = it->second;
|
|
}
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltUlthreadInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state->_name, sizeof(info->name));
|
|
info->runtime = state->_runtime;
|
|
info->contextAttribute = state->_contextAttribute;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetUlthreadRuntimeInfo(void* runtime, UltUlthreadRuntimeInfo* info, void** ulthreads, std::uint32_t ulthreadCapacity, Pthread* workerThreads, std::uint32_t workerCapacity) {
|
|
if (runtime == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
if (ulthreads == nullptr && ulthreadCapacity != 0) return ULT_ERROR_NULL;
|
|
if (workerThreads == nullptr && workerCapacity != 0) return ULT_ERROR_NULL;
|
|
if (workerThreads != nullptr && workerCapacity != 0) throw std::runtime_error("ULT ulthread runtime worker thread handles are not implemented");
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gRuntimes.find(runtime);
|
|
if (it == gRuntimes.end()) return ULT_ERROR_STATE;
|
|
const auto& state = it->second;
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltUlthreadRuntimeInfo);
|
|
info->object = runtime;
|
|
std::memcpy(info->name, state._name, sizeof(info->name));
|
|
info->maxUlthreads = state._maxNumUlthread;
|
|
info->workerThreads = state._numWorkerThread;
|
|
std::uint32_t count = 0;
|
|
for (const auto& [obj, ulthread] : gUlthreads) {
|
|
if (ulthread->_runtime == runtime && !ulthread->_exited.load(std::memory_order_acquire)) {
|
|
if (ulthreads != nullptr && count < ulthreadCapacity) {
|
|
ulthreads[count] = obj;
|
|
}
|
|
++count;
|
|
}
|
|
}
|
|
info->activeUlthreads = count;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltGetWaitingQueueResourcePoolInfo(void* object, UltWaitingQueueResourcePoolInfo* info) {
|
|
if (object == nullptr || info == nullptr) return ULT_ERROR_NULL;
|
|
std::lock_guard lock(gMutex);
|
|
const auto it = gResourcePools.find(object);
|
|
if (it == gResourcePools.end()) return ULT_ERROR_STATE;
|
|
const auto& state = it->second;
|
|
std::memset(info, 0, sizeof(*info));
|
|
info->size = sizeof(UltWaitingQueueResourcePoolInfo);
|
|
info->object = object;
|
|
std::memcpy(info->name, state._name, sizeof(info->name));
|
|
info->maxThreads = state._numThreads;
|
|
info->availableThreads = state._numThreads;
|
|
info->maxSyncObjects = state._numSyncObjects;
|
|
info->availableSyncObjects = state._numSyncObjects;
|
|
return ULT_OK;
|
|
}
|
|
|
|
int APS5_VABI sceUltAbstractThreadFactoryCreateThread() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltAbstractThreadFactoryGetNumMaxThreads() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltAbstractThreadFactoryJoinThread() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltKernelThreadFactoryFinalize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltKernelThreadFactoryGetFactoryPointer() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltKernelThreadFactoryInitialize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltModuleFinalize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltModuleInitialize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadFactoryFinalize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadFactoryGetFactoryPointer() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadFactoryGetWorkAreaSize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
int APS5_VABI sceUltUlthreadFactoryInitialize() {
|
|
NotImplemented_nid_no_patch(__func__);
|
|
return 0;
|
|
}
|
|
|
|
}
|