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
//! One-Shot Channels
//!
//! Often, clients of drivers only want to process one "in-flight" message at
//! a time. If request pipelining is not required, then a One-Shot Channel
//! is an easy way to perform an async/await request/response cycle.

use core::{
    cell::UnsafeCell,
    mem::MaybeUninit,
    sync::atomic::{AtomicU8, Ordering},
};

use maitake::sync::{Closed, WaitCell};
use mnemos_alloc::containers::Arc;

/// Not waiting for anything.
const ROSC_IDLE: u8 = 0;
/// A Sender has been created, but no writes have begun yet
const ROSC_WAITING: u8 = 1;
/// A Sender has begun writing, and will be dropped shortly.
const ROSC_WRITING: u8 = 2;
/// The Sender has been dropped and the message has been send
const ROSC_READY: u8 = 3;
/// Reading has already started
const ROSC_READING: u8 = 4;
/// The receiver has been manually closed or dropped.
const ROSC_CLOSED: u8 = 5;

/// A reusable One-Shot channel.
///
/// Essentially, a Reusable is a single producer, single consumer, channel, with a max
/// depth of one. Many producers can be created over the lifecycle of a single consumer,
/// however only zero or one producers can be live at any given time.
///
/// A `Reusable<T>` can be used to hand out single-use [Sender] items, which can
/// be used to make a single reply.
///
/// A given `Reusable<T>` can only ever have zero or one `Sender<T>`s live at any
/// given time, and a response can be received through a call to [Reusable::receive].
pub struct Reusable<T> {
    inner: Arc<Inner<T>>,
}

/// A single-use One-Shot channel sender
///
/// It can be consumed to send a response back to the [Reusable] instance that created
/// the [Sender].
pub struct Sender<T> {
    inner: Arc<Inner<T>>,
}

// An error type for the Reusable channel and Sender
#[derive(Debug, Eq, PartialEq)]
pub enum ReusableError {
    SenderAlreadyActive,
    NoSenderActive,
    ChannelClosed,
    InternalError,
}

impl From<Closed> for ReusableError {
    fn from(_: Closed) -> Self {
        ReusableError::ChannelClosed
    }
}

/// An inner type shared between the Rosc and Sender.
struct Inner<T> {
    state: AtomicU8,
    cell: UnsafeCell<MaybeUninit<T>>,
    wait: WaitCell,
}

// impl Reusable

impl<T> Reusable<T> {
    /// Create a new `Reusable<T>` using the heap from the given kernel
    pub async fn new_async() -> Self {
        Self {
            inner: Arc::new(Inner::new()).await,
        }
    }

    /// Create a sender for the given `Reusable<T>`. If a sender is already
    /// active, or the previous response has not yet been retrieved, an
    /// error will be immediately returned.
    ///
    /// This error can be cleared by awaiting [Reusable::receive].
    pub async fn sender(&self) -> Result<Sender<T>, ReusableError> {
        loop {
            let swap = self.inner.state.compare_exchange(
                ROSC_IDLE,
                ROSC_WAITING,
                Ordering::AcqRel,
                Ordering::Relaxed,
            );

            match swap {
                Ok(_) => {
                    return Ok(Sender {
                        inner: self.inner.clone(),
                    })
                }
                Err(val) => {
                    if val == ROSC_READY {
                        let _ = self.receive().await;
                    } else if (val == ROSC_WAITING) | (val == ROSC_WRITING) {
                        return Err(ReusableError::SenderAlreadyActive);
                    } else {
                        return Err(ReusableError::InternalError);
                    }
                }
            }
        }
    }

    /// Await the response from a created sender.
    ///
    /// If a sender has not been created, this function will immediately return
    /// an error.
    ///
    /// If the sender is dropped without sending a response, this function will
    /// return an error after the sender has been dropped.
    pub async fn receive(&self) -> Result<T, ReusableError> {
        loop {
            let wait = self.inner.wait.subscribe().await;
            let swap = self.inner.state.compare_exchange(
                ROSC_READY,
                ROSC_READING,
                Ordering::AcqRel,
                Ordering::Relaxed,
            );

            match swap {
                Ok(_) => {
                    // We just swapped from READY to READING, that's a success!
                    unsafe {
                        let mut ret = MaybeUninit::<T>::uninit();
                        core::ptr::copy_nonoverlapping(
                            self.inner.cell.get().cast(),
                            ret.as_mut_ptr(),
                            1,
                        );
                        self.inner.state.store(ROSC_IDLE, Ordering::Release);
                        return Ok(ret.assume_init());
                    }
                }
                Err(ROSC_WAITING | ROSC_WRITING) => {
                    // We are still waiting for the Sender to start or complete.
                    // Trigger another wait cycle.
                    //
                    // NOTE: it's impossible for the wait to fail here, as we only
                    // close the channel when dropping the Reusable, which can't be
                    // done while the borrow of self is active in this function.
                    wait.await?;
                }
                Err(ROSC_IDLE) => {
                    // We are currently idle, i.e. no sender has been created,
                    // or the existing one was dropped unused.
                    break Err(ReusableError::NoSenderActive);
                }
                Err(_) => {
                    // Something has gone terribly wrong. Return an error.
                    break Err(ReusableError::InternalError);
                }
            }
        }
    }

