ax-sync 0.6.0

ArceOS synchronization primitives
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Hidden runtime boundary for the OS-independent lock wrappers.

use core::{
    cell::UnsafeCell,
    mem::MaybeUninit,
    panic::Location,
    sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, AtomicU32, AtomicU64, AtomicUsize},
};

/// Do not alter the current execution context.
pub const CONTEXT_RAW: u8 = 0;
/// Disable preemption while the lock is held.
pub const CONTEXT_PREEMPT: u8 = 1;
/// Save and disable local IRQs while the guard is alive.
pub const CONTEXT_IRQSAVE: u8 = 2;
/// Disable preemption, then save and disable local IRQs.
pub const CONTEXT_PREEMPT_IRQSAVE: u8 = 3;

/// An exclusive spin lock operation.
pub const LOCK_KIND_SPIN: u8 = 0;
/// A spin read-write lock operation.
pub const LOCK_KIND_RW: u8 = 1;
/// A sleepable mutex operation.
pub const LOCK_KIND_MUTEX: u8 = 2;

/// Exclusive ownership.
pub const LOCK_MODE_EXCLUSIVE: u8 = 0;
/// Shared read ownership.
pub const LOCK_MODE_READ: u8 = 1;
/// Exclusive write ownership.
pub const LOCK_MODE_WRITE: u8 = 2;

/// Number of pointer-sized words reserved for the scheduler-owned PI waiter tree.
pub const PI_MUTEX_WAIT_STORAGE_WORDS: usize = 5;

/// Fixed external storage for one native PI-mutex core.
///
/// The provider interprets this storage as the native `ax-task` PI core. The
/// wrapper never reads or mutates the state machine itself.
#[repr(C)]
pub struct PiMutexStorage {
    owner_word: AtomicU64,
    generation: AtomicU64,
    wait_state: AtomicU8,
    wait_storage: UnsafeCell<[MaybeUninit<usize>; PI_MUTEX_WAIT_STORAGE_WORDS]>,
}

/// Exclusive borrow of every field in one external PI-mutex storage object.
#[doc(hidden)]
pub struct PiMutexStoragePartsMut<'lock> {
    pub owner_word: &'lock mut AtomicU64,
    pub generation: &'lock mut AtomicU64,
    pub wait_state: &'lock mut AtomicU8,
    pub wait_storage: &'lock mut UnsafeCell<[MaybeUninit<usize>; PI_MUTEX_WAIT_STORAGE_WORDS]>,
}

impl PiMutexStorage {
    /// Creates storage for an unlocked, generation-free PI mutex.
    pub const fn new() -> Self {
        Self {
            owner_word: AtomicU64::new(0),
            generation: AtomicU64::new(0),
            wait_state: AtomicU8::new(0),
            wait_storage: UnsafeCell::new([MaybeUninit::uninit(); PI_MUTEX_WAIT_STORAGE_WORDS]),
        }
    }

    /// Returns the native owner-word storage for provider layout validation.
    #[doc(hidden)]
    pub const fn owner_word(&self) -> &AtomicU64 {
        &self.owner_word
    }

    /// Returns the native lock-generation storage for provider layout validation.
    #[doc(hidden)]
    pub const fn generation(&self) -> &AtomicU64 {
        &self.generation
    }

    /// Returns the inline waiter lifecycle storage for provider layout validation.
    #[doc(hidden)]
    pub const fn wait_state(&self) -> &AtomicU8 {
        &self.wait_state
    }

    /// Returns the native inline waiter storage borrowed by the provider.
    #[doc(hidden)]
    pub const fn wait_storage(
        &self,
    ) -> &UnsafeCell<[MaybeUninit<usize>; PI_MUTEX_WAIT_STORAGE_WORDS]> {
        &self.wait_storage
    }

