-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
sync: new internal semaphore based on intrusive lists #2325
Merged
Merged
Changes from 45 commits
Commits
Show all changes
97 commits
Select commit
Hold shift + click to select a range
bfc4414
sync: adds Notify for basic task notification
carllerche 1890645
fix warnings
carllerche 5e82767
wip
hawkw ea8ba09
wip2
hawkw a888cb7
let's see just how broken it is
hawkw 9ae6fba
works but is horrible
hawkw ea499fe
wip
hawkw 3b4a14f
fix weird logic
hawkw 8836c38
fix close behavior
hawkw 4c81c51
Merge branch 'master' into eliza/semaphore-2
hawkw 5ee03b1
update to use new linked list
hawkw c4c685b
bring back old semaphore_ll so channels can work
hawkw 1337d8e
add loom tests
hawkw 19e0157
decr assigned permits when assigning to self
hawkw 2764ba2
closed means REALLY closed
hawkw d5ed440
broken
hawkw cb472fd
wip broken
hawkw 66e419b
Merge branch 'master' into eliza/semaphore-2
hawkw bd1c5db
update after merge
hawkw 1880121
almost evherything works
hawkw c7c08ff
wip
hawkw 1a4f733
ALL LOOM TESTS PASS
hawkw a320d56
fix typoed fail ordering
hawkw df1e7bb
remove some warnings
hawkw b957af8
bring back try_acquire
hawkw ec2b4e7
misc cleanup
hawkw 770d859
add test for release of leftover permits
hawkw 38f654f
simplify `poll_acquire` behavior
hawkw 9046b04
rm unneeded AtomicWaker
hawkw 187e4d7
remove unused imports
hawkw c2450a2
add test for cancelling an acquire future
hawkw ddc1726
dropping an incomplete `Acquire` releases permits
hawkw 1aa8088
no need to lock if unqueued
hawkw 88b5e86
remove dbg/println
hawkw cf27256
remove more dbg/printlns
hawkw cb73e3f
add comments/remove dbgs
hawkw ce66762
add comments/cleanup
hawkw 9f49f80
rm tests for stuff you can't do w new semaphore
hawkw fea9fd2
clean up & add comments
hawkw df6c556
rewrap comments, fix unused warnings
hawkw 201db12
more wrapping
hawkw 9f3f544
style/clean up
hawkw 8628723
revert unneeded MPSC changes
hawkw 49ed5dd
revert unneeded MPSC test changes
hawkw f5f5bc1
Apply suggestions from code review
hawkw 24479c5
remove printlns from tests
hawkw 6275513
checks to ensure API types remain Send/Sync/Unpin
hawkw f86147a
style: more consistent imports
hawkw 9206558
make docs more accurate
hawkw 0bcb271
Merge branch 'eliza/semaphore-2' of github.com:tokio-rs/tokio into el…
hawkw b412284
comments style improvements
hawkw 3b4f416
make LinkedList::is_linked more misuse-resistant
hawkw c2c3e04
fix feature flag unhappiness
hawkw c9e122f
quick rwlock benches
hawkw 034e141
quick semaphore benches
hawkw bffa18b
Merge branch 'eliza/bench-sync' into eliza/semaphore-2
hawkw 0e0e13d
add `LinkedList::split_back` method
hawkw 705afe1
add DoubleEndedIterator for linked_list::Iter~
hawkw c9e4800
(WIP) move notification outside of lock
hawkw 389e231
clean up
hawkw a18d569
fixup
hawkw f758fb6
rm unused
hawkw 8231eb7
Merge branch 'master' into eliza/semaphore-2
hawkw 90f98c9
put back coop yielding in acquire futures
hawkw 32a12bb
(hopefully) fix feature flags
hawkw 5c76e67
move waiter state inside of lock
hawkw 95bb327
remove dbg
hawkw c9699dc
cleanup
hawkw da1b027
remove unneeded SeqCst
hawkw 819a59f
notify outside of lock on close, too
hawkw 29fea22
carl-style feature flags
hawkw bdce157
ag
hawkw d9bf8b2
simplify permit struct significantly
hawkw 7406d54
simplify lock/cas loop interaction
hawkw 1f8afa6
simplify lock/cas loop interaction, episode II
hawkw 2d8d413
remove permit struct entirely
hawkw cc2afac
unweaken cas
hawkw 5573c65
fix backwards assertions in linked-list tests
hawkw e3c4b9f
rustfmt
hawkw 23257c1
fixup loom test
hawkw 4de0d58
LinkedList fmt::Debug impls shouldn't need T: Debug
hawkw f22a180
fix racy close behavior
hawkw 9c9354b
run rustfmt on file cargo fmt doesn't see
hawkw 2f1ead5
don't set unqueued when errored
hawkw 077a61f
bring back atomic permit counter
hawkw be77a72
simplify a few things
hawkw d20a93a
expect instead of unwrap
hawkw be8df9e
ensure node pointers don't dangle on panics
hawkw af8273f
cleanup/docs
hawkw 4bb9fb1
fix max permits, use low bits for flags
hawkw 67fe420
const-ify permit shift amount
hawkw 4d84ec7
check before reregistering waker/reduce unsafe scope
hawkw f1e3c4c
add overflow checks
hawkw a82c043
add comments explaining notification
hawkw 88fa1aa
fix backwards assertion
hawkw 0a01a59
Merge branch 'master' into eliza/semaphore-2
hawkw 449f8b1
document future causalcell improvement
hawkw 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,192 @@ | ||
use crate::sync::batch_semaphore::*; | ||
|
||
use futures::future::poll_fn; | ||
use loom::future::block_on; | ||
use loom::thread; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use loom::sync::atomic::AtomicUsize; | ||
use std::sync::atomic::Ordering::SeqCst; | ||
use std::sync::Arc; | ||
use std::task::Poll::Ready; | ||
use std::task::{Context, Poll}; | ||
|
||
#[test] | ||
fn basic_usage() { | ||
const NUM: usize = 2; | ||
|
||
struct Shared { | ||
semaphore: Semaphore, | ||
active: AtomicUsize, | ||
} | ||
|
||
async fn actor(shared: Arc<Shared>) { | ||
let mut permit = Permit::new(); | ||
permit.acquire(1, &shared.semaphore).await.unwrap(); | ||
println!("acquired!"); | ||
hawkw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let actual = shared.active.fetch_add(1, SeqCst); | ||
assert!(actual <= NUM - 1); | ||
|
||
let actual = shared.active.fetch_sub(1, SeqCst); | ||
assert!(actual <= NUM); | ||
|
||
permit.release(1, &shared.semaphore); | ||
} | ||
|
||
loom::model(|| { | ||
|
||
let shared = Arc::new(Shared { | ||
semaphore: Semaphore::new(NUM), | ||
active: AtomicUsize::new(0), | ||
}); | ||
|
||
for _ in 0..NUM { | ||
let shared = shared.clone(); | ||
|
||
thread::spawn(move || { | ||
block_on(actor(shared)); | ||
}); | ||
} | ||
|
||
block_on(actor(shared)); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn release() { | ||
loom::model(|| { | ||
let semaphore = Arc::new(Semaphore::new(1)); | ||
|
||
{ | ||
let semaphore = semaphore.clone(); | ||
thread::spawn(move || { | ||
let mut permit = Permit::new(); | ||
|
||
block_on(permit.acquire(1, &semaphore)).unwrap(); | ||
|
||
permit.release(1, &semaphore); | ||
}); | ||
} | ||
|
||
let mut permit = Permit::new(); | ||
|
||
block_on(permit.acquire(1, &semaphore)).unwrap(); | ||
|
||
permit.release(1, &semaphore); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn basic_closing() { | ||
const NUM: usize = 2; | ||
|
||
loom::model(|| { | ||
println!("-- iter --"); | ||
hawkw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let semaphore = Arc::new(Semaphore::new(1)); | ||
|
||
for _ in 0..NUM { | ||
let semaphore = semaphore.clone(); | ||
|
||
thread::spawn(move || { | ||
let mut permit = Permit::new(); | ||
|
||
for _ in 0..2 { | ||
block_on(permit.acquire(1, &semaphore)).map_err(|_|())?; | ||
|
||
permit.release(1, &semaphore); | ||
} | ||
|
||
Ok::<(), ()>(()) | ||
}); | ||
} | ||
|
||
semaphore.close(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn concurrent_close() { | ||
const NUM: usize = 3; | ||
|
||
loom::model(|| { | ||
let semaphore = Arc::new(Semaphore::new(1)); | ||
|
||
for _ in 0..NUM { | ||
let semaphore = semaphore.clone(); | ||
|
||
thread::spawn(move || { | ||
let mut permit = Permit::new(); | ||
|
||
block_on(permit.acquire(1, &semaphore)).map_err(|_|())?; | ||
|
||
permit.release(1, &semaphore); | ||
|
||
semaphore.close(); | ||
|
||
Ok::<(), ()>(()) | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
#[test] | ||
fn batch() { | ||
let mut b = loom::model::Builder::new(); | ||
b.preemption_bound = Some(1); | ||
|
||
b.check(|| { | ||
let semaphore = Arc::new(Semaphore::new(10)); | ||
let active = Arc::new(AtomicUsize::new(0)); | ||
let mut ths = vec![]; | ||
|
||
for _ in 0..2 { | ||
let semaphore = semaphore.clone(); | ||
let active = active.clone(); | ||
|
||
ths.push(thread::spawn(move || { | ||
let mut permit = Permit::new(); | ||
|
||
for n in &[4, 10, 8] { | ||
block_on(permit.acquire(*n, &semaphore)).unwrap(); | ||
|
||
active.fetch_add(*n as usize, SeqCst); | ||
|
||
let num_active = active.load(SeqCst); | ||
assert!(num_active <= 10); | ||
|
||
thread::yield_now(); | ||
|
||
active.fetch_sub(*n as usize, SeqCst); | ||
|
||
permit.release(*n, &semaphore); | ||
} | ||
})); | ||
} | ||
|
||
for th in ths.into_iter() { | ||
th.join().unwrap(); | ||
} | ||
|
||
assert_eq!(10, semaphore.available_permits()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn release_during_acquire() { | ||
loom::model(|| { | ||
let semaphore = Arc::new(Semaphore::new(10)); | ||
let mut permit1 = Permit::new(); | ||
permit1.try_acquire(8, &semaphore).expect("try_acquire should succeed; semaphore uncontended"); | ||
let semaphore2 = semaphore.clone(); | ||
let thread = thread::spawn(move || { | ||
let mut permit = Permit::new(); | ||
block_on(permit.acquire(4, &semaphore2)).unwrap(); | ||
permit | ||
}); | ||
|
||
permit1.release(8, &semaphore); | ||
let mut permit2 = thread.join().unwrap(); | ||
permit2.release(4, &semaphore); | ||
assert_eq!(10, semaphore.available_permits()); | ||
}) | ||
} |
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
Oops, something went wrong.
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'm usually against allowing
dead_code
, but in this case, I think it is a good choice. Reason: the entiresemaphore_ll.rs
file is planed to be removed.