Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / cds / algo / backoff_strategy.h
index 01c1dbd194d0a0b9670b2fe2d6329b76ab3dd424..62cf5a5df0c07ddc7e4b7ea1b44c99e5f6ceffc4 100644 (file)
@@ -1,7 +1,35 @@
-//$$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 __CDS_BACKOFF_STRATEGY_H
-#define __CDS_BACKOFF_STRATEGY_H
+#ifndef CDSLIB_BACKOFF_STRATEGY_H
+#define CDSLIB_BACKOFF_STRATEGY_H
 
 /*
     Filename: backoff_strategy.h
@@ -16,9 +44,9 @@
     2009.09.10  Maxim Khiszinsky    reset() function added
 */
 
+#include <utility>      // declval
 #include <thread>
 #include <chrono>
-#include <cds/details/defs.h>
 #include <cds/compiler/backoff.h>
 
 namespace cds {
@@ -56,16 +84,16 @@ namespace cds {
         /// Empty backoff strategy. Do nothing
         struct empty {
             //@cond
-            void operator ()()
+            void operator ()() const CDS_NOEXCEPT
             {}
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
                 return pr();
             }
 
-            void reset()
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -73,21 +101,21 @@ namespace cds {
         /// Switch to another thread (yield). Good for thread preemption architecture.
         struct yield {
             //@cond
-            void operator ()()
+            void operator ()() const CDS_NOEXCEPT
             {
                 std::this_thread::yield();
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
-                if ( pr() )
+                if ( pr())
                     return true;
                 operator()();
                 return false;
             }
 
-            void reset()
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -99,7 +127,7 @@ namespace cds {
         */
         struct pause {
             //@cond
-            void operator ()()
+            void operator ()() const CDS_NOEXCEPT
             {
 #            ifdef CDS_backoff_pause_defined
                 platform::backoff_pause();
@@ -107,15 +135,15 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
-                if ( pr() )
+                if ( pr())
                     return true;
                 operator()();
                 return false;
             }
 
-            void reset()
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -128,7 +156,7 @@ namespace cds {
         struct hint
         {
         //@cond
-            void operator ()()
+            void operator ()() const CDS_NOEXCEPT
             {
 #           if defined(CDS_backoff_hint_defined)
                 platform::backoff_hint();
@@ -138,15 +166,15 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_(noexcept(std::declval<Predicate>()()))
             {
-                if ( pr() )
+                if ( pr())
                     return true;
                 operator()();
                 return false;
             }
 
-            void reset()
+            static void reset() CDS_NOEXCEPT
             {}
         //@endcond
         };
@@ -216,7 +244,7 @@ namespace cds {
         public:
             typedef SpinBkoff  spin_backoff    ;   ///< spin back-off strategy
             typedef YieldBkoff yield_backoff   ;   ///< yield back-off strategy
-            typedef Tag         impl_tag        ;   ///< implementation separation tag
+            typedef Tag        impl_tag        ;   ///< implementation separation tag
 
             static size_t s_nExpMin ;   ///< Default minimum spinning bound (16)
             static size_t s_nExpMax ;   ///< Default maximum spinning bound (16384)
@@ -231,7 +259,7 @@ namespace cds {
 
         public:
             /// Initializes m_nExpMin and m_nExpMax from default s_nExpMin and s_nExpMax respectively
-            exponential()
+            exponential() CDS_NOEXCEPT
                 : m_nExpMin( s_nExpMin )
                 , m_nExpMax( s_nExpMax )
             {
@@ -245,7 +273,7 @@ namespace cds {
             exponential(
                 size_t nExpMin,     ///< Minimum spinning
                 size_t nExpMax      ///< Maximum spinning
-                )
+                ) CDS_NOEXCEPT
                 : m_nExpMin( nExpMin )
                 , m_nExpMax( nExpMax )
             {
@@ -253,7 +281,7 @@ namespace cds {
             }
 
             //@cond
-            void operator ()()
+            void operator ()() CDS_NOEXCEPT_(noexcept(std::declval<spin_backoff>()()) && noexcept(std::declval<yield_backoff>()()))
             {
                 if ( m_nExpCur <= m_nExpMax ) {
                     for ( size_t n = 0; n < m_nExpCur; ++n )
@@ -265,11 +293,11 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()( Predicate pr ) CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()) && noexcept(std::declval<spin_backoff>()()) && noexcept(std::declval<yield_backoff>()()))
             {
                 if ( m_nExpCur <= m_nExpMax ) {
                     for ( size_t n = 0; n < m_nExpCur; ++n ) {
-                        if ( m_bkSpin(pr) )
+                        if ( m_bkSpin(pr))
                             return true;
                     }
                     m_nExpCur *= 2;
@@ -279,7 +307,7 @@ namespace cds {
                 return false;
             }
 
-            void reset()
+            void reset() CDS_NOEXCEPT_( noexcept( std::declval<spin_backoff>().reset()) && noexcept( std::declval<yield_backoff>().reset()))
             {
                 m_nExpCur = m_nExpMin;
                 m_bkSpin.reset();
@@ -356,12 +384,12 @@ namespace cds {
 
         public:
             /// Default ctor takes the timeout from s_nTimeout
-            delay()
+            delay() CDS_NOEXCEPT
                 : m_nTimeout( s_nTimeout )
             {}
 
             /// Initializes timeout from \p nTimeout
-            CDS_CONSTEXPR delay( unsigned int nTimeout )
+            CDS_CONSTEXPR explicit delay( unsigned int nTimeout ) CDS_NOEXCEPT
                 : m_nTimeout( nTimeout )
             {}
 
@@ -372,17 +400,17 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) const
+            bool operator()(Predicate pr) const
             {
                 for ( unsigned int i = 0; i < m_nTimeout; i += 2 ) {
-                    if ( pr() )
+                    if ( pr())
                         return true;
                     std::this_thread::sleep_for( duration_type( 2 ));
                 }
                 return false;
             }
 
-            void reset() const
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -406,7 +434,7 @@ namespace cds {
         //@cond
             typedef delay<Duration> base_class;
         public:
-            delay_of()
+            delay_of() CDS_NOEXCEPT
                 : base_class( Timeout )
             {}
         //@endcond
@@ -423,4 +451,4 @@ namespace cds {
 } // namespace cds
 
 
-#endif // #ifndef __CDS_BACKOFF_STRATEGY_H
+#endif // #ifndef CDSLIB_BACKOFF_STRATEGY_H