Repository navigation
Expand file tree
/
Copy pathThread.cpp
More file actions
141 lines (115 loc) · 4.86 KB
/
Copy pathThread.cpp
File metadata and controls
141 lines (115 loc) · 4.86 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
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cstdint>
#include <cstddef>
#include <cstring>
#include "SceTypes.hpp"
#include "../include/ThreadLifecycle.hpp"
#include "../include/Cancel.hpp"
#include "prx/libc/include/General.hpp"
#include "../include/Pthread.hpp"
#include "Common.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include <chrono>
#include <thread>
extern "C" {
int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name);
int APS5_VABI scePthreadDetach(Pthread thread);
void APS5_VABI scePthreadExit(void* retval);
int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
int APS5_VABI scePthreadRename(Pthread thread, const char* name);
int APS5_VABI scePthreadGetname(Pthread thread, char* name);
Pthread APS5_VABI scePthreadSelf();
int APS5_VABI scePthreadCancel(Pthread thread);
int APS5_VABI scePthreadSetcancelstate(int state, int* old_state);
int APS5_VABI scePthreadSetcanceltype(int type, int* old_type);
void APS5_VABI scePthreadTestcancel();
int APS5_VABI scePthreadSetprio(Pthread thread, int prio);
int APS5_VABI scePthreadGetprio(Pthread thread, int* prio);
}
static constexpr int GUEST_SCHED_FIFO = 1;
extern "C" {
int APS5_VABI pthread_create_nid_postfix(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg) {
return PosixThread::ToErrno(scePthreadCreate(thread, attr, entry, arg, nullptr));
}
int APS5_VABI pthread_create_name_np_nid_postfix(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name) {
return PosixThread::ToErrno(scePthreadCreate(thread, attr, entry, arg, name));
}
int APS5_VABI pthread_detach_nid_postfix(Pthread thread) {
return PosixThread::ToErrno(scePthreadDetach(thread));
}
void APS5_VABI pthread_exit_nid_postfix(void* value) {
scePthreadExit(value);
}
int APS5_VABI scePthreadGetschedparam(Pthread thread, int* policy, KernelSchedParam* param) {
if (!policy || !param) return SCE_KERNEL_ERROR_EINVAL;
*policy = GUEST_SCHED_FIFO;
return scePthreadGetprio(thread, ¶m->sched_priority);
}
int APS5_VABI scePthreadSetschedparam(Pthread thread, int policy, const KernelSchedParam* param) {
(void)policy;
if (!param) return SCE_KERNEL_ERROR_EINVAL;
return scePthreadSetprio(thread, param->sched_priority);
}
int APS5_VABI pthread_getschedparam_nid_postfix(Pthread thread, int* policy, KernelSchedParam* param) {
if (!policy || !param) return PosixThread::GUEST_EINVAL;
*policy = GUEST_SCHED_FIFO;
return PosixThread::ToErrno(scePthreadGetprio(thread, ¶m->sched_priority));
}
int APS5_VABI pthread_join_nid_postfix(Pthread thread, void** value) {
return PosixThread::ToErrno(scePthreadJoin(thread, value));
}
int APS5_VABI pthread_rename_np_nid_postfix(Pthread thread, const char* name) {
if (thread && name && std::strlen(name) >= 32) return PosixThread::ToErrno(SCE_KERNEL_ERROR_ENAMETOOLONG);
return PosixThread::ToErrno(scePthreadRename(thread, name));
}
int APS5_VABI pthread_getname_np_nid_postfix(Pthread thread, char* name) {
if (!thread) return PosixThread::GUEST_ESRCH;
if (!name) return PosixThread::GUEST_EFAULT;
return PosixThread::ToErrno(scePthreadGetname(thread, name));
}
Pthread APS5_VABI pthread_self_nid_postfix(void) {
return scePthreadSelf();
}
int APS5_VABI pthread_equal_nid_postfix(Pthread first, Pthread second) {
return first == second;
}
int APS5_VABI pthread_getcpuclockid_nid_postfix(Pthread thread, int* clockId) {
constexpr int guestFault = 14;
if (!thread) return PosixThread::GUEST_EINVAL;
if (!clockId) return guestFault;
*clockId = GuestThreadCpuClockId(thread);
return 0;
}
int APS5_VABI sched_yield_nid_postfix(void) {
std::this_thread::yield();
return 0;
}
int APS5_VABI pthread_cancel_nid_postfix(Pthread thread) {
return PosixThread::ToErrno(scePthreadCancel(thread));
}
int APS5_VABI pthread_setcancelstate_nid_postfix(int state, int* old_state) {
return PosixThread::ToErrno(scePthreadSetcancelstate(state, old_state));
}
int APS5_VABI pthread_setcanceltype_nid_postfix(int type, int* old_type) {
return PosixThread::ToErrno(scePthreadSetcanceltype(type, old_type));
}
int APS5_VABI pthread_setprio_nid_postfix(Pthread thread, int prio) {
return PosixThread::ToErrno(scePthreadSetprio(thread, prio));
}
int APS5_VABI pthread_setschedparam_nid_postfix(Pthread thread, int policy, const KernelSchedParam* param) {
(void)policy;
if (!param) return PosixThread::GUEST_EINVAL;
return PosixThread::ToErrno(scePthreadSetprio(thread, param->sched_priority));
}
void APS5_VABI pthread_testcancel_nid_postfix(void) {
scePthreadTestcancel();
}
void APS5_VABI pthread_yield_nid_postfix(void) {
std::this_thread::yield();
}
unsigned int APS5_VABI sleep_nid_postfix(unsigned int seconds) {
ThreadCancel::Check();
std::this_thread::sleep_for(std::chrono::seconds(seconds));
ThreadCancel::Check();
return 0;
}
}