Repository navigation
Expand file tree
/
Copy pathSem.cpp
More file actions
128 lines (113 loc) · 3.9 KB
/
Copy pathSem.cpp
File metadata and controls
128 lines (113 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstdint>
#include <cstddef>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "Common.hpp"
#include "../include/Cancel.hpp"
#include "prx/libkernel/Time/include/TimedWait.hpp"
#include <cerrno>
#include <chrono>
#include <cstring>
#include <mutex>
// The guest sem_t is at least 8 bytes; its first word holds a pointer to the host semaphore.
struct PosixSemaphore {
std::mutex lock;
TimedWait::Condition available;
unsigned int count;
};
static constexpr int GUEST_EAGAIN = 35;
static constexpr int GUEST_EOVERFLOW = 84;
static constexpr unsigned int GUEST_SEM_VALUE_MAX = 0x7fffffffu;
static constexpr int GUEST_REALTIME_CLOCK = 0;
static int Fail(int error) {
errno = error;
return -1;
}
static PosixSemaphore* Get(void* sem) {
if (!sem) return nullptr;
PosixSemaphore* semaphore = nullptr;
std::memcpy(&semaphore, sem, sizeof(semaphore));
return semaphore;
}
static int WaitFor(PosixSemaphore* semaphore, const KernelUseconds* usec) {
ThreadCancel::Check();
std::unique_lock lock(semaphore->lock);
const auto ready = [&] { return semaphore->count > 0; };
if (usec) {
if (!ThreadCancel::WaitUntil(semaphore->available, lock, TimedWait::DeadlineNanos(*usec), ready)) return Fail(PosixThread::GUEST_ETIMEDOUT);
} else {
ThreadCancel::WaitUntil(semaphore->available, lock, std::nullopt, ready);
}
--semaphore->count;
return 0;
}
extern "C" {
int APS5_VABI sem_init_nid_postfix(void* sem, int pshared, unsigned int value) {
(void)pshared;
if (!sem || value > GUEST_SEM_VALUE_MAX) return Fail(PosixThread::GUEST_EINVAL);
auto* semaphore = new PosixSemaphore();
semaphore->count = value;
std::memcpy(sem, &semaphore, sizeof(semaphore));
return 0;
}
int APS5_VABI sem_destroy_nid_postfix(void* sem) {
auto* semaphore = Get(sem);
if (!semaphore) return Fail(PosixThread::GUEST_EINVAL);
delete semaphore;
std::memset(sem, 0, sizeof(semaphore));
return 0;
}
int APS5_VABI sem_wait_nid_postfix(void* sem) {
auto* semaphore = Get(sem);
if (!semaphore) return Fail(PosixThread::GUEST_EINVAL);
return WaitFor(semaphore, nullptr);
}
int APS5_VABI sem_trywait_nid_postfix(void* sem) {
auto* semaphore = Get(sem);
if (!semaphore) return Fail(PosixThread::GUEST_EINVAL);
std::lock_guard lock(semaphore->lock);
if (semaphore->count == 0) return Fail(GUEST_EAGAIN);
--semaphore->count;
return 0;
}
int APS5_VABI sem_timedwait_nid_postfix(void* sem, const KernelTimespec* abstime) {
auto* semaphore = Get(sem);
if (!semaphore) return Fail(PosixThread::GUEST_EINVAL);
ThreadCancel::Check();
{
std::lock_guard lock(semaphore->lock);
if (semaphore->count > 0) {
--semaphore->count;
return 0;
}
}
if (!abstime) return WaitFor(semaphore, nullptr);
KernelUseconds usec = 0;
if (!PosixThread::RelativeMicroseconds(GUEST_REALTIME_CLOCK, abstime, &usec)) return Fail(PosixThread::GUEST_EINVAL);
return WaitFor(semaphore, &usec);
}
int APS5_VABI sem_reltimedwait_np_nid_postfix(void* sem, uint32_t usec) {
auto* semaphore = Get(sem);
if (!semaphore) return Fail(PosixThread::GUEST_EINVAL);
const KernelUseconds timeout = usec;
return WaitFor(semaphore, &timeout);
}
int APS5_VABI sem_post_nid_postfix(void* sem) {
auto* semaphore = Get(sem);
if (!semaphore) return Fail(PosixThread::GUEST_EINVAL);
{
std::lock_guard lock(semaphore->lock);
if (semaphore->count == GUEST_SEM_VALUE_MAX) return Fail(GUEST_EOVERFLOW);
++semaphore->count;
}
semaphore->available.NotifyOne();
return 0;
}
int APS5_VABI sem_getvalue_nid_postfix(void* sem, int* value) {
auto* semaphore = Get(sem);
if (!semaphore || !value) return Fail(PosixThread::GUEST_EINVAL);
std::lock_guard lock(semaphore->lock);
*value = static_cast<int>(semaphore->count);
return 0;
}
}