sel4_driver_interfaces/
timer.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::cell::RefCell;
use core::fmt;
use core::ops::Deref;
use core::time::Duration;

use lock_api::{Mutex, RawMutex};

use crate::{HandleInterrupt, WrappedMutex, WrappedRefCell, WrappedRefCellError};

pub trait ErrorType {
    type Error: fmt::Debug;
}

pub trait Clock: ErrorType {
    fn get_time(&mut self) -> Result<Duration, Self::Error>;
}

pub trait Timers: Clock {
    type TimerLayout;

    type Timer;

    fn timer_layout(&mut self) -> Result<Self::TimerLayout, Self::Error>;

    fn set_timeout_on(&mut self, timer: Self::Timer, relative: Duration)
        -> Result<(), Self::Error>;

    fn clear_timeout_on(&mut self, timer: Self::Timer) -> Result<(), Self::Error>;
}

pub trait Timer: Clock {
    fn set_timeout(&mut self, relative: Duration) -> Result<(), Self::Error>;

    fn clear_timeout(&mut self) -> Result<(), Self::Error>;
}

pub struct TrivialTimers<T>(pub T);

impl<T: ErrorType> ErrorType for TrivialTimers<T> {
    type Error = T::Error;
}

impl<T: Clock> Clock for TrivialTimers<T> {
    fn get_time(&mut self) -> Result<Duration, Self::Error> {
        self.0.get_time()
    }
}

impl<T: Timer> Timers for TrivialTimers<T> {
    type TimerLayout = ();

    type Timer = ();

    fn timer_layout(&mut self) -> Result<Self::TimerLayout, Self::Error> {
        Ok(())
    }

    fn set_timeout_on(
        &mut self,
        _timer: Self::Timer,
        relative: Duration,
    ) -> Result<(), Self::Error> {
        self.0.set_timeout(relative)
    }

    fn clear_timeout_on(&mut self, _timer: Self::Timer) -> Result<(), Self::Error> {
        self.0.clear_timeout()
    }
}

pub struct DefaultTimer<T>(pub T);

impl<T: ErrorType> ErrorType for DefaultTimer<T> {
    type Error = T::Error;
}

impl<T: Clock> Clock for DefaultTimer<T> {
    fn get_time(&mut self) -> Result<Duration, Self::Error> {
        self.0.get_time()
    }
}

impl<T: Timers<Timer: Default>> Timer for DefaultTimer<T> {
    fn set_timeout(&mut self, relative: Duration) -> Result<(), Self::Error> {
        self.0.set_timeout_on(Default::default(), relative)
    }

    fn clear_timeout(&mut self) -> Result<(), Self::Error> {
        self.0.clear_timeout_on(Default::default())
    }
}

pub struct NumTimers(pub usize);

pub struct SingleTimer<T>(pub T);

impl<T: ErrorType> ErrorType for SingleTimer<T> {
    type Error = T::Error;
}

impl<T: Clock> Clock for SingleTimer<T> {
    fn get_time(&mut self) -> Result<Duration, Self::Error> {
        self.0.get_time()
    }
}

impl<T: Timer> Timers for SingleTimer<T> {
    type TimerLayout = NumTimers;

    type Timer = usize;

    fn timer_layout(&mut self) -> Result<Self::TimerLayout, Self::Error> {
        Ok(NumTimers(1))
    }

    fn set_timeout_on(
        &mut self,
        _timer: Self::Timer,
        relative: Duration,
    ) -> Result<(), Self::Error> {
        self.0.set_timeout(relative)
    }

    fn clear_timeout_on(&mut self, _timer: Self::Timer) -> Result<(), Self::Error> {
        self.0.clear_timeout()
    }
}

impl<T: HandleInterrupt> HandleInterrupt for SingleTimer<T> {
    fn handle_interrupt(&mut self) {
        self.0.handle_interrupt()
    }
}

// // //

impl<T: Deref<Target = RefCell<U>>, U: ErrorType> ErrorType for &WrappedRefCell<T> {
    type Error = WrappedRefCellError<U::Error>;
}

impl<T: Deref<Target = RefCell<U>>, U: Clock> Clock for &WrappedRefCell<T> {
    fn get_time(&mut self) -> Result<Duration, Self::Error> {
        self.with_mut(|this| this.get_time())
    }
}

impl<T: Deref<Target = RefCell<U>>, U: Timers> Timers for &WrappedRefCell<T> {
    type TimerLayout = U::TimerLayout;

    type Timer = U::Timer;

    fn timer_layout(&mut self) -> Result<Self::TimerLayout, Self::Error> {
        self.with_mut(|this| this.timer_layout())
    }

    fn set_timeout_on(
        &mut self,
        timer: Self::Timer,
        relative: Duration,
    ) -> Result<(), Self::Error> {
        self.with_mut(|this| this.set_timeout_on(timer, relative))
    }

    fn clear_timeout_on(&mut self, timer: Self::Timer) -> Result<(), Self::Error> {
        self.with_mut(|this| this.clear_timeout_on(timer))
    }
}

impl<T: Deref<Target = RefCell<U>>, U: Timer> Timer for &WrappedRefCell<T> {
    fn set_timeout(&mut self, relative: Duration) -> Result<(), Self::Error> {
        self.with_mut(|this| this.set_timeout(relative))
    }

    fn clear_timeout(&mut self) -> Result<(), Self::Error> {
        self.with_mut(|this| this.clear_timeout())
    }
}

impl<R: RawMutex, T: Deref<Target = Mutex<R, U>>, U: ErrorType> ErrorType for &WrappedMutex<T> {
    type Error = U::Error;
}

impl<R: RawMutex, T: Deref<Target = Mutex<R, U>>, U: Clock> Clock for &WrappedMutex<T> {
    fn get_time(&mut self) -> Result<Duration, Self::Error> {
        self.with_mut(|this| this.get_time())
    }
}

impl<R: RawMutex, T: Deref<Target = Mutex<R, U>>, U: Timers> Timers for &WrappedMutex<T> {
    type TimerLayout = U::TimerLayout;

    type Timer = U::Timer;

    fn timer_layout(&mut self) -> Result<Self::TimerLayout, Self::Error> {
        self.with_mut(|this| this.timer_layout())
    }

    fn set_timeout_on(
        &mut self,
        timer: Self::Timer,
        relative: Duration,
    ) -> Result<(), Self::Error> {
        self.with_mut(|this| this.set_timeout_on(timer, relative))
    }

    fn clear_timeout_on(&mut self, timer: Self::Timer) -> Result<(), Self::Error> {
        self.with_mut(|this| this.clear_timeout_on(timer))
    }
}

impl<R: RawMutex, T: Deref<Target = Mutex<R, U>>, U: Timer> Timer for &WrappedMutex<T> {
    fn set_timeout(&mut self, relative: Duration) -> Result<(), Self::Error> {
        self.with_mut(|this| this.set_timeout(relative))
    }

    fn clear_timeout(&mut self) -> Result<(), Self::Error> {
        self.with_mut(|this| this.clear_timeout())
    }
}