Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to Parking in Bounded and Unbounded Channels #1105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions crossbeam-channel/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::err::{
};
use crate::flavors;
use crate::select::{Operation, SelectHandle, Token};
use crate::waker::BlockingState;

/// Creates a multi-producer multi-consumer channel of unbounded capacity.
///
Expand Down Expand Up @@ -1358,6 +1359,14 @@ impl<T> fmt::Debug for IntoIter<T> {
}

impl<T> SelectHandle for Sender<T> {
fn start(&self) -> Option<BlockingState<'_>> {
match &self.flavor {
SenderFlavor::Array(chan) => chan.sender().start_ref(),
SenderFlavor::List(chan) => chan.sender().start_ref(),
SenderFlavor::Zero(chan) => chan.start(),
}
}

fn try_select(&self, token: &mut Token) -> bool {
match &self.flavor {
SenderFlavor::Array(chan) => chan.sender().try_select(token),
Expand All @@ -1370,11 +1379,11 @@ impl<T> SelectHandle for Sender<T> {
None
}

fn register(&self, oper: Operation, cx: &Context) -> bool {
fn register(&self, oper: Operation, cx: &Context, state: Option<&BlockingState<'_>>) -> bool {
match &self.flavor {
SenderFlavor::Array(chan) => chan.sender().register(oper, cx),
SenderFlavor::List(chan) => chan.sender().register(oper, cx),
SenderFlavor::Zero(chan) => chan.sender().register(oper, cx),
SenderFlavor::Array(chan) => chan.sender().register(oper, cx, state),
SenderFlavor::List(chan) => chan.sender().register(oper, cx, state),
SenderFlavor::Zero(chan) => chan.sender().register(oper, cx, state),
}
}

Expand Down Expand Up @@ -1402,11 +1411,11 @@ impl<T> SelectHandle for Sender<T> {
}
}

fn watch(&self, oper: Operation, cx: &Context) -> bool {
fn watch(&self, oper: Operation, cx: &Context, state: Option<&BlockingState<'_>>) -> bool {
match &self.flavor {
SenderFlavor::Array(chan) => chan.sender().watch(oper, cx),
SenderFlavor::List(chan) => chan.sender().watch(oper, cx),
SenderFlavor::Zero(chan) => chan.sender().watch(oper, cx),
SenderFlavor::Array(chan) => chan.sender().watch(oper, cx, state),
SenderFlavor::List(chan) => chan.sender().watch(oper, cx, state),
SenderFlavor::Zero(chan) => chan.sender().watch(oper, cx, state),
}
}

Expand All @@ -1420,6 +1429,17 @@ impl<T> SelectHandle for Sender<T> {
}

impl<T> SelectHandle for Receiver<T> {
fn start(&self) -> Option<BlockingState<'_>> {
match &self.flavor {
ReceiverFlavor::Array(chan) => chan.receiver().start_ref(),
ReceiverFlavor::List(chan) => chan.receiver().start_ref(),
ReceiverFlavor::Zero(chan) => chan.start(),
ReceiverFlavor::At(chan) => chan.start(),
ReceiverFlavor::Tick(chan) => chan.start(),
ReceiverFlavor::Never(chan) => chan.start(),
}
}

fn try_select(&self, token: &mut Token) -> bool {
match &self.flavor {
ReceiverFlavor::Array(chan) => chan.receiver().try_select(token),
Expand All @@ -1442,14 +1462,14 @@ impl<T> SelectHandle for Receiver<T> {
}
}

fn register(&self, oper: Operation, cx: &Context) -> bool {
fn register(&self, oper: Operation, cx: &Context, state: Option<&BlockingState<'_>>) -> bool {
match &self.flavor {
ReceiverFlavor::Array(chan) => chan.receiver().register(oper, cx),
ReceiverFlavor::List(chan) => chan.receiver().register(oper, cx),
ReceiverFlavor::Zero(chan) => chan.receiver().register(oper, cx),
ReceiverFlavor::At(chan) => chan.register(oper, cx),
ReceiverFlavor::Tick(chan) => chan.register(oper, cx),
ReceiverFlavor::Never(chan) => chan.register(oper, cx),
ReceiverFlavor::Array(chan) => chan.receiver().register(oper, cx, state),
ReceiverFlavor::List(chan) => chan.receiver().register(oper, cx, state),
ReceiverFlavor::Zero(chan) => chan.receiver().register(oper, cx, state),
ReceiverFlavor::At(chan) => chan.register(oper, cx, state),
ReceiverFlavor::Tick(chan) => chan.register(oper, cx, state),
ReceiverFlavor::Never(chan) => chan.register(oper, cx, state),
}
}

Expand Down Expand Up @@ -1486,14 +1506,14 @@ impl<T> SelectHandle for Receiver<T> {
}
}

fn watch(&self, oper: Operation, cx: &Context) -> bool {
fn watch(&self, oper: Operation, cx: &Context, state: Option<&BlockingState<'_>>) -> bool {
match &self.flavor {
ReceiverFlavor::Array(chan) => chan.receiver().watch(oper, cx),
ReceiverFlavor::List(chan) => chan.receiver().watch(oper, cx),
ReceiverFlavor::Zero(chan) => chan.receiver().watch(oper, cx),
ReceiverFlavor::At(chan) => chan.watch(oper, cx),
ReceiverFlavor::Tick(chan) => chan.watch(oper, cx),
ReceiverFlavor::Never(chan) => chan.watch(oper, cx),
ReceiverFlavor::Array(chan) => chan.receiver().watch(oper, cx, state),
ReceiverFlavor::List(chan) => chan.receiver().watch(oper, cx, state),
ReceiverFlavor::Zero(chan) => chan.receiver().watch(oper, cx, state),
ReceiverFlavor::At(chan) => chan.watch(oper, cx, state),
ReceiverFlavor::Tick(chan) => chan.watch(oper, cx, state),
ReceiverFlavor::Never(chan) => chan.watch(oper, cx, state),
}
}

Expand Down
Loading