psOff_public/modules/libScePosix/entry.cpp

193 lines
5.3 KiB
C++
Raw Permalink Normal View History

2024-04-11 15:22:36 +00:00
#include "boost/date_time/posix_time/posix_time_types.hpp"
#include "boost/interprocess/sync/interprocess_semaphore.hpp"
2024-03-03 18:38:48 +00:00
#include "common.h"
#include "core/kernel/errors.h"
2024-03-06 13:40:30 +00:00
#include "core/kernel/semaphore.h"
2024-03-04 19:16:43 +00:00
#include "core/timer/timer.h"
2024-03-03 18:38:48 +00:00
#include "logging.h"
#include "types.h"
2024-03-04 19:16:43 +00:00
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
2024-04-11 15:22:36 +00:00
2024-03-03 18:38:48 +00:00
LOG_DEFINE_MODULE(libScePosix);
using ScePthreadSem_t = ISemaphore*;
extern "C" {
2024-03-05 12:52:16 +00:00
EXPORT const char* MODULE_NAME = "libkernel";
2024-03-03 18:38:48 +00:00
2024-04-11 15:22:36 +00:00
EXPORT SYSV_ABI int __NID(sem_init)(boost::interprocess::interprocess_semaphore** sem, int pshared, unsigned int value) {
*sem = new boost::interprocess::interprocess_semaphore(value);
2024-03-03 18:38:48 +00:00
return Ok;
}
2024-04-11 15:22:36 +00:00
EXPORT SYSV_ABI int __NID(sem_destroy)(boost::interprocess::interprocess_semaphore** sem) {
2024-03-03 18:38:48 +00:00
if (sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
delete *sem;
*sem = nullptr;
return Ok;
}
2024-04-11 15:22:36 +00:00
EXPORT SYSV_ABI int __NID(sem_post)(boost::interprocess::interprocess_semaphore** sem) {
2024-03-03 18:38:48 +00:00
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
2024-04-11 15:22:36 +00:00
(*sem)->post();
return Ok;
2024-03-03 18:38:48 +00:00
}
2024-05-05 09:19:44 +00:00
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;
}
2024-04-23 10:48:48 +00:00
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);
}
2024-04-11 15:22:36 +00:00
EXPORT SYSV_ABI int __NID(sem_trywait)(boost::interprocess::interprocess_semaphore** sem) {
2024-03-03 18:38:48 +00:00
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
2024-04-11 15:22:36 +00:00
return (*sem)->try_wait() ? Ok : POSIX_SET(ErrCode::_EBUSY);
2024-03-03 18:38:48 +00:00
}
// EXPORT SYSV_ABI int sem_unlink(const char* semName){}
2024-04-11 15:22:36 +00:00
EXPORT SYSV_ABI int __NID(sem_wait)(boost::interprocess::interprocess_semaphore** sem) {
2024-03-03 18:38:48 +00:00
if (sem == nullptr || *sem == nullptr) {
return POSIX_SET(ErrCode::_ESRCH);
}
2024-04-11 15:22:36 +00:00
(*sem)->wait();
return Ok;
2024-03-03 18:38:48 +00:00
}
2024-03-04 19:16:43 +00:00
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;
}
2024-03-05 12:52:16 +00:00
EXPORT SYSV_ABI int __NID(sched_yield)(void) {
2024-03-04 19:16:43 +00:00
boost::this_thread::yield();
return Ok;
}
2024-04-16 16:39:34 +00:00
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(socket)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(bind)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(setsockopt)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(select)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(recvfrom)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(listen)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(accept)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(sendto)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(recvmsg)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-26 20:57:44 +00:00
EXPORT SYSV_ABI int __NID(getsockname)() {
LOG_USE_MODULE(libScePosix);
return Ok;
}
EXPORT SYSV_ABI int __NID(connect)() {
2024-04-16 16:39:34 +00:00
LOG_USE_MODULE(libScePosix);
return Ok;
}
2024-04-16 16:45:01 +00:00
}