    /// Exclusively borrows every field for the native destruction transaction.
    #[doc(hidden)]
    pub fn parts_mut(&mut self) -> PiMutexStoragePartsMut<'_> {
        PiMutexStoragePartsMut {
            owner_word: &mut self.owner_word,
            generation: &mut self.generation,
            wait_state: &mut self.wait_state,
            wait_storage: &mut self.wait_storage,
        }
    }
}

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

// SAFETY: the provider publishes initialization through `wait_state` and
// serializes every access to the concrete object stored in `wait_storage`.
unsafe impl Sync for PiMutexStorage {}

/// Opaque execution-context restore state returned by the provider.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct ContextState {
    preempt: usize,
    irq: usize,
}

impl ContextState {
    /// Creates a provider context result.
    #[doc(hidden)]
    pub const fn new(preempt: usize, irq: usize) -> Self {
        Self { preempt, irq }
    }

    /// Returns the provider's preemption restore token.
    #[doc(hidden)]
    pub const fn preempt(self) -> usize {
        self.preempt
    }

    /// Returns the provider's raw local-IRQ restore state.
    #[doc(hidden)]
    pub const fn irq(self) -> usize {
        self.irq
    }
}

/// Result of one complete non-sleeping acquisition transaction.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct AcquireResult {
    acquired: u8,
    _reserved: [u8; 7],
    context_state: ContextState,
}

impl AcquireResult {
    /// Creates a provider result.
    #[doc(hidden)]
    pub const fn new(acquired: bool, context_state: ContextState) -> Self {
        Self {
            acquired: acquired as u8,
            _reserved: [0; 7],
            context_state,
        }
    }

    pub(crate) const fn acquired(self) -> bool {
        self.acquired != 0
    }

    pub(crate) const fn context_state(self) -> ContextState {
        self.context_state
    }
}

/// Lock-class storage whose layout is shared with the native provider.
#[repr(C)]
pub struct LockMetadata {
    class_id: AtomicU32,
    class_key: AtomicPtr<Location<'static>>,
}

impl LockMetadata {
    /// Creates metadata for a statically constructed lock class.
    #[track_caller]
    pub const fn new() -> Self {
        Self {
            class_id: AtomicU32::new(0),
            class_key: AtomicPtr::new(
                Location::caller() as *const Location<'static> as *mut Location<'static>
            ),
        }
    }

    /// Returns the class-id storage for the runtime adapter.
    #[doc(hidden)]
    pub const fn class_id(&self) -> &AtomicU32 {
        &self.class_id
    }

    /// Returns the class-key storage for the runtime adapter.
    #[doc(hidden)]
    pub const fn class_key(&self) -> &AtomicPtr<Location<'static>> {
        &self.class_key
    }
}

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

/// Complete execution-context operations used by standalone guards.
#[ax_crate_interface::def_interface]
pub trait ContextOps {
    /// Enters `context` and returns its opaque restore token.
    fn enter(context: u8) -> ContextState;

    /// Leaves `context` using the matching token.
    fn exit(context: u8, state: ContextState);

    /// Enters the preemption scope consumed by a hard-IRQ return epilogue.
    fn irq_return_preempt_enter() -> usize;

    /// Leaves one IRQ-return preemption scope while raw local IRQs stay disabled.
    fn irq_return_preempt_exit(state: usize);

    /// Publishes entry into the runtime hard-interrupt lifecycle.
    fn hardirq_enter();

    /// Publishes exit from the runtime hard-interrupt lifecycle.
    fn hardirq_exit();
}

/// Complete spin-lock acquisition and release operations.
#[ax_crate_interface::def_interface]
pub trait SpinOps {
    fn acquire(
        locked: &AtomicBool,
        metadata: &LockMetadata,
        lock_addr: usize,
        context: u8,
        subclass: u32,
        caller: &'static Location<'static>,
    ) -> ContextState;

    fn try_acquire(
        locked: &AtomicBool,
        metadata: &LockMetadata,
        lock_addr: usize,
        context: u8,
        subclass: u32,
        caller: &'static Location<'static>,
    ) -> AcquireResult;

