-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathticktimer.rs
43 lines (38 loc) · 1.69 KB
/
ticktimer.rs
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
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
use crate::os::xous::ffi::Connection;
pub(crate) enum TicktimerScalar {
ElapsedMs,
SleepMs(usize),
LockMutex(usize /* cookie */),
UnlockMutex(usize /* cookie */),
WaitForCondition(usize /* cookie */, usize /* timeout (ms) */),
NotifyCondition(usize /* cookie */, usize /* count */),
FreeMutex(usize /* cookie */),
FreeCondition(usize /* cookie */),
}
impl Into<[usize; 5]> for TicktimerScalar {
fn into(self) -> [usize; 5] {
match self {
TicktimerScalar::ElapsedMs => [0, 0, 0, 0, 0],
TicktimerScalar::SleepMs(msecs) => [1, msecs, 0, 0, 0],
TicktimerScalar::LockMutex(cookie) => [6, cookie, 0, 0, 0],
TicktimerScalar::UnlockMutex(cookie) => [7, cookie, 0, 0, 0],
TicktimerScalar::WaitForCondition(cookie, timeout_ms) => [8, cookie, timeout_ms, 0, 0],
TicktimerScalar::NotifyCondition(cookie, count) => [9, cookie, count, 0, 0],
TicktimerScalar::FreeMutex(cookie) => [10, cookie, 0, 0, 0],
TicktimerScalar::FreeCondition(cookie) => [11, cookie, 0, 0, 0],
}
}
}
/// Returns a `Connection` to the ticktimer server. This server is used for synchronization
/// primitives such as sleep, Mutex, and Condvar.
pub(crate) fn ticktimer_server() -> Connection {
static TICKTIMER_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
let cid = TICKTIMER_SERVER_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
return cid.into();
}
let cid = crate::os::xous::ffi::connect("ticktimer-server".try_into().unwrap()).unwrap();
TICKTIMER_SERVER_CONNECTION.store(cid.into(), Ordering::Relaxed);
cid
}