AU: replace sleep/usleep with g_usleep

Also deploying 'using base::TimeDelta' where it is useful.

BUG=chromium-os:33541
TEST=Passes unit tests; update runs on x86-alex

Change-Id: I9478d46d0796b13789878393e3a4086564124d33
Reviewed-on: https://gerrit.chromium.org/gerrit/40904
Commit-Queue: Gilad Arnold <[email protected]>
Reviewed-by: Gilad Arnold <[email protected]>
Tested-by: Gilad Arnold <[email protected]>
diff --git a/gpio_handler.cc b/gpio_handler.cc
index caf7679..f49a555 100644
--- a/gpio_handler.cc
+++ b/gpio_handler.cc
@@ -7,6 +7,8 @@
 #include <base/memory/scoped_ptr.h>
 #include <base/string_util.h>
 #include <base/stringprintf.h>
+#include <base/time.h>
+#include <glib.h>
 
 #include "update_engine/file_descriptor.h"
 
@@ -530,7 +532,7 @@
   }
 
   // Wait, giving the receiving end enough time to sense the fall.
-  sleep(kServoOutputResponseWaitInSecs);
+  g_usleep(kServoOutputResponseWaitInSecs * G_USEC_PER_SEC);
 
   // Flip the output signal.
   if (!SetGpioValue(kGpioIdDutflagb, kGpioValDown, false)) {
@@ -549,7 +551,7 @@
     if (is_first_response_check)
       is_first_response_check = false;
     else
-      usleep(delay.InMicroseconds());
+      g_usleep(delay.InMicroseconds());
 
     // Read input GPIO.
     if (!GetGpioValue(kGpioIdDutflaga, &dutflaga_gpio_value, true)) {
diff --git a/gpio_handler.h b/gpio_handler.h
index 1e25009..c71043a 100644
--- a/gpio_handler.h
+++ b/gpio_handler.h
@@ -9,8 +9,6 @@
 
 #include <string>
 
-#include <base/time.h>
-
 #include "update_engine/file_descriptor.h"
 #include "update_engine/udev_interface.h"
 
diff --git a/http_fetcher_unittest.cc b/http_fetcher_unittest.cc
index 09b8999..4fb8903 100644
--- a/http_fetcher_unittest.cc
+++ b/http_fetcher_unittest.cc
@@ -12,6 +12,7 @@
 #include <base/memory/scoped_ptr.h>
 #include <base/string_util.h>
 #include <base/stringprintf.h>
+#include <base/time.h>
 #include <chromeos/dbus/service_constants.h>
 #include <glib.h>
 #include <gtest/gtest.h>
@@ -31,6 +32,7 @@
 using std::string;
 using std::vector;
 
+using base::TimeDelta;
 using testing::_;
 using testing::SetArgumentPointee;
 using testing::DoAll;
@@ -97,17 +99,14 @@
     }
     LOG(INFO) << "started http server with pid " << pid_;
     int rc = 1;
-    const uint64_t kMaxSleep = 60UL * 60UL * 1000UL * 1000UL;  // 60 min
-    uint64_t timeout = 15 * 1000;  // 15 ms
+    const TimeDelta kMaxSleep = TimeDelta::FromMinutes(60);
+    TimeDelta timeout = TimeDelta::FromMilliseconds(15);
     started_ = true;
     while (rc && timeout < kMaxSleep) {
       // Wait before the first attempt also as it takes a while for the
       // test_http_server to be ready.
-      LOG(INFO) << "waiting for " << timeout / 1000 << " ms";
-      if (timeout < (1000 * 1000))  // sub 1-second sleep, use usleep
-        usleep(static_cast<useconds_t>(timeout));
-      else
-        sleep(static_cast<unsigned int>(timeout / (1000 * 1000)));
+      LOG(INFO) << "waiting for " << utils::FormatTimeDelta(timeout);
+      g_usleep(timeout.InMicroseconds());
       timeout *= 2;
 
       LOG(INFO) << "running wget to start";
diff --git a/subprocess_unittest.cc b/subprocess_unittest.cc
index ae67fbb..d23040d 100644
--- a/subprocess_unittest.cc
+++ b/subprocess_unittest.cc
@@ -5,16 +5,21 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+
 #include <string>
 #include <vector>
-#include "base/string_util.h"
+
+#include <base/string_util.h>
 #include <base/stringprintf.h>
+#include <base/time.h>
 #include <glib.h>
 #include <gtest/gtest.h>
+
 #include "update_engine/subprocess.h"
 #include "update_engine/test_utils.h"
 #include "update_engine/utils.h"
 
+using base::TimeDelta;
 using std::string;
 using std::vector;
 
@@ -114,8 +119,9 @@
   cancel_test_data->spawned = true;
   printf("spawned\n");
   // Wait for server to be up and running
-  useconds_t total_wait_time = 0;
-  const useconds_t kMaxWaitTime = 3 * 1000000;  // 3 seconds
+  TimeDelta total_wait_time;
+  const TimeDelta kSleepTime = TimeDelta::FromMilliseconds(100);
+  const TimeDelta kMaxWaitTime = TimeDelta::FromSeconds(3);
   for (;;) {
     int status =
         System(StringPrintf("wget -O /dev/null http://127.0.0.1:%d/foo",
@@ -126,10 +132,9 @@
     if (0 == WEXITSTATUS(status))
       break;
 
-    const useconds_t kSleepTime = 100 * 1000;  // 100ms
-    usleep(kSleepTime);  // 100 ms
+    g_usleep(kSleepTime.InMicroseconds());
     total_wait_time += kSleepTime;
-    CHECK_LT(total_wait_time, kMaxWaitTime);
+    CHECK_LT(total_wait_time.InMicroseconds(), kMaxWaitTime.InMicroseconds());
   }
   Subprocess::Get().CancelExec(tag);
   return FALSE;
diff --git a/update_engine_client.cc b/update_engine_client.cc
index f8396c9..9d5767c 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -50,7 +50,7 @@
     if (i > 0) {
       LOG(INFO) << "Retrying to get dbus proxy. Try "
                 << (i + 1) << "/" << kTries;
-      sleep(kRetrySeconds);
+      g_usleep(kRetrySeconds * G_USEC_PER_SEC);
     }
     proxy = dbus_g_proxy_new_for_name_owner(bus,
                                             kUpdateEngineServiceName,
diff --git a/utils.cc b/utils.cc
index 5a1d812..e5d7290 100644
--- a/utils.cc
+++ b/utils.cc
@@ -27,6 +27,7 @@
 #include <base/string_number_conversions.h>
 #include <base/string_util.h>
 #include <base/stringprintf.h>
+#include <glib.h>
 #include <google/protobuf/stubs/common.h>
 #include <rootdev/rootdev.h>
 
@@ -35,6 +36,7 @@
 #include "update_engine/subprocess.h"
 
 using base::Time;
+using base::TimeDelta;
 using std::min;
 using std::string;
 using std::vector;
@@ -517,7 +519,7 @@
 
     TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY &&
                                 num_retries < kUnmountMaxNumOfRetries);
-    usleep(kUnmountRetryIntervalInMicroseconds);
+    g_usleep(kUnmountRetryIntervalInMicroseconds);
   }
   return true;
 }
@@ -661,19 +663,19 @@
 }
 
 string FormatSecs(unsigned secs) {
-  return FormatTimeDelta(base::TimeDelta::FromSeconds(secs));
+  return FormatTimeDelta(TimeDelta::FromSeconds(secs));
 }
 
-string FormatTimeDelta(base::TimeDelta delta) {
+string FormatTimeDelta(TimeDelta delta) {
   // Canonicalize into days, hours, minutes, seconds and microseconds.
   unsigned days = delta.InDays();
-  delta -= base::TimeDelta::FromDays(days);
+  delta -= TimeDelta::FromDays(days);
   unsigned hours = delta.InHours();
-  delta -= base::TimeDelta::FromHours(hours);
+  delta -= TimeDelta::FromHours(hours);
   unsigned mins = delta.InMinutes();
-  delta -= base::TimeDelta::FromMinutes(mins);
+  delta -= TimeDelta::FromMinutes(mins);
   unsigned secs = delta.InSeconds();
-  delta -= base::TimeDelta::FromSeconds(secs);
+  delta -= TimeDelta::FromSeconds(secs);
   unsigned usecs = delta.InMicroseconds();
 
   // Construct and return string.