-
Notifications
You must be signed in to change notification settings - Fork 471
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
Add select_biased! macro #1040
Merged
Merged
Add select_biased! macro #1040
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
364bda6
Add select_biased! macro
ryoqun 53a3d5d
Improve test a bit
ryoqun d96b7d4
Enforce biased selection even with timeout
ryoqun 1ba63bc
Enforce biased selection even without blocking
ryoqun 5d1d81a
Improve test so that assert_eq! would be triggered
ryoqun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ use std::ops::Deref; | |
use std::thread; | ||
use std::time::{Duration, Instant}; | ||
|
||
use crossbeam_channel::{after, bounded, never, select, tick, unbounded}; | ||
use crossbeam_channel::{after, bounded, never, select, select_biased, tick, unbounded}; | ||
use crossbeam_channel::{Receiver, RecvError, SendError, Sender, TryRecvError}; | ||
use crossbeam_utils::thread::scope; | ||
|
||
|
@@ -943,6 +943,121 @@ fn fairness_send() { | |
assert!(hits.iter().all(|x| *x >= COUNT / 4)); | ||
} | ||
|
||
#[test] | ||
fn unfairness() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i omitted more test cases like send one. let me know if i need to add more... |
||
#[cfg(miri)] | ||
const COUNT: usize = 100; | ||
#[cfg(not(miri))] | ||
const COUNT: usize = 10_000; | ||
|
||
let (s1, r1) = unbounded::<()>(); | ||
let (s2, r2) = unbounded::<()>(); | ||
let (s3, r3) = unbounded::<()>(); | ||
|
||
for _ in 0..COUNT { | ||
s1.send(()).unwrap(); | ||
s2.send(()).unwrap(); | ||
} | ||
s3.send(()).unwrap(); | ||
|
||
let mut hits = [0usize; 3]; | ||
for _ in 0..COUNT { | ||
select_biased! { | ||
recv(r1) -> _ => hits[0] += 1, | ||
recv(r2) -> _ => hits[1] += 1, | ||
recv(r3) -> _ => hits[2] += 1, | ||
} | ||
} | ||
assert_eq!(hits, [COUNT, 0, 0]); | ||
|
||
for _ in 0..COUNT { | ||
select_biased! { | ||
recv(r1) -> _ => hits[0] += 1, | ||
recv(r2) -> _ => hits[1] += 1, | ||
recv(r3) -> _ => hits[2] += 1, | ||
} | ||
} | ||
assert_eq!(hits, [COUNT, COUNT, 0]); | ||
} | ||
|
||
#[test] | ||
fn unfairness_timeout() { | ||
#[cfg(miri)] | ||
const COUNT: usize = 100; | ||
#[cfg(not(miri))] | ||
const COUNT: usize = 10_000; | ||
|
||
let (s1, r1) = unbounded::<()>(); | ||
let (s2, r2) = unbounded::<()>(); | ||
let (s3, r3) = unbounded::<()>(); | ||
|
||
for _ in 0..COUNT { | ||
s1.send(()).unwrap(); | ||
s2.send(()).unwrap(); | ||
} | ||
s3.send(()).unwrap(); | ||
|
||
let mut hits = [0usize; 3]; | ||
for _ in 0..COUNT { | ||
select_biased! { | ||
recv(r1) -> _ => hits[0] += 1, | ||
recv(r2) -> _ => hits[1] += 1, | ||
recv(r3) -> _ => hits[2] += 1, | ||
default(ms(1000)) => unreachable!(), | ||
} | ||
} | ||
assert_eq!(hits, [COUNT, 0, 0]); | ||
|
||
for _ in 0..COUNT { | ||
select_biased! { | ||
recv(r1) -> _ => hits[0] += 1, | ||
recv(r2) -> _ => hits[1] += 1, | ||
recv(r3) -> _ => hits[2] += 1, | ||
default(ms(1000)) => unreachable!(), | ||
} | ||
} | ||
assert_eq!(hits, [COUNT, COUNT, 0]); | ||
} | ||
|
||
#[test] | ||
fn unfairness_try() { | ||
#[cfg(miri)] | ||
const COUNT: usize = 100; | ||
#[cfg(not(miri))] | ||
const COUNT: usize = 10_000; | ||
|
||
let (s1, r1) = unbounded::<()>(); | ||
let (s2, r2) = unbounded::<()>(); | ||
let (s3, r3) = unbounded::<()>(); | ||
|
||
for _ in 0..COUNT { | ||
s1.send(()).unwrap(); | ||
s2.send(()).unwrap(); | ||
} | ||
s3.send(()).unwrap(); | ||
|
||
let mut hits = [0usize; 3]; | ||
for _ in 0..COUNT { | ||
select_biased! { | ||
recv(r1) -> _ => hits[0] += 1, | ||
recv(r2) -> _ => hits[1] += 1, | ||
recv(r3) -> _ => hits[2] += 1, | ||
default() => unreachable!(), | ||
} | ||
} | ||
assert_eq!(hits, [COUNT, 0, 0]); | ||
|
||
for _ in 0..COUNT { | ||
select_biased! { | ||
recv(r1) -> _ => hits[0] += 1, | ||
recv(r2) -> _ => hits[1] += 1, | ||
recv(r3) -> _ => hits[2] += 1, | ||
default() => unreachable!(), | ||
} | ||
} | ||
assert_eq!(hits, [COUNT, COUNT, 0]); | ||
} | ||
|
||
#[allow(clippy::or_fun_call, clippy::unnecessary_literal_unwrap)] // This is intentional. | ||
#[test] | ||
fn references() { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this was easiest way to implement... but this is accessible to user code. however, it seems we have precedent:
_LEN
.