| 1 | use std::{ |
| 2 | cell::UnsafeCell, |
| 3 | ffi::c_void, |
| 4 | ptr::NonNull, |
| 5 | sync::{ |
| 6 | atomic::{AtomicU8, Ordering}, |
| 7 | Arc, |
| 8 | }, |
| 9 | task::{Context, Poll}, |
| 10 | }; |
| 11 | |
| 12 | use atomic_waker::AtomicWaker; |
| 13 | |
| 14 | use super::Completion; |
| 15 | |
| 16 | pub trait PlatformTransfer: Send { |
| 17 | /// Request cancellation of a transfer that may or may not currently be |
| 18 | /// pending. |
| 19 | fn cancel(&self); |
| 20 | } |
| 21 | |
| 22 | pub trait TransferRequest { |
| 23 | type Response; |
| 24 | } |
| 25 | |
| 26 | pub trait PlatformSubmit<D: TransferRequest>: PlatformTransfer { |
| 27 | /// Fill the transfer with the data from `data` and submit it to the kernel. |
| 28 | /// Arrange for `notify_completion(transfer)` to be called once the transfer |
| 29 | /// has completed. |
| 30 | /// |
| 31 | /// SAFETY(caller): transfer is in an idle state |
| 32 | unsafe fn submit(&mut self, data: D, transfer: *mut c_void); |
| 33 | |
| 34 | /// SAFETY(caller): `transfer` is in a completed state |
| 35 | unsafe fn take_completed(&mut self) -> Completion<D::Response>; |
| 36 | } |
| 37 | |
| 38 | struct TransferInner<P: PlatformTransfer> { |
| 39 | /// Platform-specific data. |
| 40 | /// |
| 41 | /// In an `UnsafeCell` because we provide `&mut` when the |
| 42 | /// state guarantees us exclusive access |
| 43 | platform_data: UnsafeCell<P>, |
| 44 | |
| 45 | /// One of the `STATE_*` constants below, used to synchronize |
| 46 | /// the state. |
| 47 | state: AtomicU8, |
| 48 | |
| 49 | /// Waker that is notified when transfer completes. |
| 50 | waker: Arc<AtomicWaker>, |
| 51 | } |
| 52 | |
| 53 | /// Handle to a transfer. |
| 54 | /// |
| 55 | /// Cancels the transfer and arranges for memory to be freed |
| 56 | /// when dropped. |
| 57 | pub(crate) struct TransferHandle<P: PlatformTransfer> { |
| 58 | ptr: NonNull<TransferInner<P>>, |
| 59 | } |
| 60 | |
| 61 | unsafe impl<P: PlatformTransfer> Send for TransferHandle<P> {} |
| 62 | unsafe impl<P: PlatformTransfer> Sync for TransferHandle<P> {} |
| 63 | |
| 64 | /// The transfer has not been submitted. The buffer is not valid. |
| 65 | const STATE_IDLE: u8 = 0; |
| 66 | |
| 67 | /// The transfer has been or is about to be submitted to the kernel and |
| 68 | /// completion has not yet been handled. The buffer points to valid memory but |
| 69 | /// cannot necessarily be accessed by userspace. There is a future or queue |
| 70 | /// waiting for it completion. |
| 71 | const STATE_PENDING: u8 = 1; |
| 72 | |
| 73 | /// Like PENDING, but there is no one waiting for completion. The completion |
| 74 | /// handler will drop the buffer and transfer. |
| 75 | const STATE_ABANDONED: u8 = 2; |
| 76 | |
| 77 | /// The transfer completion has been handled on the event loop thread. The |
| 78 | /// buffer is valid and may be accessed by the `TransferHandle`. |
| 79 | const STATE_COMPLETED: u8 = 3; |
| 80 | |
| 81 | impl<P: PlatformTransfer> TransferHandle<P> { |
| 82 | /// Create a new transfer and get a handle. |
| 83 | pub(crate) fn new(inner: P) -> TransferHandle<P> { |
| 84 | let b = Box::new(TransferInner { |
| 85 | platform_data: UnsafeCell::new(inner), |
| 86 | state: AtomicU8::new(STATE_IDLE), |
| 87 | waker: Arc::new(AtomicWaker::new()), |
| 88 | }); |
| 89 | |
| 90 | TransferHandle { |
| 91 | ptr: Box::leak(b).into(), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn inner(&self) -> &TransferInner<P> { |
| 96 | // SAFETY: while `TransferHandle` is alive, its `TransferInner` is alive |
| 97 | // (it may be shared by `notify_completion` on the event thread, so can't be &mut) |
| 98 | unsafe { self.ptr.as_ref() } |
| 99 | } |
| 100 | |
| 101 | fn platform_data(&self) -> &P { |
| 102 | // SAFETY: while `TransferHandle` is alive, the only mutable access to `platform_data` |
| 103 | // is via this `TransferHandle`. |
| 104 | unsafe { &*self.inner().platform_data.get() } |
| 105 | } |
| 106 | |
| 107 | pub(crate) fn submit<D>(&mut self, data: D) |
| 108 | where |
| 109 | D: TransferRequest, |
| 110 | P: PlatformSubmit<D>, |
| 111 | { |
| 112 | let inner = self.inner(); |
| 113 | |
| 114 | // It's the syscall that submits the transfer that actually performs the |
| 115 | // release ordering. |
| 116 | let prev = self.inner().state.swap(STATE_PENDING, Ordering::Relaxed); |
| 117 | assert_eq!(prev, STATE_IDLE, "Transfer should be idle when submitted"); |
| 118 | |
| 119 | // SAFETY: while `TransferHandle` is alive, the only mutable access to `platform_data` |
| 120 | // is via this `TransferHandle`. Verified that it is idle. |
| 121 | unsafe { |
| 122 | let p = &mut *inner.platform_data.get(); |
| 123 | p.submit(data, self.ptr.as_ptr() as *mut c_void); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub(crate) fn cancel(&mut self) { |
| 128 | self.platform_data().cancel(); |
| 129 | } |
| 130 | |
| 131 | fn poll_completion_generic(&mut self, cx: &Context) -> Poll<&mut P> { |
| 132 | let inner = self.inner(); |
| 133 | inner.waker.register(cx.waker()); |
| 134 | match inner.state.load(Ordering::Acquire) { |
| 135 | STATE_PENDING => Poll::Pending, |
| 136 | STATE_COMPLETED => { |
| 137 | // Relaxed because this doesn't synchronize with anything, |
| 138 | // just marks that we no longer need to drop the buffer |
| 139 | inner.state.store(STATE_IDLE, Ordering::Relaxed); |
| 140 | |
| 141 | // SAFETY: while `TransferHandle` is alive, the only mutable access to `platform_data` |
| 142 | // is via this `TransferHandle`. |
| 143 | Poll::Ready(unsafe { &mut *inner.platform_data.get() }) |
| 144 | } |
| 145 | s => panic!("Polling transfer in unexpected state {s}"), |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | pub fn poll_completion<D: TransferRequest>( |
| 150 | &mut self, |
| 151 | cx: &Context, |
| 152 | ) -> Poll<Completion<D::Response>> |
| 153 | where |
| 154 | D: TransferRequest, |
| 155 | P: PlatformSubmit<D>, |
| 156 | { |
| 157 | // SAFETY: `poll_completion_generic` checks that it is completed |
| 158 | self.poll_completion_generic(cx) |
| 159 | .map(|u| unsafe { u.take_completed() }) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | impl<P: PlatformTransfer> Drop for TransferHandle<P> { |
| 164 | fn drop(&mut self) { |
| 165 | match self.inner().state.swap(STATE_ABANDONED, Ordering::Acquire) { |
| 166 | STATE_PENDING => { |
| 167 | self.cancel(); |
| 168 | /* handler responsible for dropping */ |
| 169 | } |
| 170 | STATE_IDLE | STATE_COMPLETED => { |
| 171 | // SAFETY: state means there is no concurrent access |
| 172 | unsafe { drop(Box::from_raw(self.ptr.as_ptr())) } |
| 173 | } |
| 174 | s => panic!("Dropping transfer in unexpected state {s}"), |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// Notify that a transfer has completed. |
| 180 | /// |
| 181 | /// SAFETY: `transfer` must be a pointer previously passed to `submit`, and |
| 182 | /// the caller / kernel must no longer dereference it or its buffer. |
| 183 | pub(crate) unsafe fn notify_completion<P: PlatformTransfer>(transfer: *mut c_void) { |
| 184 | unsafe { |
| 185 | let transfer = transfer as *mut TransferInner<P>; |
| 186 | let waker = (*transfer).waker.clone(); |
| 187 | match (*transfer).state.swap(STATE_COMPLETED, Ordering::Release) { |
| 188 | STATE_PENDING => waker.wake(), |
| 189 | STATE_ABANDONED => { |
| 190 | drop(Box::from_raw(transfer)); |
| 191 | } |
| 192 | s => panic!("Completing transfer in unexpected state {s}"), |
| 193 | } |
| 194 | } |
| 195 | } |