From 20dfb9d38180b3f48ed9d543af4debf061ae3d0b Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Tue, 13 Oct 2020 09:22:42 -0700 Subject: [PATCH 1/8] Correctly convert relative time to absolute time ... without overflow by saturating on the high end and clipping to `now`. --- stl/inc/condition_variable | 4 ++-- stl/inc/mutex | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/stl/inc/condition_variable b/stl/inc/condition_variable index 1289183537c..7acd1e47a4e 100644 --- a/stl/inc/condition_variable +++ b/stl/inc/condition_variable @@ -130,7 +130,7 @@ public: template bool wait_for(_Lock& _Lck, const chrono::duration<_Rep, _Period>& _Rel_time, _Predicate _Pred) { // wait for signal with timeout and check predicate - return wait_until(_Lck, chrono::steady_clock::now() + _Rel_time, _STD move(_Pred)); + return wait_until(_Lck, _To_absolute_time(_Rel_time), _STD move(_Pred)); } template @@ -232,7 +232,7 @@ public: template bool wait_for(_Lock& _Lck, stop_token _Stoken, const chrono::duration<_Rep, _Period>& _Rel_time, _Predicate _Pred) { - return wait_until(_Lck, _STD move(_Stoken), chrono::steady_clock::now() + _Rel_time, _STD move(_Pred)); + return wait_until(_Lck, _STD move(_Stoken), _To_absolute_time(_Rel_time), _STD move(_Pred)); } #endif // _HAS_CXX20 diff --git a/stl/inc/mutex b/stl/inc/mutex index 1811ccc021b..ada5f65008b 100644 --- a/stl/inc/mutex +++ b/stl/inc/mutex @@ -564,6 +564,21 @@ void(call_once)(once_flag& _Once, _Fn&& _Fx, _Args&&... _Ax) noexcept( #undef _WINDOWS_API #undef _RENAME_WINDOWS_API +template +_NODISCARD inline chrono::steady_clock::time_point _To_absolute_time( + const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { + chrono::steady_clock::time_point _Abs_time = chrono::steady_clock::now(); + if (_Rel_time > chrono::duration<_Rep, _Period>::zero()) { + const auto _Forever = chrono::steady_clock::time_point::max(); + if (_Abs_time < _Forever - _Rel_time) { + _Abs_time += _Rel_time; + } else { + _Abs_time = _Forever; + } + } + return _Abs_time; +} + // condition_variable, timed_mutex, and recursive_timed_mutex are not supported under /clr #ifndef _M_CEE enum class cv_status { // names for wait returns @@ -628,7 +643,7 @@ public: template bool wait_for(unique_lock& _Lck, const chrono::duration<_Rep, _Period>& _Rel_time, _Predicate _Pred) { // wait for signal with timeout and check predicate - return _Wait_until1(_Lck, chrono::steady_clock::now() + _Rel_time, _Pred); + return _Wait_until1(_Lck, _To_absolute_time(_Rel_time), _Pred); } template @@ -776,7 +791,7 @@ public: template _NODISCARD bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration - return try_lock_until(chrono::steady_clock::now() + _Rel_time); + return try_lock_until(_To_absolute_time(_Rel_time)); } template @@ -875,7 +890,7 @@ public: template _NODISCARD bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration - return try_lock_until(chrono::steady_clock::now() + _Rel_time); + return try_lock_until(_To_absolute_time(_Rel_time)); } template From 3f569865f43205d73d59d991ea5a7398de0acb25 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Tue, 13 Oct 2020 16:57:17 +0000 Subject: [PATCH 2/8] Annotate suspicious behavior in test --- tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp b/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp index 1495227aacd..0e63df1ccce 100644 --- a/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp +++ b/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp @@ -180,7 +180,7 @@ int main() { jthread worker([&](stop_token token) { unique_lock lck{m}; assert(cv.wait(lck, move(token), [] { return true; }) == true); - assert(cv.wait(lck, move(token), [&] { return b; }) == true); + assert(cv.wait(lck, move(token), [&] { return b; }) == true); // Intentionally uses moved-from token }); { @@ -198,7 +198,7 @@ int main() { jthread worker([&](stop_token token) { unique_lock lck{m}; assert(cv.wait_until(lck, move(token), infinity, [] { return true; }) == true); - assert(cv.wait_until(lck, move(token), infinity, [&] { return b; }) == true); + assert(cv.wait_until(lck, move(token), infinity, [&] { return b; }) == true); // Intentionally uses moved-from token }); { @@ -216,7 +216,7 @@ int main() { jthread worker([&](stop_token token) { unique_lock lck{m}; assert(cv.wait_for(lck, move(token), forever, [] { return true; }) == true); - assert(cv.wait_for(lck, move(token), forever, [&] { return b; }) == true); + assert(cv.wait_for(lck, move(token), forever, [&] { return b; }) == true); // Intentionally uses moved-from token }); { From 0e21effb50b4f6c78b595e246898c9edd9b37718 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Tue, 13 Oct 2020 17:08:36 +0000 Subject: [PATCH 3/8] guard against max macro --- stl/inc/mutex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/mutex b/stl/inc/mutex index ada5f65008b..283fe56f667 100644 --- a/stl/inc/mutex +++ b/stl/inc/mutex @@ -569,7 +569,7 @@ _NODISCARD inline chrono::steady_clock::time_point _To_absolute_time( const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { chrono::steady_clock::time_point _Abs_time = chrono::steady_clock::now(); if (_Rel_time > chrono::duration<_Rep, _Period>::zero()) { - const auto _Forever = chrono::steady_clock::time_point::max(); + const auto _Forever = (chrono::steady_clock::time_point::max)(); if (_Abs_time < _Forever - _Rel_time) { _Abs_time += _Rel_time; } else { From 9172c02a398f9f48875afc596a65ae1e08248dc7 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 13 Oct 2020 19:17:33 -0700 Subject: [PATCH 4/8] _To_absolute_time() cleanups. * This is a template, so we don't need inline. * Use auto for _Abs_time - its type is unsurprising. * Extract constexpr _Zero to avoid a function call in debug mode. * Similarly, upgrade _Forever to constexpr. --- stl/inc/mutex | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stl/inc/mutex b/stl/inc/mutex index 283fe56f667..e933af5da79 100644 --- a/stl/inc/mutex +++ b/stl/inc/mutex @@ -565,11 +565,12 @@ void(call_once)(once_flag& _Once, _Fn&& _Fx, _Args&&... _Ax) noexcept( #undef _RENAME_WINDOWS_API template -_NODISCARD inline chrono::steady_clock::time_point _To_absolute_time( +_NODISCARD chrono::steady_clock::time_point _To_absolute_time( const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { - chrono::steady_clock::time_point _Abs_time = chrono::steady_clock::now(); - if (_Rel_time > chrono::duration<_Rep, _Period>::zero()) { - const auto _Forever = (chrono::steady_clock::time_point::max)(); + auto _Abs_time = chrono::steady_clock::now(); + constexpr auto _Zero = chrono::duration<_Rep, _Period>::zero(); + if (_Rel_time > _Zero) { + constexpr auto _Forever = (chrono::steady_clock::time_point::max)(); if (_Abs_time < _Forever - _Rel_time) { _Abs_time += _Rel_time; } else { From fa7f8ee1f7df22c05140acb018fdedc908a04a0b Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 13 Oct 2020 19:29:18 -0700 Subject: [PATCH 5/8] Move _To_absolute_time up to thread, fix 3 more lines. --- stl/inc/mutex | 16 ---------------- stl/inc/shared_mutex | 4 ++-- stl/inc/thread | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/stl/inc/mutex b/stl/inc/mutex index e933af5da79..501fa02c01f 100644 --- a/stl/inc/mutex +++ b/stl/inc/mutex @@ -564,22 +564,6 @@ void(call_once)(once_flag& _Once, _Fn&& _Fx, _Args&&... _Ax) noexcept( #undef _WINDOWS_API #undef _RENAME_WINDOWS_API -template -_NODISCARD chrono::steady_clock::time_point _To_absolute_time( - const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { - auto _Abs_time = chrono::steady_clock::now(); - constexpr auto _Zero = chrono::duration<_Rep, _Period>::zero(); - if (_Rel_time > _Zero) { - constexpr auto _Forever = (chrono::steady_clock::time_point::max)(); - if (_Abs_time < _Forever - _Rel_time) { - _Abs_time += _Rel_time; - } else { - _Abs_time = _Forever; - } - } - return _Abs_time; -} - // condition_variable, timed_mutex, and recursive_timed_mutex are not supported under /clr #ifndef _M_CEE enum class cv_status { // names for wait returns diff --git a/stl/inc/shared_mutex b/stl/inc/shared_mutex index 705b1d30f18..06c921d8e76 100644 --- a/stl/inc/shared_mutex +++ b/stl/inc/shared_mutex @@ -110,7 +110,7 @@ public: template _NODISCARD bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration - return try_lock_until(chrono::steady_clock::now() + _Rel_time); + return try_lock_until(_To_absolute_time(_Rel_time)); } template @@ -168,7 +168,7 @@ public: template _NODISCARD bool try_lock_shared_for( const chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock non-exclusive for relative time - return try_lock_shared_until(_Rel_time + chrono::steady_clock::now()); + return try_lock_shared_until(_To_absolute_time(_Rel_time)); } template diff --git a/stl/inc/thread b/stl/inc/thread index 20d6ee84272..4ca9db7ad3f 100644 --- a/stl/inc/thread +++ b/stl/inc/thread @@ -156,6 +156,22 @@ private: _Thrd_t _Thr; }; +template +_NODISCARD chrono::steady_clock::time_point _To_absolute_time( + const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { + auto _Abs_time = chrono::steady_clock::now(); + constexpr auto _Zero = chrono::duration<_Rep, _Period>::zero(); + if (_Rel_time > _Zero) { + constexpr auto _Forever = (chrono::steady_clock::time_point::max)(); + if (_Abs_time < _Forever - _Rel_time) { + _Abs_time += _Rel_time; + } else { + _Abs_time = _Forever; + } + } + return _Abs_time; +} + namespace this_thread { _NODISCARD thread::id get_id() noexcept; @@ -183,7 +199,7 @@ namespace this_thread { template void sleep_for(const chrono::duration<_Rep, _Period>& _Rel_time) { - sleep_until(chrono::steady_clock::now() + _Rel_time); + sleep_until(_To_absolute_time(_Rel_time)); } } // namespace this_thread From 6000872af087cf9b656ebe5e862d2775d7e876f5 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 13 Oct 2020 19:49:03 -0700 Subject: [PATCH 6/8] Update to VS 2019 16.8 Preview 4 and Python 3.9.0. --- README.md | 6 +++--- azure-devops/provision-image.ps1 | 2 +- azure-pipelines.yml | 2 +- tests/CMakeLists.txt | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 70e3e9041e7..72576a20bee 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Just try to follow these rules, so we can spend more time fixing bugs and implem The STL uses boost-math headers to provide P0226R1 Mathematical Special Functions. We recommend using [vcpkg][] to acquire this dependency. -1. Install Visual Studio 2019 16.8 Preview 3 or later. +1. Install Visual Studio 2019 16.8 Preview 4 or later. * We recommend selecting "C++ CMake tools for Windows" in the VS Installer. This will ensure that you're using supported versions of CMake and Ninja. * Otherwise, install [CMake][] 3.17 or later, and [Ninja][] 1.8.2 or later. @@ -158,7 +158,7 @@ acquire this dependency. # How To Build With A Native Tools Command Prompt -1. Install Visual Studio 2019 16.8 Preview 3 or later. +1. Install Visual Studio 2019 16.8 Preview 4 or later. * We recommend selecting "C++ CMake tools for Windows" in the VS Installer. This will ensure that you're using supported versions of CMake and Ninja. * Otherwise, install [CMake][] 3.17 or later, and [Ninja][] 1.8.2 or later. @@ -235,7 +235,7 @@ C:\Users\username\Desktop>dumpbin /IMPORTS .\example.exe | findstr msvcp 1. Follow either [How To Build With A Native Tools Command Prompt][] or [How To Build With The Visual Studio IDE][]. 2. Invoke `git submodule update --init llvm-project` at the root of the STL source tree. -3. Acquire [Python][] 3.8 or newer and have it on the `PATH` (or run it directly using its absolute or relative path). +3. Acquire [Python][] 3.9 or newer and have it on the `PATH` (or run it directly using its absolute or relative path). 4. Have LLVM's `bin` directory on the `PATH` (so `clang-cl.exe` is available). * We recommend selecting "C++ Clang tools for Windows" in the VS Installer. This will automatically add LLVM to the `PATH` of the x86 and x64 Native Tools Command Prompts, and will ensure that you're using a supported version. diff --git a/azure-devops/provision-image.ps1 b/azure-devops/provision-image.ps1 index fb09a22eeea..ea84e185ab1 100644 --- a/azure-devops/provision-image.ps1 +++ b/azure-devops/provision-image.ps1 @@ -97,7 +97,7 @@ $Workloads = @( $ReleaseInPath = 'Preview' $Sku = 'Enterprise' $VisualStudioBootstrapperUrl = 'https://aka.ms/vs/16/pre/vs_enterprise.exe' -$PythonUrl = 'https://www.python.org/ftp/python/3.8.5/python-3.8.5-amd64.exe' +$PythonUrl = 'https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe' $CudaUrl = ` 'https://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_426.00_win10.exe' diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 49c9a55da32..bc4ce5d12c0 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -6,7 +6,7 @@ variables: tmpDir: 'D:\Temp' -pool: 'StlBuild-2020-09-14' +pool: 'StlBuild-2020-10-13' stages: - stage: Code_Format diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b8059d68cd6..1103c9dd3bc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,7 +18,7 @@ add_subdirectory(tr1) # chance to add to the config map and test directory global properties. add_subdirectory(utils/stl-lit) -find_package(Python "3.8" REQUIRED COMPONENTS Interpreter) +find_package(Python "3.9" REQUIRED COMPONENTS Interpreter) if(NOT DEFINED LIT_FLAGS) list(APPEND LIT_FLAGS "-o" "${CMAKE_CURRENT_BINARY_DIR}/test_results.json") From d36de98910c2bb6b980b4391c4f8529c8b1dc66b Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 14 Oct 2020 00:21:40 -0700 Subject: [PATCH 7/8] Fix _To_absolute_time return type. --- stl/inc/thread | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stl/inc/thread b/stl/inc/thread index 4ca9db7ad3f..583cdcf6631 100644 --- a/stl/inc/thread +++ b/stl/inc/thread @@ -157,10 +157,10 @@ private: }; template -_NODISCARD chrono::steady_clock::time_point _To_absolute_time( - const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { - auto _Abs_time = chrono::steady_clock::now(); - constexpr auto _Zero = chrono::duration<_Rep, _Period>::zero(); +_NODISCARD auto _To_absolute_time(const chrono::duration<_Rep, _Period>& _Rel_time) noexcept { + constexpr auto _Zero = chrono::duration<_Rep, _Period>::zero(); + const auto _Now = chrono::steady_clock::now(); + decltype(_Now + _Rel_time) _Abs_time = _Now; // return common type if (_Rel_time > _Zero) { constexpr auto _Forever = (chrono::steady_clock::time_point::max)(); if (_Abs_time < _Forever - _Rel_time) { From cab9288acac981cc5b71a1adf5b0616d4f915c0c Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 14 Oct 2020 00:52:53 -0700 Subject: [PATCH 8/8] clang-format. --- tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp b/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp index 0e63df1ccce..dfe79d60dde 100644 --- a/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp +++ b/tests/std/tests/P0660R10_jthread_and_cv_any/test.cpp @@ -198,7 +198,8 @@ int main() { jthread worker([&](stop_token token) { unique_lock lck{m}; assert(cv.wait_until(lck, move(token), infinity, [] { return true; }) == true); - assert(cv.wait_until(lck, move(token), infinity, [&] { return b; }) == true); // Intentionally uses moved-from token + assert(cv.wait_until(lck, move(token), infinity, [&] { return b; }) + == true); // Intentionally uses moved-from token }); { @@ -216,7 +217,8 @@ int main() { jthread worker([&](stop_token token) { unique_lock lck{m}; assert(cv.wait_for(lck, move(token), forever, [] { return true; }) == true); - assert(cv.wait_for(lck, move(token), forever, [&] { return b; }) == true); // Intentionally uses moved-from token + assert(cv.wait_for(lck, move(token), forever, [&] { return b; }) + == true); // Intentionally uses moved-from token }); {