Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / cds / algo / backoff_strategy.h
index c80ea829d43d365d7a55d220c4a13dabb98b1b4e..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/
 
-#ifndef __CDS_BACKOFF_STRATEGY_H
-#define __CDS_BACKOFF_STRATEGY_H
+    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_BACKOFF_STRATEGY_H
+#define CDSLIB_BACKOFF_STRATEGY_H
 
 /*
     Filename: backoff_strategy.h
@@ -16,6 +44,7 @@
     2009.09.10  Maxim Khiszinsky    reset() function added
 */
 
+#include <utility>      // declval
 #include <thread>
 #include <chrono>
 #include <cds/compiler/backoff.h>
@@ -59,12 +88,12 @@ namespace cds {
             {}
 
             template <typename Predicate>
-            bool operator()(Predicate pr) CDS_NOEXCEPT
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
                 return pr();
             }
 
-            void reset() const CDS_NOEXCEPT
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -72,21 +101,21 @@ namespace cds {
         /// Switch to another thread (yield). Good for thread preemption architecture.
         struct yield {
             //@cond
-            void operator ()() CDS_NOEXCEPT
+            void operator ()() const CDS_NOEXCEPT
             {
                 std::this_thread::yield();
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) CDS_NOEXCEPT
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
-                if ( pr() )
+                if ( pr())
                     return true;
                 operator()();
                 return false;
             }
 
-            void reset() const CDS_NOEXCEPT
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -98,7 +127,7 @@ namespace cds {
         */
         struct pause {
             //@cond
-            void operator ()() CDS_NOEXCEPT
+            void operator ()() const CDS_NOEXCEPT
             {
 #            ifdef CDS_backoff_pause_defined
                 platform::backoff_pause();
@@ -106,15 +135,15 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) CDS_NOEXCEPT
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
-                if ( pr() )
+                if ( pr())
                     return true;
                 operator()();
                 return false;
             }
 
-            void reset() const CDS_NOEXCEPT
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -127,7 +156,7 @@ namespace cds {
         struct hint
         {
         //@cond
-            void operator ()() CDS_NOEXCEPT
+            void operator ()() const CDS_NOEXCEPT
             {
 #           if defined(CDS_backoff_hint_defined)
                 platform::backoff_hint();
@@ -137,15 +166,15 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) CDS_NOEXCEPT
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_(noexcept(std::declval<Predicate>()()))
             {
-                if ( pr() )
+                if ( pr())
                     return true;
                 operator()();
                 return false;
             }
 
-            void reset() const CDS_NOEXCEPT
+            static void reset() CDS_NOEXCEPT
             {}
         //@endcond
         };
@@ -215,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)
@@ -252,7 +281,7 @@ namespace cds {
             }
 
             //@cond
-            void operator ()() CDS_NOEXCEPT_(noexcept(spin_backoff()()) && noexcept(yield_backoff()()))
+            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 )
@@ -264,11 +293,11 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) CDS_NOEXCEPT_(noexcept( spin_backoff()()) && noexcept( yield_backoff()()))
+            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;
@@ -278,7 +307,7 @@ namespace cds {
                 return false;
             }
 
-            void reset() CDS_NOEXCEPT_(noexcept(spin_backoff().reset()) && noexcept(yield_backoff().reset()))
+            void reset() CDS_NOEXCEPT_( noexcept( std::declval<spin_backoff>().reset()) && noexcept( std::declval<yield_backoff>().reset()))
             {
                 m_nExpCur = m_nExpMin;
                 m_bkSpin.reset();
@@ -360,28 +389,28 @@ namespace cds {
             {}
 
             /// Initializes timeout from \p nTimeout
-            CDS_CONSTEXPR delay( unsigned int nTimeout ) CDS_NOEXCEPT
+            CDS_CONSTEXPR explicit delay( unsigned int nTimeout ) CDS_NOEXCEPT
                 : m_nTimeout( nTimeout )
             {}
 
             //@cond
-            void operator()() const CDS_NOEXCEPT
+            void operator()() const
             {
                 std::this_thread::sleep_for( duration_type( m_nTimeout ));
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) const CDS_NOEXCEPT
+            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 CDS_NOEXCEPT
+            static void reset() CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -422,4 +451,4 @@ namespace cds {
 } // namespace cds
 
 
-#endif // #ifndef __CDS_BACKOFF_STRATEGY_H
+#endif // #ifndef CDSLIB_BACKOFF_STRATEGY_H