Revised noexcept expressions in back-off strategies
[libcds.git] / cds / algo / backoff_strategy.h
index 24852bbafb294811314be6de3fde571b2e75f331..c80ea829d43d365d7a55d220c4a13dabb98b1b4e 100644 (file)
@@ -16,9 +16,9 @@
     2009.09.10  Maxim Khiszinsky    reset() function added
 */
 
+#include <thread>
+#include <chrono>
 #include <cds/compiler/backoff.h>
-#include <cds/details/std/thread.h>
-#include <cds/details/std/chrono.h>
 
 namespace cds {
     /// Different backoff schemes
@@ -55,16 +55,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) CDS_NOEXCEPT
             {
                 return pr();
             }
 
-            void reset()
+            void reset() const CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -72,14 +72,13 @@ namespace cds {
         /// Switch to another thread (yield). Good for thread preemption architecture.
         struct yield {
             //@cond
-            void operator ()()
+            void operator ()() CDS_NOEXCEPT
             {
-                cds_std::this_thread::yield();
-                //OS::yield();
+                std::this_thread::yield();
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()( Predicate pr ) CDS_NOEXCEPT
             {
                 if ( pr() )
                     return true;
@@ -87,7 +86,7 @@ namespace cds {
                 return false;
             }
 
-            void reset()
+            void reset() const CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -99,7 +98,7 @@ namespace cds {
         */
         struct pause {
             //@cond
-            void operator ()()
+            void operator ()() CDS_NOEXCEPT
             {
 #            ifdef CDS_backoff_pause_defined
                 platform::backoff_pause();
@@ -107,7 +106,7 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()( Predicate pr ) CDS_NOEXCEPT
             {
                 if ( pr() )
                     return true;
@@ -115,7 +114,7 @@ namespace cds {
                 return false;
             }
 
-            void reset()
+            void reset() const CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -128,7 +127,7 @@ namespace cds {
         struct hint
         {
         //@cond
-            void operator ()()
+            void operator ()() CDS_NOEXCEPT
             {
 #           if defined(CDS_backoff_hint_defined)
                 platform::backoff_hint();
@@ -138,7 +137,7 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()( Predicate pr ) CDS_NOEXCEPT
             {
                 if ( pr() )
                     return true;
@@ -146,7 +145,7 @@ namespace cds {
                 return false;
             }
 
-            void reset()
+            void reset() const CDS_NOEXCEPT
             {}
         //@endcond
         };
@@ -231,7 +230,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 +244,7 @@ namespace cds {
             exponential(
                 size_t nExpMin,     ///< Minimum spinning
                 size_t nExpMax      ///< Maximum spinning
-                )
+                ) CDS_NOEXCEPT
                 : m_nExpMin( nExpMin )
                 , m_nExpMax( nExpMax )
             {
@@ -253,7 +252,7 @@ namespace cds {
             }
 
             //@cond
-            void operator ()()
+            void operator ()() CDS_NOEXCEPT_(noexcept(spin_backoff()()) && noexcept(yield_backoff()()))
             {
                 if ( m_nExpCur <= m_nExpMax ) {
                     for ( size_t n = 0; n < m_nExpCur; ++n )
@@ -265,7 +264,7 @@ namespace cds {
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr )
+            bool operator()( Predicate pr ) CDS_NOEXCEPT_(noexcept( spin_backoff()()) && noexcept( yield_backoff()()))
             {
                 if ( m_nExpCur <= m_nExpMax ) {
                     for ( size_t n = 0; n < m_nExpCur; ++n ) {
@@ -279,7 +278,7 @@ namespace cds {
                 return false;
             }
 
-            void reset()
+            void reset() CDS_NOEXCEPT_(noexcept(spin_backoff().reset()) && noexcept(yield_backoff().reset()))
             {
                 m_nExpCur = m_nExpMin;
                 m_bkSpin.reset();
@@ -342,7 +341,7 @@ namespace cds {
             \endcode
 
         */
-        template <class Duration = cds_std::chrono::milliseconds, typename Tag=void >
+        template <class Duration = std::chrono::milliseconds, typename Tag=void >
         class delay
         {
         public:
@@ -356,33 +355,33 @@ 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 delay( unsigned int nTimeout ) CDS_NOEXCEPT
                 : m_nTimeout( nTimeout )
             {}
 
             //@cond
-            void operator()() const
+            void operator()() const CDS_NOEXCEPT
             {
-                cds_std::this_thread::sleep_for( duration_type( m_nTimeout ));
+                std::this_thread::sleep_for( duration_type( m_nTimeout ));
             }
 
             template <typename Predicate>
-            bool operator()( Predicate pr ) const
+            bool operator()( Predicate pr ) const CDS_NOEXCEPT
             {
                 for ( unsigned int i = 0; i < m_nTimeout; i += 2 ) {
                     if ( pr() )
                         return true;
-                    cds_std::this_thread::sleep_for( duration_type( 2 ));
+                    std::this_thread::sleep_for( duration_type( 2 ));
                 }
                 return false;
             }
 
-            void reset() const
+            void reset() const CDS_NOEXCEPT
             {}
             //@endcond
         };
@@ -400,13 +399,13 @@ namespace cds {
             The declaration <tt>cds::backoff::delay_of< 5 > bkoff</tt> is equal for
             <tt>cds::backoff::delay<> bkoff(5)</tt>.
         */
-        template <unsigned int Timeout, class Duration = cds_std::chrono::milliseconds >
+        template <unsigned int Timeout, class Duration = std::chrono::milliseconds >
         class delay_of: public delay<Duration>
         {
         //@cond
             typedef delay<Duration> base_class;
         public:
-            delay_of()
+            delay_of() CDS_NOEXCEPT
                 : base_class( Timeout )
             {}
         //@endcond