[libcxx] r210210 - Handle partial nanosleeps in this_thread::sleep_for

David Majnemer david.majnemer at gmail.com
Wed Jun 4 12:43:20 PDT 2014


Author: majnemer
Date: Wed Jun  4 14:43:20 2014
New Revision: 210210

URL: http://llvm.org/viewvc/llvm-project?rev=210210&view=rev
Log:
Handle partial nanosleeps in this_thread::sleep_for

Signals may result in nanosleep returning with only some of the
requested sleeping performed.

Utilize nanosleep's "time-remaining" out parameter to continue sleeping
when this occurs.

Modified:
    libcxx/trunk/src/thread.cpp
    libcxx/trunk/test/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp

Modified: libcxx/trunk/src/thread.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/thread.cpp?rev=210210&r1=210209&r2=210210&view=diff
==============================================================================
--- libcxx/trunk/src/thread.cpp (original)
+++ libcxx/trunk/src/thread.cpp Wed Jun  4 14:43:20 2014
@@ -121,7 +121,9 @@ sleep_for(const chrono::nanoseconds& ns)
             ts.tv_sec = ts_sec_max;
             ts.tv_nsec = giga::num - 1;
         }
-        nanosleep(&ts, 0);
+
+        while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
+            ;
     }
 }
 

Modified: libcxx/trunk/test/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp?rev=210210&r1=210209&r2=210210&view=diff
==============================================================================
--- libcxx/trunk/test/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp (original)
+++ libcxx/trunk/test/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp Wed Jun  4 14:43:20 2014
@@ -15,9 +15,29 @@
 #include <thread>
 #include <cstdlib>
 #include <cassert>
+#include <signal.h>
+#include <sys/time.h>
 
 int main()
 {
+    int ec;
+    struct sigaction action;
+    action.sa_handler = [](int) {};
+    sigemptyset(&action.sa_mask);
+    action.sa_flags = 0;
+
+    ec = sigaction(SIGALRM, &action, nullptr);
+    assert(!ec);
+
+    struct itimerval it;
+    it.it_interval = { 0 };
+    it.it_value.tv_sec = 0;
+    it.it_value.tv_usec = 250000;
+    // This will result in a SIGALRM getting fired resulting in the nanosleep
+    // inside sleep_for getting EINTR.
+    ec = setitimer(ITIMER_REAL, &it, nullptr);
+    assert(!ec);
+
     typedef std::chrono::system_clock Clock;
     typedef Clock::time_point time_point;
     typedef Clock::duration duration;





More information about the cfe-commits mailing list