[libcxx] r295329 - threading_support: make __thread_sleep_for be alertable

Saleem Abdulrasool via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 16 07:47:45 PST 2017


Author: compnerd
Date: Thu Feb 16 09:47:45 2017
New Revision: 295329

URL: http://llvm.org/viewvc/llvm-project?rev=295329&view=rev
Log:
threading_support: make __thread_sleep_for be alertable

On Windows, we were using `Sleep` which is not alertable.  This means
that if the thread was used for a user APC or WinProc handling and
thread::sleep was used, we could potentially dead lock.  Use `SleepEx`
with an alertable sleep, resuming until the time has expired if we are
awoken early.

Modified:
    libcxx/trunk/include/__threading_support

Modified: libcxx/trunk/include/__threading_support
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__threading_support?rev=295329&r1=295328&r2=295329&view=diff
==============================================================================
--- libcxx/trunk/include/__threading_support (original)
+++ libcxx/trunk/include/__threading_support Thu Feb 16 09:47:45 2017
@@ -589,11 +589,14 @@ void __libcpp_thread_yield()
 
 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
 {
-  using namespace chrono;
+  using namespace _VSTD::chrono;
+
   // round-up to the nearest milisecond
   milliseconds __ms =
       duration_cast<milliseconds>(__ns + chrono::nanoseconds(999999));
-  Sleep(__ms.count());
+  auto start = system_clock::now();
+  while (::SleepEx((__ms - (system_clock::now() - start)).count(),
+                   TRUE) == WAIT_IO_COMPLETION);
 }
 
 // Thread Local Storage




More information about the cfe-commits mailing list