ax-task 0.6.10

ArceOS task management module
Documentation
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Execution-context guards used by the synchronization primitives.

use core::marker::PhantomData;

/// Saves the local interrupt state and disables local interrupts.
///
/// This low-level entry point exists for capability adapters whose trait API
/// transports the saved interrupt state separately from an RAII guard.
#[doc(hidden)]
#[inline(always)]
pub fn irq_save_and_disable() -> usize {
    imp::irq_save_and_disable()
}

/// Restores a local interrupt state returned by [`irq_save_and_disable`].
///
/// # Safety
///
/// `state` must come from the matching save operation on the current CPU and
/// must be restored exactly once, in properly nested order.
#[doc(hidden)]
#[inline(always)]
pub unsafe fn irq_restore(state: usize) {
    imp::irq_restore(state);
}

/// Internal critical-section contract used by spin-lock guards.
#[doc(hidden)]
pub trait GuardState {
    /// Saved state needed when the guard is released.
    type State: Clone + Copy;

    /// Enters the critical section.
    fn acquire() -> Self::State;

    /// Leaves the critical section.
    fn release(state: Self::State);

    /// Returns whether locks using this state participate in task lockdep.
    fn lockdep_enabled() -> bool {
        false
    }
}

/// Owns an entered critical section until a lock guard takes it over.
///
/// Lockdep validation can panic in host tests. Keeping the state in this
/// temporary guard ensures that every acquisition path restores its execution
/// context while unwinding.
pub(crate) struct PendingGuardState<G: GuardState> {
    state: Option<G::State>,
}

impl<G: GuardState> PendingGuardState<G> {
    #[inline(always)]
    pub(crate) fn acquire() -> Self {
        Self {
            state: Some(G::acquire()),
        }
    }

    #[inline(always)]
    pub(crate) fn into_state(mut self) -> G::State {
        self.state
            .take()
            .expect("pending guard state must be present")
    }
}

impl<G: GuardState> Drop for PendingGuardState<G> {
    #[inline(always)]
    fn drop(&mut self) {
        if let Some(state) = self.state.take() {
            G::release(state);
        }
    }
}

/// Raw lock state which does not alter the execution context.
#[doc(hidden)]
pub struct RawState;

/// Lock state which disables kernel preemption.
#[doc(hidden)]
pub struct PreemptState;

impl PreemptState {
    pub(crate) fn release_from_irq_return(state: usize) {
        imp::enable_preempt_from_irq_return(state);
    }
}

/// Lock state which saves and disables local interrupts.
#[doc(hidden)]
pub struct IrqSaveState;

/// Lock state which disables preemption, then saves and disables interrupts.
#[doc(hidden)]
pub struct PreemptIrqSaveState;

impl GuardState for RawState {
    type State = ();

    #[inline(always)]
    fn acquire() -> Self::State {}

    #[inline(always)]
    fn release(_state: Self::State) {}
}

impl GuardState for PreemptState {
    type State = usize;

    #[inline(always)]
    fn acquire() -> Self::State {
        imp::disable_preempt()
    }

    #[inline(always)]
    fn release(state: Self::State) {
        imp::enable_preempt(state);
    }

    fn lockdep_enabled() -> bool {
        true
    }
}

impl GuardState for IrqSaveState {
    type State = usize;

    #[inline(always)]
    fn acquire() -> Self::State {
        imp::irq_save_and_disable()
    }

    #[inline(always)]
    fn release(state: Self::State) {
        imp::irq_restore(state);
    }
}

impl GuardState for PreemptIrqSaveState {
    type State = usize;

    #[inline(always)]
    fn acquire() -> Self::State {
        let preemption = imp::disable_preempt();
        assert_eq!(preemption & 1, 0, "preemption token must be aligned");
        preemption | (imp::irq_save_and_disable() & 1)
    }

    #[inline(always)]
    fn release(state: Self::State) {
        imp::irq_restore(state & 1);
        imp::enable_preempt(state & !1);
    }

    fn lockdep_enabled() -> bool {
        true
    }
}

