diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 4cd423af6..7f4a0dd66 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -43,7 +43,9 @@ #include #include +#include #include +#include #include #include @@ -301,7 +303,7 @@ static Transport* open_device() { announce = false; fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device"); } - usleep(1000); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } diff --git a/fastboot/usb_linux.cpp b/fastboot/usb_linux.cpp index 6db1e2727..cdab4f1d2 100644 --- a/fastboot/usb_linux.cpp +++ b/fastboot/usb_linux.cpp @@ -43,11 +43,15 @@ #include #include +#include #include +#include #include "fastboot.h" #include "usb.h" +using namespace std::chrono_literals; + #define MAX_RETRIES 5 /* Timeout in seconds for usb_wait_for_disconnect. @@ -426,7 +430,7 @@ ssize_t LinuxUsbTransport::Read(void* _data, size_t len) return -1; } - while(len > 0) { + while (len > 0) { int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; bulk.ep = handle_->ep_in; @@ -435,18 +439,17 @@ ssize_t LinuxUsbTransport::Read(void* _data, size_t len) bulk.timeout = 0; retry = 0; - do{ - DBG("[ usb read %d fd = %d], fname=%s\n", xfer, handle_->desc, handle_->fname); - n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk); - DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, handle_->fname, retry); + do { + DBG("[ usb read %d fd = %d], fname=%s\n", xfer, handle_->desc, handle_->fname); + n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk); + DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, handle_->fname, retry); - if( n < 0 ) { - DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); - if ( ++retry > MAX_RETRIES ) return -1; - sleep( 1 ); - } - } - while( n < 0 ); + if (n < 0) { + DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); + if (++retry > MAX_RETRIES) return -1; + std::this_thread::sleep_for(1s); + } + } while (n < 0); count += n; len -= n; @@ -488,9 +491,8 @@ int LinuxUsbTransport::WaitForDisconnect() { double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT; while (now() < deadline) { - if (access(handle_->fname, F_OK)) - return 0; - usleep(50000); + if (access(handle_->fname, F_OK)) return 0; + std::this_thread::sleep_for(50ms); } return -1; } diff --git a/fastboot/usb_windows.cpp b/fastboot/usb_windows.cpp index 1cdeb3279..3dab5ac17 100644 --- a/fastboot/usb_windows.cpp +++ b/fastboot/usb_windows.cpp @@ -362,9 +362,3 @@ Transport* usb_open(ifc_match_func callback) std::unique_ptr handle = find_usb_device(callback); return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr; } - -// called from fastboot.c -void sleep(int seconds) -{ - Sleep(seconds * 1000); -} diff --git a/init/builtins.cpp b/init/builtins.cpp index 852423464..de75438ff 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -38,6 +38,8 @@ #include #include +#include + #include #include @@ -65,7 +67,6 @@ #include "util.h" #define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW -#define UNMOUNT_CHECK_MS 5000 #define UNMOUNT_CHECK_TIMES 10 static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s; @@ -192,11 +193,9 @@ static void unmount_and_fsck(const struct mntent *entry) { close(fd); break; } else if (errno == EBUSY) { - /* Some processes using |entry->mnt_dir| are still alive. Wait for a - * while then retry. - */ - TEMP_FAILURE_RETRY( - usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES)); + // Some processes using |entry->mnt_dir| are still alive. Wait for a + // while then retry. + std::this_thread::sleep_for(5000ms / UNMOUNT_CHECK_TIMES); continue; } else { /* Cannot open the device. Give up. */ @@ -754,7 +753,7 @@ static int do_powerctl(const std::vector& args) { } // Wait a bit before recounting the number or running services. - usleep(50000 /*us*/); + std::this_thread::sleep_for(50ms); } LOG(VERBOSE) << "Terminating running services took " << t.duration() << " seconds"; } diff --git a/init/devices.cpp b/init/devices.cpp index d422ba792..5098fb340 100644 --- a/init/devices.cpp +++ b/init/devices.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -841,7 +842,7 @@ try_loading_again: if (booting) { // If we're not fully booted, we may be missing // filesystems needed for firmware, wait and retry. - usleep(100000); + std::this_thread::sleep_for(100ms); booting = is_booting(); goto try_loading_again; } diff --git a/init/util.cpp b/init/util.cpp index 5288a0557..65b238b29 100644 --- a/init/util.cpp +++ b/init/util.cpp @@ -34,6 +34,8 @@ #include #include +#include + #include #include #include @@ -326,7 +328,7 @@ int wait_for_file(const char* filename, std::chrono::nanoseconds timeout) { struct stat sb; if (stat(filename, &sb) != -1) return 0; - usleep(10000); + std::this_thread::sleep_for(10ms); } return -1; } diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index 7c1542990..eb6672762 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -31,12 +31,15 @@ #include #include #include +#include #include #include #include +using namespace std::chrono_literals; + // Uncomment line below use memory cgroups for keeping track of (forked) PIDs // #define USE_MEMCG 1 @@ -288,7 +291,7 @@ int killProcessGroup(uid_t uid, int initialPid, int signal) while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) { LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid; if (retry > 0) { - usleep(5 * 1000); // 5ms + std::this_thread::sleep_for(5ms); --retry; } else { LOG(ERROR) << "failed to kill " << processes << " processes for processgroup "