2
0
mirror of https://github.com/boostorg/atomic.git synced 2026-01-26 06:22:08 +00:00
Files
atomic/test/test_clock.hpp
Andrey Semashev 3929919495 Implement a special test_clock for Windows.
The implementation uses GetTickCount/GetTickCount64 internally,
which is a steady and sufficiently low precision time source.

We need the clock to have relatively low precision so that wait
tests don't fail spuriously because the blocked threads wake up
too soon, according to more precise clocks.

boost::chrono::system_clock currently has an acceptably low precision,
but it is not a steady clock.
2020-06-12 13:32:32 +03:00

56 lines
1.6 KiB
C++

// Copyright (c) 2020 Andrey Semashev
//
// 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)
#ifndef BOOST_ATOMIC_TEST_TEST_CLOCK_HPP_INCLUDED_
#define BOOST_ATOMIC_TEST_TEST_CLOCK_HPP_INCLUDED_
#include <boost/config.hpp>
#if defined(BOOST_WINDOWS)
#include <boost/winapi/config.hpp>
#include <boost/winapi/basic_types.hpp>
#include <boost/winapi/time.hpp>
#include <boost/ratio/ratio.hpp>
#endif
#include <boost/chrono/chrono.hpp>
namespace chrono = boost::chrono;
#if defined(BOOST_WINDOWS)
// On Windows high precision clocks tend to cause spurious test failures because threads wake up earlier than expected.
// Use a lower precision steady clock for tests.
struct test_clock
{
#if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
typedef boost::winapi::ULONGLONG_ rep;
#else
typedef boost::winapi::DWORD_ rep;
#endif
typedef boost::milli period;
typedef chrono::duration< rep, period > duration;
typedef chrono::time_point< test_clock, duration > time_point;
static BOOST_CONSTEXPR_OR_CONST bool is_steady = true;
static time_point now() BOOST_NOEXCEPT
{
#if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
rep ticks = boost::winapi::GetTickCount64();
#else
rep ticks = boost::winapi::GetTickCount();
#endif
return time_point(duration(ticks));
}
};
#elif defined(BOOST_CHRONO_HAS_CLOCK_STEADY)
typedef chrono::steady_clock test_clock;
#else
typedef chrono::system_clock test_clock;
#endif
#endif // BOOST_ATOMIC_TEST_TEST_CLOCK_HPP_INCLUDED_