    /// Close the receiver. This will cause any pending senders to fail.
    pub fn close(self) {
        drop(self);
    }
}

impl<T> Drop for Reusable<T> {
    fn drop(&mut self) {
        // Immediately mark the state as closed
        let old = self.inner.state.swap(ROSC_CLOSED, Ordering::AcqRel);
        // Mark the waiter as closed (shouldn't be necessary - you can only create
        // a waiter from the Reusable type, which we are now dropping).
        self.inner.wait.close();

        // Determine if we need to drop the payload, if there is one.
        match old {
            ROSC_IDLE => {
                // Nothing to do, already idle, no contents
            }
            ROSC_WAITING => {
                // We are waiting for the sender, but it will fail to send.
                // Nothing to do.
            }
            ROSC_WRITING => {
                // We are cancelling mid-send. This will cause the sender
                // to fail, and IT is responsible for dropping the almost-
                // sent message.
            }
            ROSC_READY => {
                // We have received a message, but are dropping before reception.
                // We are responsible to drop the contents.
                unsafe {
                    let ptr: *mut MaybeUninit<T> = self.inner.cell.get();
                    let ptr: *mut T = ptr.cast();
                    core::ptr::drop_in_place(ptr);
                }
            }
            ROSC_READING => {
                // This SHOULD be impossible, as this is a transient state while
                // receiving, which shouldn't be possible if we are dropping the
                // receiver. Make this a debug assert to catch if this ever happens
                // during development or testing, otherwise do nothing.
                debug_assert!(false, "Dropped receiver while reading?");
            }
            ROSC_CLOSED => {
                // This SHOULD be impossible, as closing requires dropping the
                // receiver. Make this a debug assert to catch if this ever happens
                // during development or testing, otherwise do nothing.
                debug_assert!(false, "Receiver already closed while closing?");
            }
            _ => {}
        }
    }
}

// Impl Sender

impl<T> Sender<T> {
    /// Consume the sender, providing it with a reply.
    pub fn send(self, item: T) -> Result<(), ReusableError> {
        let swap = self.inner.state.compare_exchange(
            ROSC_WAITING,
            ROSC_WRITING,
            Ordering::AcqRel,
            Ordering::Relaxed,
        );

        match swap {
            Ok(_) => {}
            Err(ROSC_CLOSED) => return Err(ReusableError::ChannelClosed),
            Err(_) => return Err(ReusableError::InternalError),
        };

        unsafe { self.inner.cell.get().write(MaybeUninit::new(item)) };

        // Attempt to swap back to READY. This COULD fail if we just swapped to closed,
        // but in that case we won't override the CLOSED state, and it becomes OUR
        // responsibility to drop the contents.
        let swap = self.inner.state.compare_exchange(
            ROSC_WRITING,
            ROSC_READY,
            Ordering::AcqRel,
            Ordering::Relaxed,
        );

        match swap {
            Ok(_) => {}
            Err(ROSC_CLOSED) => {
                // Yup, a close happened WHILE we were writing. Go ahead and drop the contents
                unsafe {
                    let ptr: *mut MaybeUninit<T> = self.inner.cell.get();
                    let ptr: *mut T = ptr.cast();
                    core::ptr::drop_in_place(ptr);
                }
                return Err(ReusableError::ChannelClosed);
            }
            Err(_) => return Err(ReusableError::InternalError),
        }

        self.inner.wait.wake();
        Ok(())
    }
}

impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        // Attempt to move the state from WAITING to IDLE, and wake any
        // pending waiters. This will cause an Err(()) on the receive side.
        let _ = self.inner.state.compare_exchange(
            ROSC_WAITING,
            ROSC_IDLE,
            Ordering::AcqRel,
            Ordering::Relaxed,
        );
        self.inner.wait.wake();
    }
}

// impl Inner

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

// NOTE: A drop impl is not necessary, as the drop of the contents is handled
// by the Sender or Reusable.
impl<T> Inner<T> {
    fn new() -> Self {
        Self {
            state: AtomicU8::new(ROSC_IDLE),
            cell: UnsafeCell::new(MaybeUninit::uninit()),
            wait: WaitCell::new(),
        }
    }
}