Page MenuHomeFreeBSD

Correct fences for sys/refcount.h
ClosedPublic

Authored by kib on Jun 19 2017, 9:52 AM.
Tags
None
Referenced Files
F106016100: D11270.diff
Mon, Dec 23, 11:08 PM
Unknown Object (File)
Mon, Dec 16, 6:30 AM
Unknown Object (File)
Nov 16 2024, 3:16 AM
Unknown Object (File)
Nov 12 2024, 5:36 PM
Unknown Object (File)
Oct 26 2024, 12:30 AM
Unknown Object (File)
Oct 20 2024, 6:00 PM
Unknown Object (File)
Oct 7 2024, 12:29 AM
Unknown Object (File)
Sep 23 2024, 3:01 AM
Subscribers

Details

Summary

The acq barrier in refcount_acquire() has no use, constructor must ensure that the changes are visible before publication by other means.

Last release must sync/with the constructor and all updaters.

This is quite subtle and based on the refcount/shared_ptr analysis I heard at the Hans Boehm/ Herb Sutter talks about C++ atomics.

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

This revision is now accepted and ready to land.Jun 19 2017, 2:24 PM
sys/sys/refcount.h
70

"destructing code" -> "destructor"

73

It's unclear what "there" refers to.

kib marked an inline comment as done.Jun 19 2017, 3:30 PM
kib added inline comments.
sys/sys/refcount.h
73

The claim is that the code for refcount_release() should look like this, using C11 operations:

int
refcount_release(volatile atomic_uint *count)
{
  int res;

   res = atomic_fetch_sub_explicit(count, 1, memory_order_acq_rel);
   return (res == 1);
}

The release operation in non-final refcount_release() calls which return false, sync/w the acquire operation on the final refcount_release() returning true. In other words, 'there' means the atomic_fetchadd_int op.

In fact, we can add atomic_fence_rel() before the atomic_fetchadd_int() line for the same effect.

kib edited edge metadata.

Provide the release fence in refcount_release(), instead of relying on user handling it correctly.

This revision now requires review to proceed.Jun 19 2017, 3:50 PM

Are the talk(s) you referenced the ones from cppcon?

edit: looks like this is covered in the last ~15 minutes of https://www.youtube.com/watch?v=M15UKpNlpeM

This revision is now accepted and ready to land.Jun 20 2017, 5:41 AM

Are the talk(s) you referenced the ones from cppcon?

edit: looks like this is covered in the last ~15 minutes of https://www.youtube.com/watch?v=M15UKpNlpeM

Yes, and Herb's talk from 2015.

Alan, are you fine with the latest variant of the patch ?

In D11270#235791, @kib wrote:

Alan, are you fine with the latest variant of the patch ?

I haven't had a chance to think about it in any depth. Give me until Friday.

alc added inline comments.
sys/sys/refcount.h
68

"destructor" is misspelled.

This revision was automatically updated to reflect the committed changes.