diff options
author | Sven Gothel <[email protected]> | 2021-01-07 09:08:03 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-01-07 09:08:03 +0100 |
commit | a2a21c4be8673f2c6ec603f37ce53fb5237f727d (patch) | |
tree | 8d1e68c7d7bdb92a1703ddab64571ab4b7249c12 /include/jau/cow_vector.hpp | |
parent | 95c6d559f66ea462046d4c1663b228461822341d (diff) |
cow_darray: Fail safe API: Only [c]begin() iterator retrieval, leave other operations on the cow_[ro|rw]_iterator avoding data races
These changes have been adopted to cow_vector as well, still deprecated now.
To allow data-race free operations using iterators from a potentially mutated CoW,
only one cow_darray::begin() const_iterator or iterator should be retrieved from this CoW
and all further operations shall use its
jau::cow_ro_iterator::size(), jau::cow_ro_iterator::begin() and jau::cow_ro_iterator::end()
- or its respective variant from jau::cow_rw_iterator.
Removed from cow_darray:
- 'const_iterator begin() const' // ambiguous mutable/immutable overload
- 'const_iterator cend()' // fetch from cow_ro_iterator::end()
- 'iterator end()' // fetch from cow_rw_iterator::end()
cow_[ro|rw]_iterator:
- private constructor w/ fried to cow_darray, .. only allow copy-ctor etc, not allowing to set false pointer
- Added size(), begin() and end(): Allowing data-race free operations
- full typedefs
- Holds the 'begin iterator' internally to implement begin() and end()
+++
Added new convenience template Type Traits and functions:
- 'template< class T > is_cow_type<T>::value' compile-time Type Trait,
determining whether the given template class is a CoW type, e.g. jau::cow_darray,
jau::cow_vector or any of their iterator.
For both, cow_type or any other using above 'is_cow_type<T>' trait:
- find_const(T& data)
- for_each_const(T& data)
- for_each_fidelity(T& data)
++++
Performance: No regression, even a little more speed-up.
Diffstat (limited to 'include/jau/cow_vector.hpp')
-rw-r--r-- | include/jau/cow_vector.hpp | 42 |
1 files changed, 15 insertions, 27 deletions
diff --git a/include/jau/cow_vector.hpp b/include/jau/cow_vector.hpp index 5223f82..365d776 100644 --- a/include/jau/cow_vector.hpp +++ b/include/jau/cow_vector.hpp @@ -117,23 +117,19 @@ namespace jau { typedef std::vector<value_type, allocator_type> storage_t; typedef std::shared_ptr<storage_t> storage_ref_t; + typedef cow_vector<value_type, allocator_type> cow_container_t; + /** - * Immutable, read-only const_iterator, lock-free, - * holding the current shared store reference until destruction. - * <p> - * Using jau::cow_vector::get_snapshot() at construction. - * </p> + * @see jau::cow_darray::const_iterator + * @see jau::cow_ro_iterator */ - typedef cow_ro_iterator<storage_t, storage_ref_t, cow_vector> const_iterator; + typedef cow_ro_iterator<storage_t, storage_ref_t, cow_container_t> const_iterator; /** - * Mutable, read-write iterator, holding the write-lock and a store copy until destruction. - * <p> - * Using jau::cow_vector::get_write_mutex(), jau::cow_vector::copy_store() at construction<br> - * and jau::cow_vector::set_store() at destruction. - * </p> + * @see jau::cow_darray::iterator + * @see jau::cow_rw_iterator */ - typedef cow_rw_iterator<storage_t, storage_ref_t, cow_vector> iterator; + typedef cow_rw_iterator<storage_t, storage_ref_t, cow_container_t> iterator; private: static constexpr size_type DIFF_MAX = std::numeric_limits<difference_type>::max(); @@ -278,32 +274,24 @@ namespace jau { // const_iterator, non mutable, read-only - constexpr const_iterator begin() const noexcept { - return const_iterator(get_snapshot(), store_ref->cbegin()); - } + // Removed for clarity: "constexpr const_iterator begin() const noexcept" + /** + * See description in jau::cow_darray::cbegin() + */ constexpr const_iterator cbegin() const noexcept { return const_iterator(get_snapshot(), store_ref->cbegin()); } - constexpr const_iterator end() const noexcept { - return const_iterator(get_snapshot(), store_ref->cend()); - } - - constexpr const_iterator cend() const noexcept { - return const_iterator(get_snapshot(), store_ref->cend()); - } - // iterator, mutable, read-write + /** + * See description in jau::cow_darray::begin() + */ constexpr iterator begin() noexcept { return iterator(*this, [](storage_ref_t& new_store) -> typename storage_t::iterator { return new_store->begin(); } ); } - constexpr iterator end() noexcept { - return iterator(*this, [](storage_ref_t& new_store) -> typename storage_t::iterator { return new_store->end(); } ); - } - // read access allocator_type get_allocator() const noexcept { |