summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2021-07-13 09:49:15 +0200
committerJoel Rosdahl <joel@rosdahl.net>2021-07-19 08:15:44 +0200
commitb1f6d16e4a01cdbb5cce3e7203ef39578edafa79 (patch)
treed21a680d8407ec0bbf989e243c6c2d7b9e042349
parentd068911695250dfa118d0e89ecafc980ad9f6d9b (diff)
downloadccache-b1f6d16e4a01cdbb5cce3e7203ef39578edafa79.tar.gz
Use std::this_thread::sleep_for instead of usleep
For portability.
-rw-r--r--src/Lockfile.cpp18
-rw-r--r--src/Win32Util.cpp8
-rw-r--r--src/Win32Util.hpp1
3 files changed, 8 insertions, 19 deletions
diff --git a/src/Lockfile.cpp b/src/Lockfile.cpp
index b0e2a82d..b1487dab 100644
--- a/src/Lockfile.cpp
+++ b/src/Lockfile.cpp
@@ -27,6 +27,8 @@
#include "third_party/fmt/core.h"
+#include <thread>
+
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
@@ -35,12 +37,6 @@
#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);
}
diff --git a/src/Win32Util.cpp b/src/Win32Util.cpp
index 1a9de6a0..14e3e5b8 100644
--- a/src/Win32Util.cpp
+++ b/src/Win32Util.cpp
@@ -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)
{
diff --git a/src/Win32Util.hpp b/src/Win32Util.hpp
index ceeab576..c805dc7d 100644
--- a/src/Win32Util.hpp
+++ b/src/Win32Util.hpp
@@ -24,7 +24,6 @@
# include <string>
-void usleep(int64_t usec);
struct tm* localtime_r(time_t* _clock, struct tm* _result);
# ifdef _MSC_VER