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
//! A Mutex-like FIFO with unlimited-waiter for embedded systems.
//!
//! Example usage:
//!
//! ```rust
//! # async fn select<F1, F2>(f1: F1, f2: F2) {}
//! use rtic_sync::arbiter::Arbiter;
//!
//! // Instantiate an Arbiter with a static lifetime.
//! static ARBITER: Arbiter<u32> = Arbiter::new(32);
//!
//! async fn run(){
//!     let write_42 = async move {
//!         *ARBITER.access().await = 42;
//!     };
//!
//!     let write_1337 = async move {
//!         *ARBITER.access().await = 1337;
//!     };
//!
//!     // Attempt to access the Arbiter concurrently.
//!     select(write_42, write_1337).await;
//! }
//! ```

use core::cell::UnsafeCell;
use core::future::poll_fn;
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
use core::task::{Poll, Waker};
use portable_atomic::{fence, AtomicBool, Ordering};

use rtic_common::dropper::OnDrop;
use rtic_common::wait_queue::{Link, WaitQueue};

/// This is needed to make the async closure in `send` accept that we "share"
/// the link possible between threads.
#[derive(Clone)]
struct LinkPtr(*mut Option<Link<Waker>>);

impl LinkPtr {
    /// This will dereference the pointer stored within and give out an `&mut`.
    unsafe fn get(&mut self) -> &mut Option<Link<Waker>> {
        &mut *self.0
    }
}

unsafe impl Send for LinkPtr {}
unsafe impl Sync for LinkPtr {}

/// An FIFO waitqueue for use in shared bus usecases.
pub struct Arbiter<T> {
    wait_queue: WaitQueue,
    inner: UnsafeCell<T>,
    taken: AtomicBool,
}

unsafe impl<T> Send for Arbiter<T> {}
unsafe impl<T> Sync for Arbiter<T> {}

impl<T> Arbiter<T> {
    /// Create a new arbiter.
    pub const fn new(inner: T) -> Self {
        Self {
            wait_queue: WaitQueue::new(),
            inner: UnsafeCell::new(inner),
            taken: AtomicBool::new(false),
        }
    }

    /// Get access to the inner value in the [`Arbiter`]. This will wait until access is granted,
    /// for non-blocking access use `try_access`.
    pub async fn access(&self) -> ExclusiveAccess<'_, T> {
        let mut link_ptr: Option<Link<Waker>> = None;

        // Make this future `Drop`-safe.
        // SAFETY(link_ptr): Shadow the original definition of `link_ptr` so we can't abuse it.
        let mut link_ptr = LinkPtr(&mut link_ptr as *mut Option<Link<Waker>>);

        let mut link_ptr2 = link_ptr.clone();
        let dropper = OnDrop::new(|| {
            // SAFETY: We only run this closure and dereference the pointer if we have
            // exited the `poll_fn` below in the `drop(dropper)` call. The other dereference
            // of this pointer is in the `poll_fn`.
            if let Some(link) = unsafe { link_ptr2.get() } {
                link.remove_from_list(&self.wait_queue);
            }
        });

        poll_fn(|cx| {
            critical_section::with(|_| {
                fence(Ordering::SeqCst);

                // The queue is empty and noone has taken the value.
                if self.wait_queue.is_empty() && !self.taken.load(Ordering::Relaxed) {
                    self.taken.store(true, Ordering::Relaxed);

                    return Poll::Ready(());
                }

                // SAFETY: This pointer is only dereferenced here and on drop of the future
                // which happens outside this `poll_fn`'s stack frame.
                let link = unsafe { link_ptr.get() };
                if let Some(link) = link {
                    if link.is_popped() {
                        return Poll::Ready(());
                    }
                } else {
                    // Place the link in the wait queue on first run.
                    let link_ref = link.insert(Link::new(cx.waker().clone()));

                    // SAFETY(new_unchecked): The address to the link is stable as it is defined
                    // outside this stack frame.
                    // SAFETY(push): `link_ref` lifetime comes from `link_ptr` that is shadowed,
                    // and  we make sure in `dropper` that the link is removed from the queue
                    // before dropping `link_ptr` AND `dropper` makes sure that the shadowed
                    // `link_ptr` lives until the end of the stack frame.
                    unsafe { self.wait_queue.push(Pin::new_unchecked(link_ref)) };
                }

                Poll::Pending
            })
        })
        .await;

