Skip to content

fix(subscriber): changed atomic add to fetch update #629

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 27 additions & 17 deletions console-subscriber/src/callsites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,37 @@ impl<const MAX_CALLSITES: usize> Callsites<MAX_CALLSITES> {
pub(crate) fn insert(&self, callsite: &'static Metadata<'static>) {
// The callsite may already have been inserted, if the callsite cache
// was invalidated and is being rebuilt. In that case, don't insert it
// again.'
// again.
if self.contains(callsite) {
return;
}

let idx = self.len.fetch_add(1, Ordering::AcqRel);
if idx < MAX_CALLSITES {
// If there's still room in the callsites array, stick the address
// in there.
self.ptrs[idx]
.compare_exchange(
ptr::null_mut(),
callsite as *const _ as *mut _,
Ordering::AcqRel,
Ordering::Acquire,
)
.expect("a callsite would have been clobbered by `insert` (this is a bug)");
} else {
// Otherwise, we've filled the callsite array (sad!). Spill over
// into a hash set.
self.spill.write().insert(callsite.callsite());
match self
.len
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |current_len| {
if current_len < MAX_CALLSITES {
Some(current_len + 1)
} else {
None
}
}) {
Ok(idx) => {
// If there's still room in the callsites array, stick the address
// in there.
self.ptrs[idx]
.compare_exchange(
ptr::null_mut(),
callsite as *const _ as *mut _,
Ordering::AcqRel,
Ordering::Acquire,
)
.expect("a callsite would have been clobbered by `insert` (this is a bug)");
}
Err(_) => {
// Otherwise, we've filled the callsite array (sad!). Spill over
// into a hash set.
self.spill.write().insert(callsite.callsite());
}
}
}

Expand Down