/// An RAII guard which disables kernel preemption while it is alive.
///
/// The guard is bound to the task that acquires it and cannot be transferred
/// to another execution context:
///
/// ```compile_fail
/// fn require_send<T: Send>() {}
/// require_send::<ax_task::sync::PreemptGuard>();
/// ```
pub struct PreemptGuard {
    state: <PreemptState as GuardState>::State,
    _not_send: PhantomData<*mut ()>,
}

impl PreemptGuard {
    /// Disables preemption and creates a guard which restores it on drop.
    pub fn new() -> Self {
        Self {
            state: PreemptState::acquire(),
            _not_send: PhantomData,
        }
    }
}

impl Default for PreemptGuard {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for PreemptGuard {
    fn drop(&mut self) {
        PreemptState::release(self.state);
    }
}

/// An RAII guard which saves and disables local interrupts while it is alive.
///
/// The saved IRQ state belongs to the acquiring CPU, so the guard cannot be
/// transferred to another execution context:
///
/// ```compile_fail
/// fn require_send<T: Send>() {}
/// require_send::<ax_task::sync::IrqSaveGuard>();
/// ```
pub struct IrqSaveGuard {
    state: <IrqSaveState as GuardState>::State,
    _not_send: PhantomData<*mut ()>,
}

impl IrqSaveGuard {
    /// Saves and disables local interrupts.
    pub fn new() -> Self {
        Self {
            state: IrqSaveState::acquire(),
            _not_send: PhantomData,
        }
    }
}

impl Default for IrqSaveGuard {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for IrqSaveGuard {
    fn drop(&mut self) {
        IrqSaveState::release(self.state);
    }
}

/// An RAII guard which disables preemption and local interrupts.
///
/// Entry disables preemption before interrupts. Drop restores interrupts
/// before re-enabling preemption, matching Linux spin-lock IRQ-save ordering.
///
/// Both saved states belong to the acquiring task and CPU, so the guard cannot
/// be transferred to another execution context:
///
/// ```compile_fail
/// fn require_send<T: Send>() {}
/// require_send::<ax_task::sync::PreemptIrqSaveGuard>();
/// ```
pub struct PreemptIrqSaveGuard {
    state: <PreemptIrqSaveState as GuardState>::State,
    _not_send: PhantomData<*mut ()>,
}

impl PreemptIrqSaveGuard {
    /// Enters a preemption-disabled, IRQ-disabled critical section.
    pub fn new() -> Self {
        Self {
            state: PreemptIrqSaveState::acquire(),
            _not_send: PhantomData,
        }
    }
}

impl Default for PreemptIrqSaveGuard {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for PreemptIrqSaveGuard {
    fn drop(&mut self) {
        PreemptIrqSaveState::release(self.state);
    }
}

#[cfg(all(feature = "host-test", not(target_os = "none")))]
mod imp {
    use std::cell::{Cell, RefCell};

    std::thread_local! {
        static PREEMPT_DEPTH: Cell<usize> = const { Cell::new(0) };
        static IRQ_ENABLED: Cell<bool> = const { Cell::new(true) };
        static EVENTS: RefCell<std::vec::Vec<&'static str>> = const { RefCell::new(std::vec::Vec::new()) };
    }

    pub(super) fn disable_preempt() -> usize {
        EVENTS.with_borrow_mut(|events| events.push("preempt-disable"));
        PREEMPT_DEPTH.set(PREEMPT_DEPTH.get() + 1);
        0
    }

    pub(super) fn enable_preempt(_state: usize) {
        EVENTS.with_borrow_mut(|events| events.push("preempt-enable"));
        PREEMPT_DEPTH.set(
            PREEMPT_DEPTH
                .get()
                .checked_sub(1)
                .expect("unbalanced preemption guard"),
        );
    }

    pub(super) fn enable_preempt_from_irq_return(state: usize) {
        enable_preempt(state);
    }

    pub(super) fn irq_save_and_disable() -> usize {
        EVENTS.with_borrow_mut(|events| events.push("irq-disable"));
        let was_enabled = IRQ_ENABLED.replace(false);
        usize::from(was_enabled)
    }

    pub(super) fn irq_restore(state: usize) {
        EVENTS.with_borrow_mut(|events| events.push("irq-restore"));
        IRQ_ENABLED.set(state != 0);
    }

    pub(super) fn snapshot() -> (usize, bool) {
        (PREEMPT_DEPTH.get(), IRQ_ENABLED.get())
    }

