Use thread-safe lock-free assignment in PackedVector::set_value#4199
Merged
Use thread-safe lock-free assignment in PackedVector::set_value#4199
Conversation
54630d5 to
19950b2
Compare
daniel-j-h
reviewed
Jun 27, 2017
| upper_offset[internal_index.element], | ||
| value); | ||
| // Lock-free update of the lower word | ||
| WordT local_lower_word, new_lower_word; |
Member
There was a problem hiding this comment.
WordT is a TBB typedef? Also is there a reason for not using the stdlib equivalents?
Contributor
Author
There was a problem hiding this comment.
WordT is an internal type alias. Please could you tell more about stdlib equivalents? So far i see only possible ways:
- use sequential update
- use an internal packed vector lock -> makes packed vector non-movable
- use boost.atomic -> requires new dependency
- use boost.interprocess atomics implementation -> outdated and only 32 bit version
- use glib atomic's -> requires new dependency
- use gcc __sync_bool_compare_and_swap and msvc _InterlockedCompareExchange64 -> possible, but requires proper testing
- wait for https://isocpp.org/blog/2014/05/n4013
as_atomic - use c11 _Atomic -> not possible
- use TBB atomic's -> uses TBB internal's
Member
There was a problem hiding this comment.
Urgh okay I see why you chose this solution now - thanks for the explanation! 🙇
Could you add this as a comment to the code please.
Contributor
Author
There was a problem hiding this comment.
@daniel-j-h i added a comment line about TBB atomics usage, but placed the options list in the git commit message
19950b2 to
04740dc
Compare
TheMarex
approved these changes
Jun 27, 2017
PR uses TBB internal atomic's for atomic CAS on non-atomic data Corresponding PR #4199 Other options: * use sequential update * use an internal packed vector lock -> makes packed vector non-movable * use boost.interprocess atomics implementation -> outdated and only 32 bit version * use glib atomic's -> requires new dependency * wait for https://isocpp.org/blog/2014/05/n4013 as_atomic * use c11 _Atomic and atomic_compare_exchange_weak -> not possible to mix c++11 and c11 * use builtin functions gcc __sync_bool_compare_and_swap and msvc _InterlockedCompareExchange64 -> possible, but requires proper testing boolean CompareAndSwapPointer(volatile * void * ptr, void * new_value, void * old_value) { if defined(_MSC_VER) if (InterlockedCompareExchange(ptr, new_value, old_value) == old_value) return false; else return true; elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100 return __sync_bool_compare_and_swap(ptr, old_value, new_value); else error No implementation endif } * use Boost.Atomic -> requires new dependency WordT local_lower_word = lower_word, new_lower_word; do { new_lower_word = set_lower_value<WordT, T>(local_lower_word, lower_mask[internal_index.element], lower_offset[internal_index.element], value); } while (!boost::atomics::detail::operations<sizeof(WordT), false>::compare_exchange_weak( lower_word, local_lower_word, new_lower_word, boost::memory_order_release, boost::memory_order_relaxed));
04740dc to
62a4556
Compare
oxidase
added a commit
that referenced
this pull request
Jun 27, 2017
PR uses TBB internal atomic's for atomic CAS on non-atomic data Corresponding PR #4199 Other options: * use sequential update * use an internal packed vector lock -> makes packed vector non-movable * use boost.interprocess atomics implementation -> outdated and only 32 bit version * use glib atomic's -> requires new dependency * wait for https://isocpp.org/blog/2014/05/n4013 as_atomic * use c11 _Atomic and atomic_compare_exchange_weak -> not possible to mix c++11 and c11 * use builtin functions gcc __sync_bool_compare_and_swap and msvc _InterlockedCompareExchange64 -> possible, but requires proper testing boolean CompareAndSwapPointer(volatile * void * ptr, void * new_value, void * old_value) { if defined(_MSC_VER) if (InterlockedCompareExchange(ptr, new_value, old_value) == old_value) return false; else return true; elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100 return __sync_bool_compare_and_swap(ptr, old_value, new_value); else error No implementation endif } * use Boost.Atomic -> requires new dependency WordT local_lower_word = lower_word, new_lower_word; do { new_lower_word = set_lower_value<WordT, T>(local_lower_word, lower_mask[internal_index.element], lower_offset[internal_index.element], value); } while (!boost::atomics::detail::operations<sizeof(WordT), false>::compare_exchange_weak( lower_word, local_lower_word, new_lower_word, boost::memory_order_release, boost::memory_order_relaxed));
oxidase
added a commit
that referenced
this pull request
Jun 27, 2017
PR uses TBB internal atomic's for atomic CAS on non-atomic data Corresponding PR #4199 Other options: * use sequential update * use an internal packed vector lock -> makes packed vector non-movable * use boost.interprocess atomics implementation -> outdated and only 32 bit version * use glib atomic's -> requires new dependency * wait for https://isocpp.org/blog/2014/05/n4013 as_atomic * use c11 _Atomic and atomic_compare_exchange_weak -> not possible to mix c++11 and c11 * use builtin functions gcc __sync_bool_compare_and_swap and msvc _InterlockedCompareExchange64 -> possible, but requires proper testing boolean CompareAndSwapPointer(volatile * void * ptr, void * new_value, void * old_value) { if defined(_MSC_VER) if (InterlockedCompareExchange(ptr, new_value, old_value) == old_value) return false; else return true; elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100 return __sync_bool_compare_and_swap(ptr, old_value, new_value); else error No implementation endif } * use Boost.Atomic -> requires new dependency WordT local_lower_word = lower_word, new_lower_word; do { new_lower_word = set_lower_value<WordT, T>(local_lower_word, lower_mask[internal_index.element], lower_offset[internal_index.element], value); } while (!boost::atomics::detail::operations<sizeof(WordT), false>::compare_exchange_weak( lower_word, local_lower_word, new_lower_word, boost::memory_order_release, boost::memory_order_relaxed));
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
PR fixes #4110 and #4065 that is caused by data races in
PackedVector::set_valueLock-free
compare_exchange_weakis implemented in <boost/atomic/detail/operations.hpp> as it is not possible to useatomic_compare_exchange_weakfrom <stdatomic.h> within C++EDIT: the current approach requires to add Boost.Atomics as a new dependency, so other approaches that do not introduce a global vector lock are welcome
Tasklist