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
use crate::result::*;
use crate::svc;
use crate::arm;
pub struct RemoteEvent {
pub handle: svc::Handle
}
impl RemoteEvent {
pub const fn empty() -> Self {
Self { handle: svc::INVALID_HANDLE }
}
pub const fn new(handle: svc::Handle) -> Self {
Self { handle }
}
pub fn reset(&self) -> Result<()> {
svc::reset_signal(self.handle)
}
pub fn wait(&self, timeout: i64) -> Result<()> {
wait_handles(&[self.handle], timeout)?;
self.reset()
}
}
impl Drop for RemoteEvent {
fn drop(&mut self) {
let _ = svc::close_handle(self.handle);
}
}
pub struct SystemEvent {
pub server_handle: svc::Handle,
pub client_handle: svc::Handle
}
impl SystemEvent {
pub const fn empty() -> Self {
Self { server_handle: 0, client_handle: 0 }
}
pub fn new() -> Result<Self> {
let (server_handle, client_handle) = svc::create_event()?;
Ok(Self { server_handle, client_handle })
}
pub fn signal(&self) -> Result<()> {
svc::signal_event(self.server_handle)
}
}
impl Drop for SystemEvent {
fn drop(&mut self) {
let _ = svc::close_handle(self.client_handle);
let _ = svc::close_handle(self.server_handle);
}
}
pub enum WaiterType {
Handle,
HandleWithClear
}
pub const MAX_OBJECT_COUNT: u32 = 0x40;
#[allow(dead_code)]
pub struct Waiter {
handle: svc::Handle,
wait_type: WaiterType
}
impl Waiter {
pub const fn from(handle: svc::Handle, wait_type: WaiterType) -> Self {
Self { handle, wait_type }
}
pub const fn from_handle(handle: svc::Handle) -> Self {
Self::from(handle, WaiterType::Handle)
}
pub const fn from_handle_with_clear(handle: svc::Handle) -> Self {
Self::from(handle, WaiterType::HandleWithClear)
}
}
type WaitFn<W> = fn(&[W], i64) -> Result<usize>;
fn handles_wait_fn(handles: &[svc::Handle], timeout: i64) -> Result<usize> {
Ok(svc::wait_synchronization(handles.as_ptr(), handles.len() as u32, timeout)? as usize)
}
fn waiters_wait_fn(_waiters: &[Waiter], _timeout: i64) -> Result<usize> {
todo!();
}
fn wait_impl<W>(wait_objects: &[W], timeout: i64, wait_fn: WaitFn<W>) -> Result<usize> {
let has_timeout = timeout != -1;
let mut deadline: u64 = 0;
if has_timeout {
deadline = arm::get_system_tick() - arm::nanoseconds_to_ticks(timeout as u64);
}
loop {
let this_timeout = match has_timeout {
true => {
let remaining = deadline - arm::get_system_tick();
arm::ticks_to_nanoseconds(remaining) as i64
},
false => -1
};
match (wait_fn)(wait_objects, this_timeout) {
Ok(index) => return Ok(index),
Err(rc) => {
if svc::rc::ResultTimedOut::matches(rc) {
if has_timeout {
return Err(rc);
}
}
else if !svc::rc::ResultCancelled::matches(rc) {
return Err(rc);
}
}
}
}
}
pub fn wait(waiters: &[Waiter], timeout: i64) -> Result<usize> {
wait_impl(waiters, timeout, waiters_wait_fn)
}
pub fn wait_handles(handles: &[svc::Handle], timeout: i64) -> Result<usize> {
wait_impl(handles, timeout, handles_wait_fn)
}