    #[cfg(all(test, feature = "host-test", not(target_os = "none")))]
    pub(super) fn take_events() -> std::vec::Vec<&'static str> {
        EVENTS.take()
    }

    pub(super) fn preempt_depth() -> usize {
        PREEMPT_DEPTH.get()
    }

    #[cfg(feature = "preempt")]
    pub(super) fn finish_initial_context_switch() {
        assert_eq!(
            PREEMPT_DEPTH.get(),
            1,
            "initial host context switch must inherit one exclusion depth"
        );
        PREEMPT_DEPTH.set(0);
    }
}

#[cfg(not(all(feature = "host-test", not(target_os = "none"))))]
mod imp {
    #[inline(always)]
    pub(super) fn disable_preempt() -> usize {
        #[cfg(feature = "multitask")]
        return crate::disable_preempt();
        #[cfg(not(feature = "multitask"))]
        0
    }

    #[inline(always)]
    pub(super) fn enable_preempt(state: usize) {
        #[cfg(feature = "multitask")]
        crate::enable_preempt(state);
        #[cfg(not(feature = "multitask"))]
        let _ = state;
    }

    #[inline(always)]
    pub(super) fn enable_preempt_from_irq_return(state: usize) {
        #[cfg(feature = "multitask")]
        crate::enable_preempt_from_irq_return(state);
        #[cfg(not(feature = "multitask"))]
        let _ = state;
    }

    #[inline(always)]
    pub(super) fn irq_save_and_disable() -> usize {
        let was_enabled = ax_hal::asm::irqs_enabled();
        ax_hal::asm::disable_irqs();
        usize::from(was_enabled)
    }

    #[inline(always)]
    pub(super) fn irq_restore(state: usize) {
        if state != 0 {
            ax_hal::asm::enable_irqs();
        } else {
            ax_hal::asm::disable_irqs();
        }
    }
}

/// Returns the preemption depth tracked by the host critical-section provider.
#[cfg(all(feature = "host-test", not(target_os = "none")))]
#[doc(hidden)]
pub fn host_preempt_depth() -> usize {
    imp::preempt_depth()
}

#[cfg(all(feature = "preempt", feature = "host-test", not(target_os = "none")))]
pub(crate) fn finish_initial_host_context_switch() {
    imp::finish_initial_context_switch();
}

#[cfg(all(feature = "host-test", not(target_os = "none")))]
pub(crate) fn host_context_snapshot() -> (usize, bool) {
    imp::snapshot()
}

#[cfg(all(test, feature = "host-test", not(target_os = "none")))]
mod tests {
    use super::{IrqSaveGuard, PreemptGuard, PreemptIrqSaveGuard, imp};

    #[test]
    fn preempt_guard_nests_and_restores_depth() {
        assert_eq!(imp::snapshot(), (0, true));
        let outer = PreemptGuard::new();
        assert_eq!(imp::snapshot(), (1, true));
        {
            let _inner = PreemptGuard::new();
            assert_eq!(imp::snapshot(), (2, true));
        }
        assert_eq!(imp::snapshot(), (1, true));
        drop(outer);
        assert_eq!(imp::snapshot(), (0, true));
    }

    #[test]
    fn irq_save_guard_preserves_nested_disabled_state() {
        assert_eq!(imp::snapshot(), (0, true));
        let outer = IrqSaveGuard::new();
        assert_eq!(imp::snapshot(), (0, false));
        {
            let _inner = IrqSaveGuard::new();
            assert_eq!(imp::snapshot(), (0, false));
        }
        assert_eq!(imp::snapshot(), (0, false));
        drop(outer);
        assert_eq!(imp::snapshot(), (0, true));
    }

    #[test]
    fn combined_guard_restores_irq_before_preempt_context() {
        assert_eq!(imp::snapshot(), (0, true));
        let _ = imp::take_events();
        let guard = PreemptIrqSaveGuard::new();
        assert_eq!(imp::snapshot(), (1, false));
        drop(guard);
        assert_eq!(imp::snapshot(), (0, true));
        assert_eq!(
            imp::take_events(),
            [
                "preempt-disable",
                "irq-disable",
                "irq-restore",
                "preempt-enable"
            ]
        );
    }
}