    fn release(locked: &AtomicBool, lock_addr: usize, context: u8, context_state: ContextState);

    fn force_release(locked: &AtomicBool, lock_addr: usize, context: u8);

    fn is_locked(locked: &AtomicBool) -> bool;
}

/// Complete spin read-write lock operations.
#[ax_crate_interface::def_interface]
pub trait RwLockOps {
    fn acquire(
        state: &AtomicUsize,
        metadata: &LockMetadata,
        lock_addr: usize,
        context: u8,
        mode: u8,
        caller: &'static Location<'static>,
    ) -> ContextState;

    fn try_acquire(
        state: &AtomicUsize,
        metadata: &LockMetadata,
        lock_addr: usize,
        context: u8,
        mode: u8,
        caller: &'static Location<'static>,
    ) -> AcquireResult;

    fn release(
        state: &AtomicUsize,
        lock_addr: usize,
        context: u8,
        context_state: ContextState,
        mode: u8,
    );

    fn force_read_decrement(state: &AtomicUsize, lock_addr: usize, context: u8);
}

/// Complete sleepable mutex operations.
#[ax_crate_interface::def_interface]
pub trait MutexOps {
    fn acquire(
        storage: &PiMutexStorage,
        next_waiter_sequence: &AtomicU64,
        metadata: &LockMetadata,
        lock_addr: usize,
        subclass: u32,
        caller: &'static Location<'static>,
    );

    fn try_acquire(
        storage: &PiMutexStorage,
        next_waiter_sequence: &AtomicU64,
        metadata: &LockMetadata,
        lock_addr: usize,
        subclass: u32,
        caller: &'static Location<'static>,
    ) -> bool;

    fn release(storage: &PiMutexStorage, lock_addr: usize);

    fn force_release(storage: &PiMutexStorage, lock_addr: usize);

    fn is_owned_by_current(storage: &PiMutexStorage) -> bool;

    fn is_locked(storage: &PiMutexStorage) -> bool;

    fn destroy(storage: &mut PiMutexStorage);
}

/// Runtime lockdep diagnostics which do not belong to one lock acquisition.
#[ax_crate_interface::def_interface]
pub trait LockdepOps {
    fn set_trace_enabled(enabled: bool);
    fn dump_trace();
}

pub(crate) fn context_enter(context: u8) -> ContextState {
    ax_crate_interface::call_interface!(ContextOps::enter, context)
}

pub(crate) fn context_exit(context: u8, state: ContextState) {
    ax_crate_interface::call_interface!(ContextOps::exit, context, state);
}

pub(crate) fn irq_return_preempt_enter() -> usize {
    ax_crate_interface::call_interface!(ContextOps::irq_return_preempt_enter)
}

pub(crate) fn irq_return_preempt_exit(state: usize) {
    ax_crate_interface::call_interface!(ContextOps::irq_return_preempt_exit, state);
}

pub(crate) fn hardirq_enter() {
    ax_crate_interface::call_interface!(ContextOps::hardirq_enter);
}

pub(crate) fn hardirq_exit() {
    ax_crate_interface::call_interface!(ContextOps::hardirq_exit);
}

pub(crate) fn spin_acquire(
    locked: &AtomicBool,
    metadata: &LockMetadata,
    lock_addr: usize,
    context: u8,
    subclass: u32,
    caller: &'static Location<'static>,
) -> ContextState {
    ax_crate_interface::call_interface!(
        SpinOps::acquire,
        locked,
        metadata,
        lock_addr,
        context,
        subclass,
        caller
    )
}

pub(crate) fn spin_try_acquire(
    locked: &AtomicBool,
    metadata: &LockMetadata,
    lock_addr: usize,
    context: u8,
    subclass: u32,
    caller: &'static Location<'static>,
) -> AcquireResult {
    ax_crate_interface::call_interface!(
        SpinOps::try_acquire,
        locked,
        metadata,
        lock_addr,
        context,
        subclass,
        caller
    )
}