        // Make sure the link is removed from the queue.
        drop(dropper);

        // SAFETY: One only gets here if there is exlusive access.
        ExclusiveAccess {
            arbiter: self,
            inner: unsafe { &mut *self.inner.get() },
        }
    }

    /// Non-blockingly tries to access the underlying value.
    /// If someone is in queue to get it, this will return `None`.
    pub fn try_access(&self) -> Option<ExclusiveAccess<'_, T>> {
        critical_section::with(|_| {
            fence(Ordering::SeqCst);

            // The queue is empty and noone has taken the value.
            if self.wait_queue.is_empty() && !self.taken.load(Ordering::Relaxed) {
                self.taken.store(true, Ordering::Relaxed);

                // SAFETY: One only gets here if there is exlusive access.
                Some(ExclusiveAccess {
                    arbiter: self,
                    inner: unsafe { &mut *self.inner.get() },
                })
            } else {
                None
            }
        })
    }
}

/// This token represents exclusive access to the value protected by the [`Arbiter`].
pub struct ExclusiveAccess<'a, T> {
    arbiter: &'a Arbiter<T>,
    inner: &'a mut T,
}

impl<'a, T> Drop for ExclusiveAccess<'a, T> {
    fn drop(&mut self) {
        critical_section::with(|_| {
            fence(Ordering::SeqCst);

            if self.arbiter.wait_queue.is_empty() {
                // If noone is in queue and we release exclusive access, reset `taken`.
                self.arbiter.taken.store(false, Ordering::Relaxed);
            } else if let Some(next) = self.arbiter.wait_queue.pop() {
                // Wake the next one in queue.
                next.wake();
            }
        })
    }
}

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

    fn deref(&self) -> &Self::Target {
        self.inner
    }
}

impl<'a, T> DerefMut for ExclusiveAccess<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner
    }
}

/// SPI bus sharing using [`Arbiter`]
pub mod spi {
    use super::Arbiter;
    use embedded_hal::digital::OutputPin;
    use embedded_hal_async::{
        delay::DelayNs,
        spi::{ErrorType, Operation, SpiBus, SpiDevice},
    };
    use embedded_hal_bus::spi::DeviceError;

    /// [`Arbiter`]-based shared bus implementation.
    pub struct ArbiterDevice<'a, BUS, CS, D> {
        bus: &'a Arbiter<BUS>,
        cs: CS,
        delay: D,
    }

    impl<'a, BUS, CS, D> ArbiterDevice<'a, BUS, CS, D> {
        /// Create a new [`ArbiterDevice`].
        pub fn new(bus: &'a Arbiter<BUS>, cs: CS, delay: D) -> Self {
            Self { bus, cs, delay }
        }
    }

    impl<'a, BUS, CS, D> ErrorType for ArbiterDevice<'a, BUS, CS, D>
    where
        BUS: ErrorType,
        CS: OutputPin,
    {
        type Error = DeviceError<BUS::Error, CS::Error>;
    }

    impl<'a, Word, BUS, CS, D> SpiDevice<Word> for ArbiterDevice<'a, BUS, CS, D>
    where
        Word: Copy + 'static,
        BUS: SpiBus<Word>,
        CS: OutputPin,
        D: DelayNs,
    {
        async fn transaction(
            &mut self,
            operations: &mut [Operation<'_, Word>],
        ) -> Result<(), DeviceError<BUS::Error, CS::Error>> {
            let mut bus = self.bus.access().await;

            self.cs.set_low().map_err(DeviceError::Cs)?;

            let op_res = 'ops: {
                for op in operations {
                    let res = match op {
                        Operation::Read(buf) => bus.read(buf).await,
                        Operation::Write(buf) => bus.write(buf).await,
                        Operation::Transfer(read, write) => bus.transfer(read, write).await,
                        Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await,
                        Operation::DelayNs(ns) => match bus.flush().await {
                            Err(e) => Err(e),
                            Ok(()) => {
                                self.delay.delay_ns(*ns).await;
                                Ok(())
                            }
                        },
                    };
                    if let Err(e) = res {
                        break 'ops Err(e);
                    }
                }
                Ok(())
            };

            // On failure, it's important to still flush and deassert CS.
            let flush_res = bus.flush().await;
            let cs_res = self.cs.set_high();

            op_res.map_err(DeviceError::Spi)?;
            flush_res.map_err(DeviceError::Spi)?;
            cs_res.map_err(DeviceError::Cs)?;

            Ok(())
        }
    }
}

