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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright 2020 Branan Riley <me@branan.info>

//! Synchronization primitives

use core::{
    cell::UnsafeCell,
    ops::{Deref, DerefMut},
    sync::atomic::{AtomicUsize, Ordering},
};

#[cfg(not(mcu = "fe310g002"))]
use core::sync::atomic::AtomicBool;

/// A true or false flag
///
/// This provides [`AtomicBool`](core::sync::atomic::AtomicBool)-like
/// semantics across all supported MCUs. On MCUs without atomic
/// support, it is implemented as a critical section.
#[cfg(not(mcu = "fe310g002"))]
#[derive(Default)]
pub struct Flag(AtomicBool);

/// A true or false flag
///
/// This provides [`AtomicBool`](core::sync::atomic::AtomicBool)-like
/// semantics across all supported MCUs. On MCUs without atomic
/// support, it is implemented as a critical section.
#[cfg(mcu = "fe310g002")]
#[derive(Default)]
pub struct Flag(AtomicUsize);

unsafe impl Send for Flag {}
unsafe impl Sync for Flag {}

impl Flag {
    /// Create a new Flag
    pub const fn new(value: bool) -> Self {
        #[cfg(mcu = "fe310g002")]
        {
            Self(AtomicUsize::new(if value { 1 } else { 0 }))
        }

        #[cfg(not(mcu = "fe310g002"))]
        {
            Self(AtomicBool::new(value))
        }
    }

    /// Store a value to the flag.
    ///
    /// See [`core::sync::atomic::AtomicBool::store`]
    pub fn store(&self, value: bool, ordering: Ordering) {
        self.0.store(value.into(), ordering)
    }
}

#[cfg(any(doc, target_has_atomic = "8"))]
impl Flag {
    /// Stores a value, returning the previous value
    ///
    /// See [`core::sync::atomic::AtomicBool::swap`]
    pub fn swap(&self, value: bool, ordering: Ordering) -> bool {
        let out = self.0.swap(value.into(), ordering);
        #[cfg(mcu = "fe310g002")]
        {
            out != 0
        }

        #[cfg(not(mcu = "fe310g002"))]
        {
            out
        }
    }
}

#[cfg(not(any(doc, target_has_atomic = "8")))]
impl Flag {
    /// Stores a value, returning the previous value
    ///
    /// See [`core::sync::atomic::AtomicBool::swap`]
    pub fn swap(&self, value: bool, ordering: Ordering) -> bool {
        let (load_ordering, store_ordering) = match ordering {
            Ordering::Relaxed => (Ordering::Relaxed, Ordering::Relaxed),
            Ordering::Acquire => (Ordering::Acquire, Ordering::Relaxed),
            Ordering::Release => (Ordering::Relaxed, Ordering::Release),
            Ordering::AcqRel => (Ordering::Acquire, Ordering::Release),
            Ordering::SeqCst => (Ordering::SeqCst, Ordering::SeqCst),
            _ => panic!("Unsupported swap ordering"),
        };
        without_interrupts(|| {
            let out = self.0.load(load_ordering);
            self.0.store(value.into(), store_ordering);
            out
        })
    }
}

/// An atomic pointer-sized value
///
/// This provides [`AtomicUsize`](core::sync::atomic::AtomicUsize)-like
/// semantics across all supported MCUs. On MCUs without atomic
/// support, it is implemented as a critical section.
pub struct Value(AtomicUsize);

unsafe impl Send for Value {}
unsafe impl Sync for Value {}

impl Value {
    /// Create a new value
    pub const fn new(value: usize) -> Self {
        Self(AtomicUsize::new(value))
    }

    /// Store a value
    ///
    /// See [`core::sync::atomic::AtomicUsize::store`]
    pub fn store(&self, value: usize, ordering: Ordering) {
        self.0.store(value, ordering)
    }

    /// Load a value
    ///
    /// See [`core::sync::atomic::AtomicUsize::load`]
    pub fn load(&self, ordering: Ordering) -> usize {
        self.0.load(ordering)
    }
}

#[cfg(any(doc, target_has_atomic = "32"))]
impl Value {
    /// Stores a value, returning the previous value
    ///
    /// See [`core::sync::atomic::AtomicUsize::swap`]
    pub fn swap(&self, value: usize, ordering: Ordering) -> usize {
        self.0.swap(value, ordering)
    }
}

#[cfg(not(any(doc, target_has_atomic = "32")))]
impl Value {
    /// Stores a value, returning the previous value
    ///
    /// See [`core::sync::atomic::AtomicUsize::swap`]
    pub fn swap(&self, value: usize, ordering: Ordering) -> usize {
        let (load_ordering, store_ordering) = match ordering {
            Ordering::Relaxed => (Ordering::Relaxed, Ordering::Relaxed),
            Ordering::Acquire => (Ordering::Acquire, Ordering::Relaxed),
            Ordering::Release => (Ordering::Relaxed, Ordering::Release),
            Ordering::AcqRel => (Ordering::Acquire, Ordering::Release),
            Ordering::SeqCst => (Ordering::SeqCst, Ordering::SeqCst),
            _ => panic!("Unsupported swap ordering"),
        };
        without_interrupts(|| {
            let out = self.0.load(load_ordering);
            self.0.store(value, store_ordering);
            out
        })
    }
}

/// A value which can be initalized only once
pub struct Once<T> {
    state: Value,
    value: UnsafeCell<Option<T>>,
}

const UNINIT: usize = 0;
const IN_PROGRESS: usize = 1;
const INIT: usize = 2;

unsafe impl<T: Send> Send for Once<T> {}
unsafe impl<T> Sync for Once<T> {}

