]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Use std::this_thread::sleep_for instead of usleep
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 13 Jul 2021 07:49:15 +0000 (09:49 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 19 Jul 2021 06:15:44 +0000 (08:15 +0200)
For portability.

src/Lockfile.cpp
src/Win32Util.cpp
src/Win32Util.hpp

index b0e2a82d25da67bc5f39e7fd21e8ee3697335abc..b1487dab2a049c608db4df3383dac39a7b98d83b 100644 (file)
@@ -27,6 +27,8 @@
 
 #include "third_party/fmt/core.h"
 
+#include <thread>
+
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 #endif
 #include <sstream>
 #include <thread>
 
-// AIX/PASE does not properly define usleep within its headers. However, the
-// function is available in libc.a.
-#ifdef _AIX
-extern "C" int usleep(useconds_t);
-#endif
-
 namespace {
 
 #ifndef _WIN32
@@ -118,7 +114,7 @@ do_acquire_posix(const std::string& lockfile, uint32_t staleness_limit)
       LOG("lockfile_acquire: failed to acquire {}; sleeping {} microseconds",
           lockfile,
           to_sleep);
-      usleep(to_sleep);
+      std::this_thread::sleep_for(std::chrono::microseconds(to_sleep));
       slept += to_sleep;
       to_sleep = std::min(max_to_sleep, 2 * to_sleep);
     } else if (content != initial_content) {
@@ -143,9 +139,9 @@ do_acquire_posix(const std::string& lockfile, uint32_t staleness_limit)
 HANDLE
 do_acquire_win32(const std::string& lockfile, uint32_t staleness_limit)
 {
-  unsigned to_sleep = 1000;      // Microseconds.
-  unsigned max_to_sleep = 10000; // Microseconds.
-  unsigned slept = 0;            // Microseconds.
+  const uint32_t max_to_sleep = 10000; // Microseconds.
+  uint32_t to_sleep = 1000;            // Microseconds.
+  uint32_t slept = 0;                  // Microseconds.
   HANDLE handle;
 
   while (true) {
@@ -190,7 +186,7 @@ do_acquire_win32(const std::string& lockfile, uint32_t staleness_limit)
     LOG("lockfile_acquire: failed to acquire {}; sleeping {} microseconds",
         lockfile,
         to_sleep);
-    usleep(to_sleep);
+    std::this_thread::sleep_for(std::chrono::microseconds(to_sleep));
     slept += to_sleep;
     to_sleep = std::min(max_to_sleep, 2 * to_sleep);
   }
index 1a9de6a0b71ca88f066db8069c6e0cc8a9fb5417..14e3e5b89fe4fdffb10069c7d2604f911397c6ed 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2021 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -143,12 +143,6 @@ gettimeofday(struct timeval* tp, struct timezone* /*tzp*/)
 }
 #endif
 
-void
-usleep(int64_t usec)
-{
-  std::this_thread::sleep_for(std::chrono::microseconds(usec));
-}
-
 struct tm*
 localtime_r(time_t* _clock, struct tm* _result)
 {
index ceeab576ab07897fb34138efaf7523283574f47c..c805dc7d8f41d0d3fdf01dbafc8999e02dac854a 100644 (file)
@@ -24,7 +24,6 @@
 
 #  include <string>
 
-void usleep(int64_t usec);
 struct tm* localtime_r(time_t* _clock, struct tm* _result);
 
 #  ifdef _MSC_VER