/// I2C bus sharing using [`Arbiter`]
///
/// An Example how to use it in RTIC application:
/// ```text
/// #[app(device = some_hal, peripherals = true, dispatchers = [TIM16])]
/// mod app {
///     use core::mem::MaybeUninit;
///     use rtic_sync::{arbiter::{i2c::ArbiterDevice, Arbiter},
///
///     #[shared]
///     struct Shared {}
///
///     #[local]
///     struct Local {
///         ens160: Ens160<ArbiterDevice<'static, I2c<'static, I2C1>>>,
///     }
///
///     #[init(local = [
///         i2c_arbiter: MaybeUninit<Arbiter<I2c<'static, I2C1>>> = MaybeUninit::uninit(),
///     ])]
///     fn init(cx: init::Context) -> (Shared, Local) {
///         let i2c = I2c::new(cx.device.I2C1);
///         let i2c_arbiter = cx.local.i2c_arbiter.write(Arbiter::new(i2c));
///         let ens160 = Ens160::new(ArbiterDevice::new(i2c_arbiter), 0x52);
///
///         i2c_sensors::spawn(i2c_arbiter).ok();
///
///         (Shared {}, Local { ens160 })
///     }
///
///     #[task(local = [ens160])]
///     async fn i2c_sensors(cx: i2c_sensors::Context, i2c: &'static Arbiter<I2c<'static, I2C1>>) {
///         use sensor::Asensor;
///
///         loop {
///             // Use scope to make sure I2C access is dropped.
///             {
///                 // Read from sensor driver that wants to use I2C directly.
///                 let mut i2c = i2c.access().await;
///                 let status = Asensor::status(&mut i2c).await;
///             }
///
///             // Read ENS160 sensor.
///             let eco2 = cx.local.ens160.eco2().await;
///         }
///     }
/// }
/// ```
pub mod i2c {
    use super::Arbiter;
    use embedded_hal::i2c::{AddressMode, ErrorType, Operation};
    use embedded_hal_async::i2c::I2c;

    /// [`Arbiter`]-based shared bus implementation for I2C.
    pub struct ArbiterDevice<'a, BUS> {
        bus: &'a Arbiter<BUS>,
    }

    impl<'a, BUS> ArbiterDevice<'a, BUS> {
        /// Create a new [`ArbiterDevice`] for I2C.
        pub fn new(bus: &'a Arbiter<BUS>) -> Self {
            Self { bus }
        }
    }

    impl<'a, BUS> ErrorType for ArbiterDevice<'a, BUS>
    where
        BUS: ErrorType,
    {
        type Error = BUS::Error;
    }

    impl<'a, BUS, A> I2c<A> for ArbiterDevice<'a, BUS>
    where
        BUS: I2c<A>,
        A: AddressMode,
    {
        async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
            let mut bus = self.bus.access().await;
            bus.read(address, read).await
        }

        async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
            let mut bus = self.bus.access().await;
            bus.write(address, write).await
        }

        async fn write_read(
            &mut self,
            address: A,
            write: &[u8],
            read: &mut [u8],
        ) -> Result<(), Self::Error> {
            let mut bus = self.bus.access().await;
            bus.write_read(address, write, read).await
        }

        async fn transaction(
            &mut self,
            address: A,
            operations: &mut [Operation<'_>],
        ) -> Result<(), Self::Error> {
            let mut bus = self.bus.access().await;
            bus.transaction(address, operations).await
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn stress_channel() {
        const NUM_RUNS: usize = 100_000;

        static ARB: Arbiter<usize> = Arbiter::new(0);
        let mut v = std::vec::Vec::new();

        for _ in 0..NUM_RUNS {
            v.push(tokio::spawn(async move {
                *ARB.access().await += 1;
            }));
        }

        for v in v {
            v.await.unwrap();
        }

        assert_eq!(*ARB.access().await, NUM_RUNS)
    }
}