impl<T> Once<T> {
    /// Create a new, uninitialized Once
    pub const fn new() -> Self {
        Self {
            state: Value::new(UNINIT),
            value: UnsafeCell::new(None),
        }
    }

    /// Initialize the stored value if needed, then return it.
    pub fn get_or_try_init<F>(&self, f: F) -> Option<&T>
    where
        F: FnOnce() -> Option<T>,
    {
        unsafe {
            match self.state.load(Ordering::Acquire) {
                IN_PROGRESS => panic!("Lock contention"),
                UNINIT => match self.state.swap(IN_PROGRESS, Ordering::Acquire) {
                    IN_PROGRESS => panic!("Lock contention"),
                    UNINIT => {
                        if let Some(value) = f() {
                            *self.value.get() = Some(value);
                            self.state.store(INIT, Ordering::Release);
                        } else {
                            self.state.store(UNINIT, Ordering::Release);
                        }
                    }
                    _ => {}
                },
                _ => {}
            }
            (&*self.value.get()).as_ref()
        }
    }
}

/// A simple lock
///
/// This lock does not disable interrupts, so attempting to use a
/// mutex in an interrupt context may deadlock. In interrupt contexts,
/// prefer a dedicated synchronization primitive based around
/// [`without_interrupts`]
pub struct Mutex<T> {
    lock: Flag,
    value: UnsafeCell<T>,
}

unsafe impl<T: Send> Send for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}

impl<T> Mutex<T> {
    /// Create a new Mutex
    pub const fn new(item: T) -> Self {
        Self {
            lock: Flag::new(false),
            value: UnsafeCell::new(item),
        }
    }

    /// Acquire this mutex
    pub fn lock(&self) -> MutexGuard<T> {
        while self.lock.swap(true, Ordering::AcqRel) {}
        MutexGuard(self)
    }

    unsafe fn unlock(&self) {
        self.lock.store(false, Ordering::Release)
    }
}

/// An RAII guard for a [`Mutex`]
pub struct MutexGuard<'a, T>(&'a Mutex<T>);

impl<'a, T> Deref for MutexGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { &*self.0.value.get() }
    }
}

impl<'a, T> DerefMut for MutexGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.0.value.get() }
    }
}

impl<'a, T> Drop for MutexGuard<'a, T> {
    fn drop(&mut self) {
        unsafe {
            self.0.unlock();
        }
    }
}

struct InterruptGate {
    count: UnsafeCell<usize>,
    enable: UnsafeCell<bool>,
}

unsafe impl Sync for InterruptGate {}

static INTERRUPTS: InterruptGate = InterruptGate::new();

impl InterruptGate {
    const fn new() -> Self {
        Self {
            count: UnsafeCell::new(0),
            enable: UnsafeCell::new(false),
        }
    }

    fn enable(&self) {
        unsafe {
            if !arch::interrupts_enabled() {
                *self.count.get() -= 1;
                if *self.count.get() == 0 && *self.enable.get() {
                    *self.enable.get() = false;
                    arch::enable_interrupts();
                }
            }
        }
    }

    fn disable(&self) {
        unsafe {
            if arch::interrupts_enabled() {
                arch::disable_interrupts();
                *self.enable.get() = true;
            }
            *self.count.get() += 1;
        }
    }

    fn flag_enable(&self) {
        unsafe {
            self.disable();
            *self.enable.get() = true;
            self.enable();
        }
    }
}

/// Runs the passed closure without interrupts, in a critical section.
///
/// This can be used as the underlying locking mechanism for
/// situations where [`Mutex`] is not appropriate.
pub fn without_interrupts<T, F>(f: F) -> T
where
    F: FnOnce() -> T,
{
    INTERRUPTS.disable();
    let out = f();
    INTERRUPTS.enable();
    out
}

/// Enable interrupts
///
/// Ensure that interrupts will be enabled once the outermost
/// [`without_interrupts`] call is completed.
pub fn enable_interrupts() {
    INTERRUPTS.flag_enable()
}

#[cfg(target_arch = "riscv32")]
mod arch {
    use bit_field::BitField;
    use core::sync::atomic::{compiler_fence, Ordering};

    pub fn interrupts_enabled() -> bool {
        unsafe {
            let mut mstatus: u32;
            asm!("csrr {0}, mstatus", out(reg) mstatus);
            mstatus.get_bit(3)
        }
    }

    pub fn disable_interrupts() {
        unsafe {
            let mut mstatus: u32;
            asm!("csrr {0}, mstatus", out(reg) mstatus);
            mstatus.set_bit(3, false);
            asm!("csrw mstatus, {0}", in(reg) mstatus);
        }
        compiler_fence(Ordering::Acquire);
    }

    pub unsafe fn enable_interrupts() {
        compiler_fence(Ordering::Release);
        let mut mstatus: u32;
        asm!("csrr {0}, mstatus", out(reg) mstatus);
        mstatus.set_bit(3, true);
        asm!("csrw mstatus, {0}", in(reg) mstatus);
    }
}

#[cfg(target_arch = "arm")]
mod arch {
    use core::sync::atomic::{compiler_fence, Ordering};

    #[inline]
    pub fn interrupts_enabled() -> bool {
        unsafe {
            let primask: u32;
            asm!("mrs {}, primask", out(reg) primask);
            primask & 1 == 0
        }
    }

    #[inline]
    pub unsafe fn disable_interrupts() {
        asm!(
            "cpsid i
             dmb
             dsb
             isb"
        );
        compiler_fence(Ordering::Acquire);
    }

    #[inline]
    pub unsafe fn enable_interrupts() {
        compiler_fence(Ordering::Release);
        asm!(
            "dmb
             dsb
             isb
             cpsie i"
        );
    }
}