Change sleep_for, sleep_until, and the condition_variable timed wait
functions to protect against duration and time_point overflow. Since
we're about to wait anyway, we can afford to spend a few more cycles on
this checking. I purposefully did not treat the timed try_locks with
overflow checking. This fixes
http://llvm.org/bugs/show_bug.cgi?id=13721 . I'm unsure if the standard
needs clarification in this area, or if this is simply QOI. The
<chrono> facilities were never intended to overflow check, but just to
not overflow if durations stayed within +/- 292 years.
llvm-svn: 162925
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: aad745a024a3d0fd573ac9649d4c659e6a713702
diff --git a/include/thread b/include/thread
index d81e853..94e7ab6 100644
--- a/include/thread
+++ b/include/thread
@@ -410,10 +410,20 @@
sleep_for(const chrono::duration<_Rep, _Period>& __d)
{
using namespace chrono;
- nanoseconds __ns = duration_cast<nanoseconds>(__d);
- if (__ns < __d)
- ++__ns;
- sleep_for(__ns);
+ if (__d > duration<_Rep, _Period>::zero())
+ {
+ _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
+ nanoseconds __ns;
+ if (__d < _Max)
+ {
+ __ns = duration_cast<nanoseconds>(__d);
+ if (__ns < __d)
+ ++__ns;
+ }
+ else
+ __ns = nanoseconds::max();
+ sleep_for(__ns);
+ }
}
template <class _Clock, class _Duration>