Merge "Switch fastboot/init/libprocessgroup to std::this_thread::sleep_for."

This commit is contained in:
Elliott Hughes 2016-11-15 18:02:30 +00:00 committed by Gerrit Code Review
commit f3e731b3c5
7 changed files with 35 additions and 32 deletions

View file

@ -43,7 +43,9 @@
#include <sys/types.h>
#include <unistd.h>
#include <chrono>
#include <functional>
#include <thread>
#include <utility>
#include <vector>
@ -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));
}
}

View file

@ -43,11 +43,15 @@
#include <linux/version.h>
#include <linux/usb/ch9.h>
#include <chrono>
#include <memory>
#include <thread>
#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;
}

View file

@ -362,9 +362,3 @@ Transport* usb_open(ifc_match_func callback)
std::unique_ptr<usb_handle> handle = find_usb_device(callback);
return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr;
}
// called from fastboot.c
void sleep(int seconds)
{
Sleep(seconds * 1000);
}

View file

@ -38,6 +38,8 @@
#include <linux/loop.h>
#include <linux/module.h>
#include <thread>
#include <selinux/selinux.h>
#include <selinux/label.h>
@ -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. */
@ -749,7 +748,7 @@ static int do_powerctl(const std::vector<std::string>& 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";
}

View file

@ -35,6 +35,7 @@
#include <linux/netlink.h>
#include <memory>
#include <thread>
#include <selinux/selinux.h>
#include <selinux/label.h>
@ -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;
}

View file

@ -34,6 +34,8 @@
#include <sys/types.h>
#include <sys/un.h>
#include <thread>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
@ -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;
}

View file

@ -31,12 +31,15 @@
#include <chrono>
#include <memory>
#include <mutex>
#include <thread>
#include <android-base/logging.h>
#include <private/android_filesystem_config.h>
#include <processgroup/processgroup.h>
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 "