Repository navigation
Expand file tree
/
Copy pathCancel.hpp
More file actions
74 lines (63 loc) · 1.94 KB
/
Copy pathCancel.hpp
File metadata and controls
74 lines (63 loc) · 1.94 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
#ifndef CORE_LIBS_PRX_LIBKERNEL_PTHREAD_CANCEL_HPP
#define CORE_LIBS_PRX_LIBKERNEL_PTHREAD_CANCEL_HPP
#include "SceTypes.hpp"
#include "prx/libkernel/Time/include/TimedWait.hpp"
#include <cstdint>
#include <optional>
inline constexpr int GUEST_CANCEL_ENABLE = 0;
inline constexpr int GUEST_CANCEL_DISABLE = 1;
inline constexpr int GUEST_CANCEL_DEFERRED = 0;
inline constexpr int GUEST_CANCEL_ASYNCHRONOUS = 2;
inline void* const GUEST_CANCELED = reinterpret_cast<void*>(std::uintptr_t{1});
struct CleanupHandler {
CleanupHandler* previous;
void (APS5_VABI* routine)(void*);
void* argument;
int onHeap;
};
extern "C" {
void APS5_VABI __pthread_cleanup_push_imp_nid_postfix(void (APS5_VABI* routine)(void*), void* argument, void* info);
void APS5_VABI __pthread_cleanup_pop_imp_nid_postfix(int execute);
}
namespace ThreadCancel {
bool Requested();
void Check();
void* CleanupMark();
void CleanupRestore(void* mark);
void BeginWait(TimedWait::Condition* condition);
void EndWait();
template <class TLock>
struct WaitLock {
TLock& inner;
void lock() {
EndWait();
inner.lock();
}
void unlock() { inner.unlock(); }
};
template <class TLock>
bool Wait(TimedWait::Condition& condition, TLock& lock, std::optional<std::uint64_t> deadlineNanos) {
BeginWait(&condition);
if (Requested()) {
EndWait();
return true;
}
WaitLock<TLock> waitLock{lock};
if (deadlineNanos) return condition.WaitUntil(waitLock, *deadlineNanos);
condition.Wait(waitLock);
return true;
}
template <class TLock, class TPredicate>
bool WaitUntil(TimedWait::Condition& condition, TLock& lock, std::optional<std::uint64_t> deadlineNanos, TPredicate predicate) {
while (!predicate()) {
const bool signaled = Wait(condition, lock, deadlineNanos);
if (Requested()) {
lock.unlock();
Check();
}
if (!signaled) return predicate();
}
return true;
}
}
#endif