Fixes RigtorpMPMC test case
[libcds.git] / cds / urcu / raw_ptr.h
index 6455c9e7efa58f96710608f77e5c71b093801b08..c9bb2fc5bceb7461b1f3c763eccdcc65487a4ef9 100644 (file)
@@ -1,4 +1,32 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
+
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
+
+    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_URCU_RAW_PTR_H
 #define CDSLIB_URCU_RAW_PTR_H
@@ -19,7 +47,7 @@ namespace cds { namespace urcu {
         outside RCU lock.
 
         The object of \p %raw_ptr solves that problem: it contains the pointer to the node found
-        and a chain of nodes that be reclaimed during traversing. The \p %raw_ptr object destructor
+        and a chain of nodes that were be reclaimed during traversing. The \p %raw_ptr object destructor
         frees the chain (but not the node found) passing it to RCU \p batch_retire().
 
         The object of \p %raw_ptr class must be destructed only outside RCU-lock of current thread.
@@ -30,26 +58,26 @@ namespace cds { namespace urcu {
         Template arguments:
         - \p RCU - one of \ref cds_urcu_gc "RCU type"
         - \p ValueType - type of values stored in container
-        - \p ReclaimedEnumerator - implemntation-defined for each type of container
+        - \p ReclaimedEnumerator - implementation-defined for each type of container
 
         Example: let \p Container is an RCU container
-        \code
-        Container c;
-        // ...
-        // Find a key
-        typename Container::raw_ptr pRaw;
-
-        // RCU locked section
-        {
-            typename Container::rcu_lock l;
-            pRaw = c.get( key );
-            if ( pRaw ) {
-                // Deal with pRaw
+        @code
+            Container c;
+            // ...
+            // Find a key
+            typename Container::raw_ptr pRaw;
+
+            // RCU locked section
+            {
+                typename Container::rcu_lock l;
+                pRaw = c.get( key );
+                if ( pRaw ) {
+                    // Deal with pRaw
+                }
             }
-        }
-        // Release outside RCU-lock
-        pRaw.release();
-        \endcode
+            // Release outside RCU-lock
+            pRaw.release();
+        @endcode
     */
     template <
         class RCU,
@@ -60,7 +88,7 @@ namespace cds { namespace urcu {
     {
     public:
         typedef RCU rcu;    ///< RCU type - one of <tt>cds::urcu::gc< ... ></tt>
-        typedef ValueType   value_type; ///< Value type pointed by \p raw_ptr
+        typedef ValueType   value_type; ///< Value type pointed by \p %raw_ptr
         typedef ReclaimedEnumerator reclaimed_enumerator; ///< implementation-defined, for internal use only
 
     private:
@@ -86,7 +114,7 @@ namespace cds { namespace urcu {
         /// Copy ctor is prohibited
         raw_ptr( raw_ptr const& ) = delete;
 
-        ///@cond
+        //@cond
         // Only for internal use
         raw_ptr( value_type * p, reclaimed_enumerator&& e )
             : m_ptr( p )
@@ -108,16 +136,12 @@ namespace cds { namespace urcu {
         /// Move assignment operator
         /**
             This operator may be called only inside RCU-lock.
-            The \p this should be empty.
-
-            In general, move assignment is intented for internal use.
         */
         raw_ptr& operator=( raw_ptr&& p ) CDS_NOEXCEPT
         {
-            assert( empty() );
-
+            assert( rcu::is_locked());
             m_ptr = p.m_ptr;
-            m_Enum = std::move( p.m_Enum );
+            m_Enum.combine(  std::move( p.m_Enum ));
             p.m_ptr = nullptr;
             return *this;
         }
@@ -159,18 +183,115 @@ namespace cds { namespace urcu {
 
         /// Releases the \p %raw_ptr object
         /**
-            This function may be called only outside RCU section.
-            After \p %release() the object can be reused.
+            This function may be called only outside RCU locked region.
+            After \p %release() the object becomes empty and can be reused.
         */
         void release()
         {
-            assert( !rcu::is_locked() );
             m_Enum.apply();
             m_ptr = nullptr;
         }
     };
 
+    //@cond
+    // Adapter of \p raw_ptr for non-intrusive containers based on intrusive counterpart
+    template <
+        typename ValueType,
+        typename RawPtr,
+        typename Converter
+    >
+    class raw_ptr_adaptor: private RawPtr
+    {
+    public:
+        typedef RawPtr      intrusive_raw_ptr;
+        typedef ValueType   value_type;
+        typedef typename intrusive_raw_ptr::value_type node_type;
+        typedef Converter   converter_type;
+
+    public:
+        // Constructs an empty raw pointer
+        raw_ptr_adaptor()
+            : intrusive_raw_ptr()
+        {}
+
+        // Move ctor
+        raw_ptr_adaptor( intrusive_raw_ptr&& p )
+            : intrusive_raw_ptr( std::move(p))
+        {}
+
+        // Move ctor
+        raw_ptr_adaptor( raw_ptr_adaptor&& p )
+            : intrusive_raw_ptr( std::move(p))
+        {}
+
+        // Copy ctor is prohibited
+        raw_ptr_adaptor( raw_ptr_adaptor const& ) = delete;
+
+        // Releases the raw pointer
+        ~raw_ptr_adaptor()
+        {
+            release();
+        }
+
+    public:
+        // Move assignment operator
+        /*
+            This operator may be called only inside RCU-lock.
+            The \p this should be empty.
+
+            In general, move assignment is intented for internal use.
+        */
+        raw_ptr_adaptor& operator=( raw_ptr_adaptor&& p ) CDS_NOEXCEPT
+        {
+            intrusive_raw_ptr::operator =(std::move(p));
+            return *this;
+        }
+
+        // Copy assignment is prohibited
+        raw_ptr_adaptor& operator=( raw_ptr_adaptor const& ) = delete;
+
+        // Returns a pointer to stored value
+        value_type * operator ->() const CDS_NOEXCEPT
+        {
+            return converter_type()( intrusive_raw_ptr::operator->());
+        }
+
+        // Returns a reference to stored value
+        value_type& operator *()
+        {
+            return converter_type()( intrusive_raw_ptr::operator*());
+        }
+
+        // Returns a reference to stored value
+        value_type const& operator *() const
+        {
+            return converter_type()( intrusive_raw_ptr::operator*());
+        }
+
+        // Checks if the \p %raw_ptr is \p nullptr
+        bool empty() const CDS_NOEXCEPT
+        {
+            return intrusive_raw_ptr::empty();
+        }
+
+        // Checks if the \p %raw_ptr is not empty
+        explicit operator bool() const CDS_NOEXCEPT
+        {
+            return !empty();
+        }
+
+        // Releases the \p %raw_ptr object
+        /*
+            This function may be called only outside RCU section.
+            After \p %release() the object can be reused.
+        */
+        void release()
+        {
+            intrusive_raw_ptr::release();
+        }
+    };
+    //@endcond
+
 }} // namespace cds::urcu
 
 #endif // #ifndef CDSLIB_URCU_RAW_PTR_H
-