Skip to content

Commit 2e5e1b5

Browse files
authored
Merge pull request #14 from Meziu/tweak/remove-svcGetThreadList
Remove reliance on svcGetThreadList and do some refactoring
2 parents 90bd4d0 + b84e4e1 commit 2e5e1b5

8 files changed

Lines changed: 912 additions & 734 deletions

File tree

src/condvar.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//! PThread condition variables implemented using libctru.
2+
3+
pub fn init() {}
4+
5+
#[no_mangle]
6+
pub unsafe extern "C" fn pthread_cond_init(
7+
cond: *mut libc::pthread_cond_t,
8+
_attr: *const libc::pthread_condattr_t,
9+
) -> libc::c_int {
10+
ctru_sys::CondVar_Init(cond as _);
11+
12+
0
13+
}
14+
15+
#[no_mangle]
16+
pub unsafe extern "C" fn pthread_cond_signal(cond: *mut libc::pthread_cond_t) -> libc::c_int {
17+
ctru_sys::CondVar_WakeUp(cond as _, 1);
18+
19+
0
20+
}
21+
22+
#[no_mangle]
23+
pub unsafe extern "C" fn pthread_cond_broadcast(cond: *mut libc::pthread_cond_t) -> libc::c_int {
24+
ctru_sys::CondVar_WakeUp(cond as _, -1);
25+
26+
0
27+
}
28+
29+
#[no_mangle]
30+
pub unsafe extern "C" fn pthread_cond_wait(
31+
cond: *mut libc::pthread_cond_t,
32+
lock: *mut libc::pthread_mutex_t,
33+
) -> libc::c_int {
34+
ctru_sys::CondVar_Wait(cond as _, lock as _);
35+
36+
0
37+
}
38+
39+
#[no_mangle]
40+
pub unsafe extern "C" fn pthread_cond_timedwait(
41+
cond: *mut libc::pthread_cond_t,
42+
lock: *mut libc::pthread_mutex_t,
43+
abstime: *const libc::timespec,
44+
) -> libc::c_int {
45+
// libctru expects a duration, but we have an absolute timestamp.
46+
// Convert to a duration before calling libctru.
47+
48+
// Get the current time so we can make a duration
49+
let mut now = libc::timeval {
50+
tv_sec: 0,
51+
tv_usec: 0,
52+
};
53+
let r = libc::gettimeofday(&mut now, std::ptr::null_mut());
54+
if r != 0 {
55+
return r;
56+
}
57+
58+
// Calculate the duration
59+
let duration_nsec = (*abstime)
60+
.tv_sec
61+
// Get the difference in seconds
62+
.saturating_sub(now.tv_sec)
63+
// Convert to nanoseconds
64+
.saturating_mul(1_000_000_000)
65+
// Add the difference in nanoseconds
66+
.saturating_add((*abstime).tv_nsec as i64)
67+
.saturating_sub(now.tv_usec as i64 * 1_000)
68+
// Don't go negative
69+
.max(0);
70+
71+
let r = ctru_sys::CondVar_WaitTimeout(cond as _, lock as _, duration_nsec);
72+
73+
// CondVar_WaitTimeout returns a boolean which is true (nonzero) if it timed out
74+
if r == 0 {
75+
0
76+
} else {
77+
libc::ETIMEDOUT
78+
}
79+
}
80+
81+
#[no_mangle]
82+
pub unsafe extern "C" fn pthread_cond_destroy(_cond: *mut libc::pthread_cond_t) -> libc::c_int {
83+
0
84+
}

0 commit comments

Comments
 (0)