Patchwork fix libstdc++/55741 - use Sleep on mingw

login
register
mail settings
Submitter Jonathan Wakely
Date Dec. 20, 2012, 2:37 p.m.
Message ID <CAH6eHdTREqk29nriMhrTqSQwuKRwVkXD2HXnhTTUw1J_-kg0Yw@mail.gmail.com>
Download mbox | patch
Permalink /patch/207674/
State New
Headers show

Comments

Jonathan Wakely - Dec. 20, 2012, 2:37 p.m.
PR libstdc++/55741
        * acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Check for Sleep.
        * config.h.in: Regenerate.
        * configure: Regenerate.
        * src/c++11/thread.cc (__sleep_for): Use Sleep if available.

Tested by Kai (thanks), committed to trunk.
commit 1149c65a987eba50ad0138a48729b020e7d8d0bd
Author: Jonathan Wakely <jwakely.gcc@gmail.com>
Date:   Thu Dec 20 14:29:54 2012 +0000

    	PR libstdc++/55741
    	* acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Check for Sleep.
    	* config.h.in: Regenerate.
    	* configure: Regenerate.
    	* src/c++11/thread.cc (__sleep_for): Use Sleep if available.

Patch

diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 281ee7e..2d4d7f0 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -1301,6 +1301,17 @@  AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME], [
       AC_MSG_RESULT($ac_has_usleep)
   fi
 
+  if test x"$ac_has_nanosleep$ac_has_sleep" = x"nono"; then
+      AC_MSG_CHECKING([for Sleep])
+      AC_TRY_COMPILE([#include <windows.h>],
+                     [Sleep(1)],
+                     [ac_has_win32_sleep=yes],[ac_has_win32_sleep=no])
+      if test x"$ac_has_win32_sleep" = x"yes"; then
+        AC_DEFINE(HAVE_WIN32_SLEEP,1, [Defined if Sleep exists.])
+      fi
+      AC_MSG_RESULT($ac_has_win32_sleep)
+  fi
+
   AC_SUBST(GLIBCXX_LIBS)
 
   CXXFLAGS="$ac_save_CXXFLAGS"
diff --git a/libstdc++-v3/src/c++11/thread.cc b/libstdc++-v3/src/c++11/thread.cc
index fa86a1b..b04e6dc 100644
--- a/libstdc++-v3/src/c++11/thread.cc
+++ b/libstdc++-v3/src/c++11/thread.cc
@@ -61,6 +61,8 @@  static inline int get_nprocs()
 #ifndef _GLIBCXX_USE_NANOSLEEP
 # ifdef _GLIBCXX_HAVE_SLEEP
 #  include <unistd.h>
+# elif defined(_GLIBCXX_HAVE_WIN32_SLEEP)
+#  include <windows.h>
 # else
 #  error "No sleep function known for this target"
 # endif
@@ -170,9 +172,8 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	static_cast<long>(__ns.count())
       };
     ::nanosleep(&__ts, 0);
-#else
-# ifdef _GLIBCXX_HAVE_SLEEP
-#  ifdef _GLIBCXX_HAVE_USLEEP
+#elif defined(_GLIBCXX_HAVE_SLEEP)
+# ifdef _GLIBCXX_HAVE_USLEEP
     ::sleep(__s.count());
     if (__ns.count() > 0)
       {
@@ -181,10 +182,14 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
           __us = 1;
         ::usleep(__us);
       }
-#  else
+# else
     ::sleep(__s.count() + (__ns >= 1000000));
-#  endif
 # endif
+#elif defined(_GLIBCXX_HAVE_WIN32_SLEEP)
+    unsigned long ms = __ns.count() / 1000000;
+    if (__ns.count() > 0 && ms == 0)
+      ms = 1;
+    ::Sleep(chrono::milliseconds(__s).count() + ms);
 #endif
   }