diff --git a/crossbeam-channel/src/channel.rs b/crossbeam-channel/src/channel.rs index 5447e3303..8b67db324 100644 --- a/crossbeam-channel/src/channel.rs +++ b/crossbeam-channel/src/channel.rs @@ -1514,7 +1514,7 @@ pub(crate) unsafe fn write(s: &Sender, token: &mut Token, msg: T) -> Resul unsafe { match &s.flavor { SenderFlavor::Array(chan) => chan.write(token, msg), - SenderFlavor::List(chan) => chan.write(token, msg), + SenderFlavor::List(chan) => chan.write(&mut token.list, msg), SenderFlavor::Zero(chan) => chan.write(token, msg), } } @@ -1525,7 +1525,7 @@ pub(crate) unsafe fn read(r: &Receiver, token: &mut Token) -> Result chan.read(token), - ReceiverFlavor::List(chan) => chan.read(token), + ReceiverFlavor::List(chan) => chan.read(&mut token.list), ReceiverFlavor::Zero(chan) => chan.read(token), ReceiverFlavor::At(chan) => { mem::transmute_copy::, Result>(&chan.read(token)) diff --git a/crossbeam-channel/src/flavors/list.rs b/crossbeam-channel/src/flavors/list.rs index e0e1325fe..1f668edc2 100644 --- a/crossbeam-channel/src/flavors/list.rs +++ b/crossbeam-channel/src/flavors/list.rs @@ -203,7 +203,7 @@ impl Channel { } /// Attempts to reserve a slot for sending a message. - fn start_send(&self, token: &mut Token) { + fn start_send(&self, token: &mut ListToken) { let backoff = Backoff::new(); let mut next_block = ptr::null_mut::>(); @@ -215,7 +215,7 @@ impl Channel { // Check if the channel is disconnected. if tail & MARK_BIT != 0 { - token.list.block = ptr::null(); + token.block = ptr::null(); break; } @@ -266,8 +266,8 @@ impl Channel { (*block).next.store(n, Ordering::Release); } - token.list.block = block as *const u8; - token.list.offset = offset; + token.block = block as *const u8; + token.offset = offset; break; }, Err(_) => { @@ -282,15 +282,15 @@ impl Channel { } /// Writes a message into the channel. - pub(crate) unsafe fn write(&self, token: &mut Token, msg: T) -> Result<(), T> { + pub(crate) unsafe fn write(&self, token: &mut ListToken, msg: T) -> Result<(), T> { // If there is no slot, the channel is disconnected. - if token.list.block.is_null() { + if token.block.is_null() { return Err(msg); } // Write the message into the slot. - let block = token.list.block.cast::>(); - let offset = token.list.offset; + let block = token.block.cast::>(); + let offset = token.offset; let slot = unsafe { (*block).get_slot_unchecked(offset) }; unsafe { slot.msg.get().write(msg) } slot.state.fetch_or(WRITE, Ordering::Release); @@ -301,7 +301,7 @@ impl Channel { } /// Attempts to reserve a slot for receiving a message. - fn start_recv(&self, token: &mut Token) -> bool { + fn start_recv(&self, token: &mut ListToken) -> bool { let backoff = Backoff::new(); let mut head; let mut block; @@ -330,7 +330,7 @@ impl Channel { // If the channel is disconnected... if tail & MARK_BIT != 0 { // ...then receive an error. - token.list.block = ptr::null(); + token.block = ptr::null(); return true; } else { // Otherwise, the receive operation is not ready. @@ -371,8 +371,8 @@ impl Channel { self.head.index.store(next_index, Ordering::Release); } - token.list.block = block as *const u8; - token.list.offset = offset; + token.block = block as *const u8; + token.offset = offset; return true; }, Err(_) => { @@ -383,15 +383,15 @@ impl Channel { } /// Reads a message from the channel. - pub(crate) unsafe fn read(&self, token: &mut Token) -> Result { - if token.list.block.is_null() { + pub(crate) unsafe fn read(&self, token: &mut ListToken) -> Result { + if token.block.is_null() { // The channel is disconnected. return Err(()); } // Read the message. - let block = token.list.block as *mut Block; - let offset = token.list.offset; + let block = token.block as *mut Block; + let offset = token.offset; let slot = unsafe { (*block).get_slot_unchecked(offset) }; slot.wait_write(); let msg = unsafe { slot.msg.get().read() }; @@ -423,8 +423,7 @@ impl Channel { msg: T, _deadline: Option, ) -> Result<(), SendTimeoutError> { - let token = MaybeUninit::uninit(); - let token = &mut unsafe { token.assume_init() }; + let token = &mut ListToken::default(); self.start_send(token); unsafe { @@ -435,8 +434,7 @@ impl Channel { /// Attempts to receive a message without blocking. pub(crate) fn try_recv(&self) -> Result { - let token = MaybeUninit::uninit(); - let token = &mut unsafe { token.assume_init() }; + let token = &mut ListToken::default(); if self.start_recv(token) { unsafe { self.read(token).map_err(|_| TryRecvError::Disconnected) } @@ -447,8 +445,7 @@ impl Channel { /// Receives a message from the channel. pub(crate) fn recv(&self, deadline: Option) -> Result { - let token = MaybeUninit::uninit(); - let token = &mut unsafe { token.assume_init() }; + let token = &mut ListToken::default(); loop { // Try receiving a message several times. @@ -701,7 +698,7 @@ pub(crate) struct Sender<'a, T>(&'a Channel); impl SelectHandle for Receiver<'_, T> { fn try_select(&self, token: &mut Token) -> bool { - self.0.start_recv(token) + self.0.start_recv(&mut token.list) } fn deadline(&self) -> Option { @@ -737,7 +734,7 @@ impl SelectHandle for Receiver<'_, T> { impl SelectHandle for Sender<'_, T> { fn try_select(&self, token: &mut Token) -> bool { - self.0.start_send(token); + self.0.start_send(&mut token.list); true }