psOff_public/modules/libScePosix/entry.cpp
2024-05-05 11:19:44 +02:00

193 lines
5.3 KiB
C++

#include "boost/date_time/posix_time/posix_time_types.hpp"
#include "boost/interprocess/sync/interprocess_semaphore.hpp"
#include "common.h"
#include "core/kernel/errors.h"
#include "core/kernel/semaphore.h"
#include "core/timer/timer.h"
#include "logging.h"
#include "types.h"
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
LOG_DEFINE_MODULE(libScePosix);
using ScePthreadSem_t = ISemaphore*;
extern "C" {
EXPORT const char* MODULE_NAME = "libkernel";
EXPORT SYSV_ABI int __NID(sem_init)(boost::interprocess::interprocess_semaphore** sem, int pshared, unsigned int value) {
*sem = new boost::interprocess::interprocess_semaphore(value);
return Ok;
}
EXPORT SYSV_ABI int __NID(sem_destroy)(boost::interprocess::interprocess_semaphore** sem) {
if (sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
delete *sem;
*sem = nullptr;
return Ok;
}
EXPORT SYSV_ABI int __NID(sem_post)(boost::interprocess::interprocess_semaphore** sem) {
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
(*sem)->post();
return Ok;
}
EXPORT SYSV_ABI int __NID(sem_getvalue)(boost::interprocess::interprocess_semaphore** sem) {
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
return (*sem)->get_count();
return Ok;
}
EXPORT SYSV_ABI int __NID(sem_reltimedwait_np)(boost::interprocess::interprocess_semaphore** sem, SceKernelTimespec* reltime) {
auto now = boost::posix_time::microsec_clock::universal_time();
auto timeout = boost::posix_time::seconds(reltime->tv_sec) + boost::posix_time::microsec(reltime->tv_nsec / 1000);
return (*sem)->timed_wait(now + timeout) ? Ok : POSIX_SET(ErrCode::_ETIMEDOUT);
}
EXPORT SYSV_ABI int __NID(sem_timedwait)(boost::interprocess::interprocess_semaphore** sem, SceKernelTimespec* abstime) {
boost::posix_time::ptime pt = boost::posix_time::from_time_t(abstime->tv_sec) + boost::posix_time::microsec(abstime->tv_nsec / 1000);
return (*sem)->timed_wait(pt) ? Ok : POSIX_SET(ErrCode::_ETIMEDOUT);
}
EXPORT SYSV_ABI int __NID(sem_trywait)(boost::interprocess::interprocess_semaphore** sem) {
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
return (*sem)->try_wait() ? Ok : POSIX_SET(ErrCode::_EBUSY);
}
// EXPORT SYSV_ABI int sem_unlink(const char* semName){}
EXPORT SYSV_ABI int __NID(sem_wait)(boost::interprocess::interprocess_semaphore** sem) {
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
(*sem)->wait();
return Ok;
}
EXPORT SYSV_ABI unsigned int __NID(sleep)(unsigned int seconds) {
boost::this_thread::sleep_for(boost::chrono::seconds(seconds));
return Ok;
}
EXPORT SYSV_ABI int __NID(usleep)(SceKernelUseconds microseconds) {
boost::this_thread::sleep_for(boost::chrono::microseconds(microseconds));
return Ok;
}
EXPORT SYSV_ABI int __NID(nanosleep)(const SceKernelTimespec* rqtp, SceKernelTimespec* rmtp) {
auto startTime = boost::chrono::high_resolution_clock::now();
boost::this_thread::sleep_for(boost::chrono::seconds(rqtp->tv_sec) + boost::chrono::nanoseconds(rqtp->tv_nsec));
if (rmtp != nullptr) {
auto const endTime = boost::chrono::high_resolution_clock::now();
auto const diff = boost::chrono::duration_cast<boost::chrono::nanoseconds>(endTime - startTime).count();
ns2timespec(rmtp, diff);
}
return Ok;
}
EXPORT SYSV_ABI int __NID(clock_getres)(SceKernelClockid clockId, SceKernelTimespec* tp) {
return POSIX_CALL(accessTimer().getTimeRes(clockId, tp));
}
EXPORT SYSV_ABI int __NID(clock_gettime)(SceKernelClockid clockId, SceKernelTimespec* tp) {
return POSIX_CALL(accessTimer().getTime(clockId, tp));
}
EXPORT SYSV_ABI int __NID(gettimeofday)(SceKernelTimeval* tp) {
return POSIX_CALL(accessTimer().getTimeofDay(tp));
}
EXPORT SYSV_ABI int __NID(sched_get_priority_max)(int) {
return SCE_KERNEL_PRIO_FIFO_HIGHEST;
}
EXPORT SYSV_ABI int __NID(sched_get_priority_min)(int) {
return SCE_KERNEL_PRIO_FIFO_LOWEST;
}
EXPORT SYSV_ABI int __NID(sched_setparam)(int64_t, const struct sched_param*) {
LOG_USE_MODULE(libScePosix);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int __NID(sched_getparam)(int64_t, struct sched_param*) {
LOG_USE_MODULE(libScePosix);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int __NID(sched_yield)(void) {
boost::this_thread::yield();
return Ok;
}
EXPORT SYSV_ABI int __NID(socket)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(bind)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(setsockopt)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(select)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(recvfrom)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(listen)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(accept)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(sendto)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(recvmsg)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(getsockname)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(connect)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
}