pub(crate) fn spin_release(
    locked: &AtomicBool,
    lock_addr: usize,
    context: u8,
    context_state: ContextState,
) {
    ax_crate_interface::call_interface!(
        SpinOps::release,
        locked,
        lock_addr,
        context,
        context_state
    );
}

pub(crate) fn spin_force_release(locked: &AtomicBool, lock_addr: usize, context: u8) {
    ax_crate_interface::call_interface!(SpinOps::force_release, locked, lock_addr, context);
}

pub(crate) fn spin_is_locked(locked: &AtomicBool) -> bool {
    ax_crate_interface::call_interface!(SpinOps::is_locked, locked)
}

pub(crate) fn rwlock_acquire(
    state: &AtomicUsize,
    metadata: &LockMetadata,
    lock_addr: usize,
    context: u8,
    mode: u8,
    caller: &'static Location<'static>,
) -> ContextState {
    ax_crate_interface::call_interface!(
        RwLockOps::acquire,
        state,
        metadata,
        lock_addr,
        context,
        mode,
        caller
    )
}

pub(crate) fn rwlock_try_acquire(
    state: &AtomicUsize,
    metadata: &LockMetadata,
    lock_addr: usize,
    context: u8,
    mode: u8,
    caller: &'static Location<'static>,
) -> AcquireResult {
    ax_crate_interface::call_interface!(
        RwLockOps::try_acquire,
        state,
        metadata,
        lock_addr,
        context,
        mode,
        caller
    )
}

pub(crate) fn rwlock_release(
    state: &AtomicUsize,
    lock_addr: usize,
    context: u8,
    context_state: ContextState,
    mode: u8,
) {
    ax_crate_interface::call_interface!(
        RwLockOps::release,
        state,
        lock_addr,
        context,
        context_state,
        mode
    );
}

pub(crate) fn rwlock_force_read_decrement(state: &AtomicUsize, lock_addr: usize, context: u8) {
    ax_crate_interface::call_interface!(RwLockOps::force_read_decrement, state, lock_addr, context);
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_acquire(
    storage: &PiMutexStorage,
    next_waiter_sequence: &AtomicU64,
    metadata: &LockMetadata,
    lock_addr: usize,
    subclass: u32,
    caller: &'static Location<'static>,
) {
    ax_crate_interface::call_interface!(
        MutexOps::acquire,
        storage,
        next_waiter_sequence,
        metadata,
        lock_addr,
        subclass,
        caller
    );
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_try_acquire(
    storage: &PiMutexStorage,
    next_waiter_sequence: &AtomicU64,
    metadata: &LockMetadata,
    lock_addr: usize,
    subclass: u32,
    caller: &'static Location<'static>,
) -> bool {
    ax_crate_interface::call_interface!(
        MutexOps::try_acquire,
        storage,
        next_waiter_sequence,
        metadata,
        lock_addr,
        subclass,
        caller
    )
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_release(storage: &PiMutexStorage, lock_addr: usize) {
    ax_crate_interface::call_interface!(MutexOps::release, storage, lock_addr);
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_force_release(storage: &PiMutexStorage, lock_addr: usize) {
    ax_crate_interface::call_interface!(MutexOps::force_release, storage, lock_addr);
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_is_owned_by_current(storage: &PiMutexStorage) -> bool {
    ax_crate_interface::call_interface!(MutexOps::is_owned_by_current, storage)
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_is_locked(storage: &PiMutexStorage) -> bool {
    ax_crate_interface::call_interface!(MutexOps::is_locked, storage)
}

#[cfg(feature = "sleep")]
pub(crate) fn mutex_destroy(storage: &mut PiMutexStorage) {
    ax_crate_interface::call_interface!(MutexOps::destroy, storage);
}

pub(crate) fn set_trace_enabled(enabled: bool) {
    ax_crate_interface::call_interface!(LockdepOps::set_trace_enabled, enabled);
}

pub(crate) fn dump_trace() {
    ax_crate_interface::call_interface!(LockdepOps::dump_trace);
}