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

Tune the Ordering for all the atomics #1707

Open
wants to merge 1 commit into
base: main
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
14 changes: 12 additions & 2 deletions polar-core/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ use crate::numerics::MOST_POSITIVE_EXACT_FLOAT;

const MAX_ID: u64 = (MOST_POSITIVE_EXACT_FLOAT - 1) as u64;

/// Note about memory ordering:
///
/// Here 'next' is just a global counter between threads and doesn't synchronize with other
/// variables. Therefore, `Relaxed` can be used in both single-threaded and multi-threaded
/// environments.
///
/// While atomic operations using 'Relaxed' memory ordering do not provide any happens-before
/// relationship, they do guarantee a total modification order of the 'next' atomic variable.
/// This means that all modifications of the 'next' atomic variable happen in an order that
/// is the same from the perspective of every single thread.
#[derive(Clone, Debug)]
pub struct Counter {
next: Arc<AtomicU64>,
Expand Down Expand Up @@ -34,12 +44,12 @@ impl Counter {
pub fn next(&self) -> u64 {
if self
.next
.compare_exchange(MAX_ID, 1, Ordering::SeqCst, Ordering::SeqCst)
.compare_exchange(MAX_ID, 1, Ordering::Relaxed, Ordering::Relaxed)
== Ok(MAX_ID)
{
MAX_ID
} else {
self.next.fetch_add(1, Ordering::SeqCst)
self.next.fetch_add(1, Ordering::Relaxed)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion polar-core/src/inverter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub struct Inverter {
_debug_id: u64,
}

/// Note about memory ordering:
///
/// Here 'ID' is just a global counter between threads and doesn't synchronize with
/// other variables. Therefore, `Relaxed` can be used in both single-threaded and
/// multi-threaded environments.
static ID: AtomicU64 = AtomicU64::new(0);

impl Inverter {
Expand All @@ -59,7 +64,7 @@ impl Inverter {
add_constraints,
results: vec![],
follower: None,
_debug_id: ID.fetch_add(1, Ordering::AcqRel),
_debug_id: ID.fetch_add(1, Ordering::Relaxed),
}
}
}
Expand Down