-//$$CDS-header$$
+/*
+ This file is a part of libcds - Concurrent Data Structures library
+
+ (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+
+ Source code repo: http://github.com/khizmax/libcds/
+ Download: http://sourceforge.net/projects/libcds/files/
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
#ifndef CDSLIB_CONTAINER_MICHAEL_MAP_RCU_H
#define CDSLIB_CONTAINER_MICHAEL_MAP_RCU_H
return m_Buckets[ hash_value( key ) ];
}
//@endcond
- protected:
- /// Forward iterator
- /**
- \p IsConst - constness boolean flag
-
- The forward iterator for Michael's map is based on \p OrderedList forward iterator and has the following features:
- - it has no post-increment operator, only pre-increment
- - it iterates items in unordered fashion
- - The iterator cannot be moved across thread boundary since it may contain GC's guard that is thread-private GC data.
- - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
- deleting operations it is no guarantee that you iterate all item in the map.
- Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator for the concurrent container
- for debug purpose only.
- */
+ protected:
+ //@cond
template <bool IsConst>
class iterator_type: private cds::intrusive::michael_set::details::iterator< bucket_type, IsConst >
{
- //@cond
typedef cds::intrusive::michael_set::details::iterator< bucket_type, IsConst > base_class;
friend class MichaelHashMap;
- //@endcond
protected:
- //@cond
- //typedef typename base_class::bucket_type bucket_type;
typedef typename base_class::bucket_ptr bucket_ptr;
typedef typename base_class::list_iterator list_iterator;
- //typedef typename bucket_type::key_type key_type;
- //@endcond
-
public:
/// Value pointer type (const for const_iterator)
typedef typename cds::details::make_const_type<typename MichaelHashMap::mapped_type, IsConst>::pointer value_ptr;
typedef typename cds::details::make_const_type<typename MichaelHashMap::value_type, IsConst>::reference pair_ref;
protected:
- //@cond
iterator_type( list_iterator const& it, bucket_ptr pFirst, bucket_ptr pLast )
: base_class( it, pFirst, pLast )
{}
- //@endcond
public:
/// Default ctor
return !( *this == i );
}
};
+ //@endcond
public:
+ ///@name Forward iterators (thread-safe under RCU lock)
+ //@{
+
/// Forward iterator
+ /**
+ The forward iterator for Michael's map is based on \p OrderedList forward iterator and has some features:
+ - it has no post-increment operator
+ - it iterates items in unordered fashion
+
+ You may safely use iterators in multi-threaded environment only under RCU lock.
+ Otherwise, a crash is possible if another thread deletes the element the iterator points to.
+
+ The iterator interface:
+ \code
+ class iterator {
+ public:
+ // Default constructor
+ iterator();
+
+ // Copy construtor
+ iterator( iterator const& src );
+
+ // Dereference operator
+ value_type * operator ->() const;
+
+ // Dereference operator
+ value_type& operator *() const;
+
+ // Preincrement operator
+ iterator& operator ++();
+
+ // Assignment operator
+ iterator& operator = (iterator const& src);
+
+ // Equality operators
+ bool operator ==(iterator const& i ) const;
+ bool operator !=(iterator const& i ) const;
+ };
+ \endcode
+ */
typedef iterator_type< false > iterator;
/// Const forward iterator
}
/// Returns a forward const iterator addressing the first element in a map
- //@{
const_iterator begin() const
{
return get_const_begin();
}
+
+ /// Returns a forward const iterator addressing the first element in a map
const_iterator cbegin() const
{
return get_const_begin();
}
- //@}
/// Returns an const iterator that addresses the location succeeding the last element in a map
- //@{
const_iterator end() const
{
return get_const_end();
}
+
+ /// Returns an const iterator that addresses the location succeeding the last element in a map
const_iterator cend() const
{
return get_const_end();
}
- //@}
+ //@}
private:
//@cond
The function applies RCU lock internally.
- Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
+ Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successful,
\p second is true if new item has been added or \p false if the item with \p key
already exists.
template <typename K, typename Func>
std::pair<bool, bool> update( K const& key, Func func, bool bAllowInsert = true )
{
- std::pair<bool, bool> bRet = bucket( key ).update( key, func );
- if ( bRet.first && bRet.second )
+ std::pair<bool, bool> bRet = bucket( key ).update( key, func, bAllowInsert );
+ if ( bRet.second )
++m_ItemCounter;
return bRet;
}
//@cond
- // Deprecated
template <typename K, typename Func>
+ CDS_DEPRECATED("ensure() is deprecated, use update()")
std::pair<bool, bool> ensure( K const& key, Func func )
{
- std::pair<bool, bool> bRet = bucket( key ).ensure( key, func );
- if ( bRet.first && bRet.second )
- ++m_ItemCounter;
- return bRet;
+ return update( key, func, true );
}
//@endcond
return bucket( key ).contains( key );
}
//@cond
- // Deprecated
template <typename K>
+ CDS_DEPRECATED("deprecated, use contains()")
bool find( K const& key )
{
return bucket( key ).contains( key );
return bucket( key ).contains( key, pred );
}
//@cond
- // Deprecated
template <typename K, typename Less>
+ CDS_DEPRECATED("deprecated, use contains()")
bool find_with( K const& key, Less pred )
{
return bucket( key ).contains( key, pred );