Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / cds / algo / backoff_strategy.h
index de098c5beb24c995ad1acf6841c3f210b315d567..62cf5a5df0c07ddc7e4b7ea1b44c99e5f6ceffc4 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_BACKOFF_STRATEGY_H
 #define CDSLIB_BACKOFF_STRATEGY_H
@@ -60,12 +88,12 @@ namespace cds {
             {}
 
             template <typename Predicate>
-            bool operator()(Predicate pr) CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()() ))
+            bool operator()(Predicate pr) const CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()))
             {
                 return pr();
             }
 
-            void reset() const CDS_NOEXCEPT
+            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 ()() CDS_NOEXCEPT
+            void operator ()() const CDS_NOEXCEPT
             {
                 std::this_thread::yield();
             }
 
             template <typename Predicate>
-            bool operator()(Predicate pr) CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()() ))
+            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
         };
@@ -99,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();
@@ -107,15 +135,15 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()(Predicate pr) CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()() ))
+            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
         };
@@ -128,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();
@@ -138,15 +166,15 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()(Predicate pr) CDS_NOEXCEPT_(noexcept(std::declval<Predicate>()() ))
+            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
         };
@@ -265,11 +293,11 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) CDS_NOEXCEPT_( noexcept(std::declval<Predicate>()()) && noexcept(std::declval<spin_backoff>()()) && noexcept(std::declval<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;
@@ -279,7 +307,7 @@ namespace cds {
                 return false;
             }
 
-            void reset() CDS_NOEXCEPT_( noexcept( std::declval<spin_backoff>().reset() ) && noexcept( std::declval<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();
@@ -361,7 +389,7 @@ 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 )
             {}
 
@@ -375,14 +403,14 @@ namespace cds {
             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
         };