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

core: additional deprecation fixes #1606

Merged
merged 3 commits into from
Oct 1, 2021
Merged
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
53 changes: 34 additions & 19 deletions tracing-core/src/spin/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,41 @@ impl<T> Once<T> {
let mut status = self.state.load(Ordering::SeqCst);

if status == INCOMPLETE {
status = self
.state
.compare_and_swap(INCOMPLETE, RUNNING, Ordering::SeqCst);
if status == INCOMPLETE {
// We init
// We use a guard (Finish) to catch panics caused by builder
let mut finish = Finish {
state: &self.state,
panicked: true,
};
unsafe { *self.data.get() = Some(builder()) };
finish.panicked = false;

status = COMPLETE;
self.state.store(status, Ordering::SeqCst);

// This next line is strictly an optimization
return self.force_get();
status = match self.state.compare_exchange(
INCOMPLETE,
RUNNING,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(status) => {
debug_assert_eq!(
status, INCOMPLETE,
"if compare_exchange succeeded, previous status must be incomplete",
);
// We init
// We use a guard (Finish) to catch panics caused by builder
let mut finish = Finish {
state: &self.state,
panicked: true,
};
unsafe { *self.data.get() = Some(builder()) };
finish.panicked = false;

self.state.store(COMPLETE, Ordering::SeqCst);

// This next line is strictly an optimization
return self.force_get();
}
Err(status) => status,
}
}

loop {
match status {
INCOMPLETE => unreachable!(),
RUNNING => {
// TODO(eliza): replace with `core::hint::spin_loop` once our MSRV supports it.
#[allow(deprecated)]
// We spin
cpu_relax();
status = self.state.load(Ordering::SeqCst)
Expand All @@ -126,7 +136,12 @@ impl<T> Once<T> {
loop {
match self.state.load(Ordering::SeqCst) {
INCOMPLETE => return None,
RUNNING => cpu_relax(), // We spin

RUNNING => {
// TODO(eliza): replace with `core::hint::spin_loop` once our MSRV supports it.
#[allow(deprecated)]
cpu_relax() // We spin
}
COMPLETE => return Some(self.force_get()),
PANICKED => panic!("Once has panicked"),
_ => unsafe { unreachable() },
Expand Down