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

Expose asymmetric_light_barrier and add a document #56

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

powergee
Copy link

It fixes #55.

Copy link
Owner

@jonhoo jonhoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there! Thanks for taking the time to write this up — this is some great documentation to have, and I agree with your rationale for making this function public!

Just one note about a nuance of the doc, and then I'm happy with it 👍

Comment on lines +251 to +254
/// To ensure safety, users need to appropriately synchronize the write operation on a hazard
/// slot and validate that the pointer hasn't already been retired. For synchronization, the
/// library offers an [`asymmetric_light_barrier`] function. It enables reclaiming threads
/// to acknowledge the preceding protection.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording here is a little imprecise for my taste. The current phrasing makes it seem like an acknowledgement is somehow "sent" via a call to asymmetric_light_barrier, but that's not really what happens. My understanding is that the barrier specifically has two side-effects:

  1. That the load_ptr on head happens strictly after the call to protect_raw
  2. That if another thread exchanges head, and that exchange is not observed by this thread, then the protect_raw must be visible to that other thread (since the exchange must have happened-after our load_ptr, which happened after our protect_raw).

I think it would be good to capture some of that nuance here and/or in the docs for asymmetric_light_barrier.

What do you think?

Copy link
Author

@powergee powergee Dec 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing, and I am sorry for the late reply. 😅

I agree with your point. The current explanation might mislead readers that asymmetric_light_barrier alone somehow prevents reclaimers from freeing the object, which is not true. I think that I have omitted too many details.
The relationship between barriers and validation should be described more clearly.

The main idea that must be expressed would be that asymmetric_light_barrier makes essential happens-before relations to hold. For example, if I model the entire system of Hazard pointers like the following...

procedure Protect(p):
  P1. Announce protection of p.
  P2. Issue a **light** barrier.
  P3. Check if p is not retired. If retired, must retry after reloading the pointer.

procedure Reclaim(p):
  R1. Announce retirement of p.
  R2. Issue a **heavy** barrier.
  R3. Check if p is protected. If protected, retry later.

The semantics of the light and heavy barrier are (-> is happens-before relation):

  1. If P2 -> R2, P1 -> R3 (the protection of P1 is visible to R3).
  2. If R2 -> P2, R1 -> P3 (the retirement of R1 is visible to P3).
  3. P2 ensures P1 -> P2 -> P3 and R2 ensures R1 -> R2 -> R3.

These three lemmas cover the two points you mentioned. With these lemmas, we can show that:

  1. If P2 happens before R2, the reclaimer will not free p.
  2. If R2 happens before P2, the accessor will not access p.

In either case, use-after-free errors do not occur.

So far, this is what I'm going to describe in a revised version. Please feel free to give comments if you want.

I think I'd be able to push a revised version this week. After the work, I would be happy if you review my PR again!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that looks really good, thanks for diving so deep into it!

@codecov-commenter
Copy link

Codecov Report

Merging #56 (7e85d86) into main (6c1842f) will increase coverage by 6.1%.
The diff coverage is 100.0%.

Additional details and impacted files
Files Coverage Δ
src/domain.rs 94.2% <100.0%> (+2.2%) ⬆️
src/hazard.rs 83.2% <ø> (+5.1%) ⬆️
src/lib.rs 78.1% <100.0%> (+18.3%) ⬆️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose asymmetric_light_barrier and explain its usage
3 participants