[Threading] Always round up when converting std::chrono::durations.

We were inadvertently rounding down, which was fine almost 100% of the
time, but on Windows where the timer resolution is only 1ms, it made
the test _very slightly_ flaky because we were asking to wait for
9ms rather than 10ms, then checking we'd waited at least 10ms.

We should explicitly round up, for that reason.

rdar://100236038
This commit is contained in:
Alastair Houghton
2022-09-29 10:31:26 +01:00
parent 0dc768aa35
commit fcf3bfa4a6
6 changed files with 97 additions and 20 deletions

View File

@@ -21,9 +21,10 @@
#include <pthread.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include "chrono.h"
#include "llvm/ADT/Optional.h"
#include "swift/Threading/Errors.h"
@@ -163,14 +164,14 @@ inline void cond_wait(cond_handle &handle) {
template <class Rep, class Period>
inline bool cond_wait(cond_handle &handle,
std::chrono::duration<Rep, Period> duration) {
auto deadline = std::chrono::time_point_cast<
std::chrono::system_clock::duration>(std::chrono::system_clock::now()
+ duration);
auto to_wait = chrono_utils::ceil<
std::chrono::system_clock::duration>(duration);
auto deadline = std::chrono::system_clock::now() + to_wait;
return cond_wait(handle, deadline);
}
inline bool cond_wait(cond_handle &handle,
std::chrono::system_clock::time_point deadline) {
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
auto ns = chrono_utils::ceil<std::chrono::nanoseconds>(
deadline.time_since_epoch()).count();
struct ::timespec ts = { ::time_t(ns / 1000000000), long(ns % 1000000000) };
SWIFT_PTHREADS_RETURN_TRUE_OR_FALSE(