// Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // (C) Copyright 2011 Vicente J. Botet Escriba #ifndef BOOST_THREAD_V2_THREAD_HPP #define BOOST_THREAD_V2_THREAD_HPP #include #ifdef BOOST_THREAD_USES_CHRONO #include #include #endif #include #include namespace boost { namespace this_thread { namespace no_interruption_point { #ifdef BOOST_THREAD_USES_CHRONO template void sleep_until(const chrono::time_point& t) { using namespace chrono; mutex mut; condition_variable cv; unique_lock lk(mut); while (Clock::now() < t) cv.wait_until(lk, t); } #ifdef BOOST_THREAD_SLEEP_FOR_IS_STEADY template void sleep_for(const chrono::duration& d) { using namespace chrono; if (d > duration::zero()) { duration Max = nanoseconds::max BOOST_PREVENT_MACRO_SUBSTITUTION (); nanoseconds ns; if (d < Max) { ns = duration_cast(d); if (ns < d) ++ns; } else ns = nanoseconds:: max BOOST_PREVENT_MACRO_SUBSTITUTION (); sleep_for(ns); } } template inline BOOST_SYMBOL_VISIBLE void sleep_until(const chrono::time_point& t) { using namespace chrono; sleep_for(t - steady_clock::now()); } #else template void sleep_for(const chrono::duration& d) { using namespace chrono; if (d > duration::zero()) { steady_clock::time_point c_timeout = steady_clock::now() + ceil(d); sleep_until(c_timeout); } } #endif #endif } #ifdef BOOST_THREAD_USES_CHRONO template void sleep_until(const chrono::time_point& t) { using namespace chrono; mutex mut; condition_variable cv; unique_lock lk(mut); while (Clock::now() < t) cv.wait_until(lk, t); } #ifdef BOOST_THREAD_SLEEP_FOR_IS_STEADY template void sleep_for(const chrono::duration& d) { using namespace chrono; if (d > duration::zero()) { duration Max = nanoseconds::max BOOST_PREVENT_MACRO_SUBSTITUTION (); nanoseconds ns; if (d < Max) { ns = duration_cast(d); if (ns < d) ++ns; } else ns = nanoseconds:: max BOOST_PREVENT_MACRO_SUBSTITUTION (); sleep_for(ns); } } template inline BOOST_SYMBOL_VISIBLE void sleep_until(const chrono::time_point& t) { using namespace chrono; sleep_for(t - steady_clock::now()); } #else template void sleep_for(const chrono::duration& d) { using namespace chrono; if (d > duration::zero()) { steady_clock::time_point c_timeout = steady_clock::now() + ceil(d); sleep_until(c_timeout); } } #endif #endif } } #endif