Add //mojo/edk/platform/thread_utils.* containing "yield" and "sleep".

Remove //mojo/edk/system/test/sleep*.

R=vardhan@google.com

Review URL: https://codereview.chromium.org/1639093002 .
diff --git a/mojo/edk/base_edk/BUILD.gn b/mojo/edk/base_edk/BUILD.gn
index 1a3a9da..c268f4e 100644
--- a/mojo/edk/base_edk/BUILD.gn
+++ b/mojo/edk/base_edk/BUILD.gn
@@ -17,6 +17,7 @@
     "platform_message_loop_impl.h",
     "platform_task_runner_impl.cc",
     "platform_task_runner_impl.h",
+    "thread_utils.cc",
   ]
 
   deps = [
diff --git a/mojo/edk/base_edk/thread_utils.cc b/mojo/edk/base_edk/thread_utils.cc
new file mode 100644
index 0000000..611bffc
--- /dev/null
+++ b/mojo/edk/base_edk/thread_utils.cc
@@ -0,0 +1,36 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file implements the functions declared in
+// //mojo/edk/platform/thread_utils.h.
+
+#include "mojo/edk/platform/thread_utils.h"
+
+#include <stdint.h>
+
+#include <limits>
+
+#include "base/logging.h"
+#include "base/threading/platform_thread.h"
+#include "base/time/time.h"
+
+namespace mojo {
+namespace platform {
+
+void ThreadYield() {
+  base::PlatformThread::YieldCurrentThread();
+}
+
+void ThreadSleep(MojoDeadline duration) {
+  // Note: This also effectively checks that |duration| isn't
+  // |MOJO_DEADLINE_INDEFINITE|.
+  DCHECK_LE(duration,
+            static_cast<MojoDeadline>(std::numeric_limits<int64_t>::max()));
+
+  base::PlatformThread::Sleep(
+      base::TimeDelta::FromMicroseconds(static_cast<int64_t>(duration)));
+}
+
+}  // namespace platform
+}  // namespace mojo
diff --git a/mojo/edk/platform/BUILD.gn b/mojo/edk/platform/BUILD.gn
index f03b712..3a30b7f 100644
--- a/mojo/edk/platform/BUILD.gn
+++ b/mojo/edk/platform/BUILD.gn
@@ -18,6 +18,7 @@
     "scoped_platform_handle.h",
     "task_runner.h",
     "thread.h",
+    "thread_utils.h",
   ]
 
   mojo_sdk_public_deps = [ "mojo/public/cpp/system" ]
@@ -59,6 +60,7 @@
     "aligned_alloc_unittest.cc",
     "io_thread_unittest.cc",
     "test_message_loops_unittest.cc",
+    "thread_utils_unittest.cc",
   ]
 
   deps = [
@@ -67,5 +69,10 @@
     "//testing/gtest",
   ]
 
+  # TODO(vtl): This is a suboptimal dependency. Probably "Stopwatch" should be
+  # moved to :test_platform (which we can do after we add a clock/time function
+  # to :platform).
+  mojo_edk_deps = [ "mojo/edk/system/test" ]
+
   mojo_sdk_deps = [ "mojo/public/cpp/system" ]
 }
diff --git a/mojo/edk/platform/thread_utils.h b/mojo/edk/platform/thread_utils.h
new file mode 100644
index 0000000..0336b86
--- /dev/null
+++ b/mojo/edk/platform/thread_utils.h
@@ -0,0 +1,24 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_EDK_PLATFORM_THREAD_UTILS_H_
+#define MOJO_EDK_PLATFORM_THREAD_UTILS_H_
+
+#include "mojo/public/c/system/types.h"
+
+namespace mojo {
+namespace platform {
+
+// Causes the calling thread to try to yield (allowing another thread to be
+// scheduled).
+void ThreadYield();
+
+// Causes the calling thread to sleep for at least the specified duration (which
+// must not be |MOJO_DEADLINE_INDEFINITE|).
+void ThreadSleep(MojoDeadline duration);
+
+}  // namespace platform
+}  // namespace mojo
+
+#endif  // MOJO_EDK_PLATFORM_THREAD_UTILS_H_
diff --git a/mojo/edk/platform/thread_utils_unittest.cc b/mojo/edk/platform/thread_utils_unittest.cc
new file mode 100644
index 0000000..8d65a4f
--- /dev/null
+++ b/mojo/edk/platform/thread_utils_unittest.cc
@@ -0,0 +1,41 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/edk/platform/thread_utils.h"
+
+#include "mojo/edk/system/test/stopwatch.h"
+#include "mojo/edk/system/test/timeouts.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using mojo::system::test::EpsilonTimeout;
+using mojo::system::test::Stopwatch;
+
+namespace mojo {
+namespace platform {
+namespace {
+
+TEST(ThreadUtils, ThreadYield) {
+  // It's pretty hard to test yield, other than maybe statistically (but tough
+  // even then, since it'd be dependent on the number of cores). So just check
+  // that it doesn't crash.
+  ThreadYield();
+}
+
+TEST(ThreadUtils, ThreadSleep) {
+  Stopwatch stopwatch;
+
+  stopwatch.Start();
+  ThreadSleep(0ULL);
+  EXPECT_LT(stopwatch.Elapsed(), EpsilonTimeout());
+
+  stopwatch.Start();
+  ThreadSleep(2 * EpsilonTimeout());
+  MojoDeadline elapsed = stopwatch.Elapsed();
+  EXPECT_GT(elapsed, EpsilonTimeout());
+  EXPECT_LT(elapsed, 3 * EpsilonTimeout());
+}
+
+}  // namespace
+}  // namespace platform
+}  // namespace mojo
diff --git a/mojo/edk/system/awakable_list_unittest.cc b/mojo/edk/system/awakable_list_unittest.cc
index c2a1fb1..d698b9b 100644
--- a/mojo/edk/system/awakable_list_unittest.cc
+++ b/mojo/edk/system/awakable_list_unittest.cc
@@ -9,13 +9,15 @@
 
 #include "mojo/edk/system/awakable_list.h"
 
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/handle_signals_state.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/waiter.h"
 #include "mojo/edk/system/waiter_test_utils.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
+
 namespace mojo {
 namespace system {
 namespace {
@@ -54,7 +56,7 @@
     test::SimpleWaiterThread thread(&result, &context);
     awakable_list.Add(thread.waiter(), MOJO_HANDLE_SIGNAL_READABLE, 3);
     thread.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     awakable_list.CancelAll();
   }  // Join |thread|.
   EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
@@ -101,7 +103,7 @@
     test::SimpleWaiterThread thread(&result, &context);
     awakable_list.Add(thread.waiter(), MOJO_HANDLE_SIGNAL_READABLE, 3);
     thread.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     awakable_list.AwakeForStateChange(HandleSignalsState(
         MOJO_HANDLE_SIGNAL_READABLE,
         MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE));
@@ -147,7 +149,7 @@
     test::SimpleWaiterThread thread(&result, &context);
     awakable_list.Add(thread.waiter(), MOJO_HANDLE_SIGNAL_READABLE, 3);
     thread.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     awakable_list.AwakeForStateChange(HandleSignalsState(
         MOJO_HANDLE_SIGNAL_NONE, MOJO_HANDLE_SIGNAL_WRITABLE));
     awakable_list.Remove(thread.waiter());
@@ -177,7 +179,7 @@
     test::SimpleWaiterThread thread2(&result2, &context2);
     awakable_list.Add(thread2.waiter(), MOJO_HANDLE_SIGNAL_WRITABLE, 2);
     thread2.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     awakable_list.CancelAll();
   }  // Join threads.
   EXPECT_EQ(MOJO_RESULT_CANCELLED, result1);
@@ -194,7 +196,7 @@
     test::SimpleWaiterThread thread2(&result2, &context2);
     awakable_list.Add(thread2.waiter(), MOJO_HANDLE_SIGNAL_WRITABLE, 4);
     thread2.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     awakable_list.AwakeForStateChange(HandleSignalsState(
         MOJO_HANDLE_SIGNAL_READABLE,
         MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE));
@@ -215,7 +217,7 @@
     test::SimpleWaiterThread thread2(&result2, &context2);
     awakable_list.Add(thread2.waiter(), MOJO_HANDLE_SIGNAL_WRITABLE, 6);
     thread2.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     awakable_list.AwakeForStateChange(HandleSignalsState(
         MOJO_HANDLE_SIGNAL_NONE, MOJO_HANDLE_SIGNAL_READABLE));
     awakable_list.Remove(thread2.waiter());
@@ -233,7 +235,7 @@
     awakable_list.Add(thread1.waiter(), MOJO_HANDLE_SIGNAL_READABLE, 7);
     thread1.Start();
 
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
 
     // Should do nothing.
     awakable_list.AwakeForStateChange(HandleSignalsState(
@@ -244,7 +246,7 @@
     awakable_list.Add(thread2.waiter(), MOJO_HANDLE_SIGNAL_WRITABLE, 8);
     thread2.Start();
 
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
 
     // Awake #1.
     awakable_list.AwakeForStateChange(HandleSignalsState(
@@ -252,7 +254,7 @@
         MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE));
     awakable_list.Remove(thread1.waiter());
 
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
 
     test::SimpleWaiterThread thread3(&result3, &context3);
     awakable_list.Add(thread3.waiter(), MOJO_HANDLE_SIGNAL_WRITABLE, 9);
@@ -262,7 +264,7 @@
     awakable_list.Add(thread4.waiter(), MOJO_HANDLE_SIGNAL_READABLE, 10);
     thread4.Start();
 
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
 
     // Awake #2 and #3 for unsatisfiability.
     awakable_list.AwakeForStateChange(HandleSignalsState(
diff --git a/mojo/edk/system/channel_endpoint.cc b/mojo/edk/system/channel_endpoint.cc
index 8d8ecc1..6de03d3 100644
--- a/mojo/edk/system/channel_endpoint.cc
+++ b/mojo/edk/system/channel_endpoint.cc
@@ -7,11 +7,12 @@
 #include <utility>
 
 #include "base/logging.h"
-#include "base/threading/platform_thread.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/channel.h"
 #include "mojo/edk/system/channel_endpoint_client.h"
 #include "mojo/public/cpp/system/macros.h"
 
+using mojo::platform::ThreadYield;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -211,7 +212,7 @@
       break;
     }
 
-    base::PlatformThread::YieldCurrentThread();
+    ThreadYield();
   }
 }
 
diff --git a/mojo/edk/system/core_unittest.cc b/mojo/edk/system/core_unittest.cc
index 18f3fda..268aadb 100644
--- a/mojo/edk/system/core_unittest.cc
+++ b/mojo/edk/system/core_unittest.cc
@@ -8,11 +8,14 @@
 
 #include <limits>
 
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/awakable.h"
 #include "mojo/edk/system/core_test_base.h"
-#include "mojo/edk/system/test/sleep.h"
+#include "mojo/edk/system/test/timeouts.h"
 #include "mojo/public/cpp/system/macros.h"
 
+using mojo::platform::ThreadSleep;
+
 namespace mojo {
 namespace system {
 namespace {
@@ -29,7 +32,7 @@
   const MojoTimeTicks start = core()->GetTimeTicksNow();
   EXPECT_NE(static_cast<MojoTimeTicks>(0), start)
       << "GetTimeTicksNow should return nonzero value";
-  test::SleepMilliseconds(15u);
+  ThreadSleep(test::DeadlineFromMilliseconds(15u));
   const MojoTimeTicks finish = core()->GetTimeTicksNow();
   // Allow for some fuzz in sleep.
   EXPECT_GE((finish - start), static_cast<MojoTimeTicks>(8000))
diff --git a/mojo/edk/system/data_pipe_impl_unittest.cc b/mojo/edk/system/data_pipe_impl_unittest.cc
index bee13f7..47b2703 100644
--- a/mojo/edk/system/data_pipe_impl_unittest.cc
+++ b/mojo/edk/system/data_pipe_impl_unittest.cc
@@ -15,6 +15,7 @@
 #include "base/logging.h"
 #include "mojo/edk/embedder/platform_channel_pair.h"
 #include "mojo/edk/embedder/simple_platform_support.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/channel.h"
 #include "mojo/edk/system/channel_endpoint.h"
 #include "mojo/edk/system/data_pipe.h"
@@ -23,7 +24,6 @@
 #include "mojo/edk/system/memory.h"
 #include "mojo/edk/system/message_pipe.h"
 #include "mojo/edk/system/raw_channel.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/test_io_thread.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/waiter.h"
@@ -31,6 +31,7 @@
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
 using mojo::util::MakeRefCounted;
 using mojo::util::RefPtr;
 
@@ -1547,7 +1548,7 @@
     if (num_bytes >= 10u * sizeof(int32_t))
       break;
 
-    test::Sleep(test::EpsilonTimeout());
+    ThreadSleep(test::EpsilonTimeout());
   }
   EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
 
@@ -1734,7 +1735,7 @@
       EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, result);
     }
 
-    test::Sleep(test::EpsilonTimeout());
+    ThreadSleep(test::EpsilonTimeout());
   }
   EXPECT_EQ(90u, total_num_bytes);
 
@@ -1747,7 +1748,7 @@
     if (num_bytes >= 100u)
       break;
 
-    test::Sleep(test::EpsilonTimeout());
+    ThreadSleep(test::EpsilonTimeout());
   }
   EXPECT_EQ(100u, num_bytes);
 
@@ -1825,7 +1826,7 @@
     if (num_bytes >= 2u * kTestDataSize)
       break;
 
-    test::Sleep(test::EpsilonTimeout());
+    ThreadSleep(test::EpsilonTimeout());
   }
   EXPECT_EQ(2u * kTestDataSize, num_bytes);
 
@@ -2309,7 +2310,7 @@
 
   // Wait a bit, to make sure that if a signal were (incorrectly) sent, it'd
   // have time to propagate.
-  test::Sleep(test::EpsilonTimeout());
+  ThreadSleep(test::EpsilonTimeout());
 
   // Still no data.
   num_bytes = 1000u;
@@ -2331,7 +2332,7 @@
   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, this->ProducerEndWriteData(0u));
 
   // Wait a bit (as above).
-  test::Sleep(test::EpsilonTimeout());
+  ThreadSleep(test::EpsilonTimeout());
 
   // Still no data.
   num_bytes = 1000u;
@@ -2353,7 +2354,7 @@
   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, this->ProducerEndWriteData(0u));
 
   // Wait a bit (as above).
-  test::Sleep(test::EpsilonTimeout());
+  ThreadSleep(test::EpsilonTimeout());
 
   // Still no data.
   num_bytes = 1000u;
@@ -2469,7 +2470,7 @@
     if (num_bytes >= kTestDataSize)
       break;
 
-    test::Sleep(test::EpsilonTimeout());
+    ThreadSleep(test::EpsilonTimeout());
   }
   EXPECT_EQ(kTestDataSize, num_bytes);
 
@@ -2490,7 +2491,7 @@
   // |ConsumerEndReadData()| below) we want |producer_open()| to be false but
   // the call to |channel_endpoint_->EnqueueMessage()| to fail. (This race can
   // occur without the sleep, but is much less likely.)
-  test::Sleep(10u);
+  ThreadSleep(10u);
 
   EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(num_bytes));
 
diff --git a/mojo/edk/system/message_pipe_dispatcher_unittest.cc b/mojo/edk/system/message_pipe_dispatcher_unittest.cc
index 168ca5d..1a4d6fe 100644
--- a/mojo/edk/system/message_pipe_dispatcher_unittest.cc
+++ b/mojo/edk/system/message_pipe_dispatcher_unittest.cc
@@ -16,10 +16,10 @@
 #include <utility>
 #include <vector>
 
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/message_pipe.h"
 #include "mojo/edk/system/test/random.h"
 #include "mojo/edk/system/test/simple_test_thread.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/stopwatch.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/waiter.h"
@@ -29,6 +29,7 @@
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
 using mojo::util::MakeUnique;
 using mojo::util::RefPtr;
 
@@ -377,7 +378,7 @@
                                 &context, &hss);
       stopwatch.Start();
       thread.Start();
-      test::Sleep(2 * test::EpsilonTimeout());
+      ThreadSleep(2 * test::EpsilonTimeout());
       // Wake it up by writing to |d0|.
       buffer[0] = 123456789;
       EXPECT_EQ(MOJO_RESULT_OK,
@@ -427,7 +428,7 @@
                                 &context, &hss);
       stopwatch.Start();
       thread.Start();
-      test::Sleep(2 * test::EpsilonTimeout());
+      ThreadSleep(2 * test::EpsilonTimeout());
       EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
     }  // Joins the thread.
     elapsed = stopwatch.Elapsed();
@@ -461,7 +462,7 @@
                                 &context, &hss);
       stopwatch.Start();
       thread.Start();
-      test::Sleep(2 * test::EpsilonTimeout());
+      ThreadSleep(2 * test::EpsilonTimeout());
       EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
     }  // Joins the thread.
     elapsed = stopwatch.Elapsed();
diff --git a/mojo/edk/system/message_pipe_test_utils.cc b/mojo/edk/system/message_pipe_test_utils.cc
index c36dd6e..8fded8c 100644
--- a/mojo/edk/system/message_pipe_test_utils.cc
+++ b/mojo/edk/system/message_pipe_test_utils.cc
@@ -6,14 +6,15 @@
 
 #include <utility>
 
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/channel.h"
 #include "mojo/edk/system/channel_endpoint.h"
 #include "mojo/edk/system/message_pipe.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/waiter.h"
 
 using mojo::platform::ScopedPlatformHandle;
+using mojo::platform::ThreadSleep;
 using mojo::util::MakeRefCounted;
 using mojo::util::RefPtr;
 
@@ -66,7 +67,7 @@
     // TODO(vtl): Remove this once |Channel| has a
     // |FlushWriteBufferAndShutdown()| (or whatever).
     while (!channel_->IsWriteBufferEmpty())
-      test::Sleep(test::EpsilonTimeout());
+      ThreadSleep(test::EpsilonTimeout());
 
     test_io_thread_.PostTaskAndWait([this] {
       channel_->Shutdown();
diff --git a/mojo/edk/system/raw_channel_unittest.cc b/mojo/edk/system/raw_channel_unittest.cc
index 26510e5..01f8b0c 100644
--- a/mojo/edk/system/raw_channel_unittest.cc
+++ b/mojo/edk/system/raw_channel_unittest.cc
@@ -15,12 +15,13 @@
 #include "mojo/edk/embedder/platform_channel_pair.h"
 #include "mojo/edk/platform/platform_handle.h"
 #include "mojo/edk/platform/scoped_platform_handle.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/message_in_transit.h"
 #include "mojo/edk/system/test/random.h"
 #include "mojo/edk/system/test/scoped_test_dir.h"
 #include "mojo/edk/system/test/simple_test_thread.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/test_io_thread.h"
+#include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/transport_data.h"
 #include "mojo/edk/test/test_utils.h"
 #include "mojo/edk/util/make_unique.h"
@@ -32,6 +33,7 @@
 
 using mojo::platform::PlatformHandle;
 using mojo::platform::ScopedPlatformHandle;
+using mojo::platform::ThreadSleep;
 using mojo::util::AutoResetWaitableEvent;
 using mojo::util::MakeUnique;
 using mojo::util::Mutex;
@@ -173,7 +175,7 @@
 
       if (static_cast<size_t>(read_size) < sizeof(buffer)) {
         i++;
-        test::SleepMilliseconds(kMessageReaderSleepMs);
+        ThreadSleep(test::DeadlineFromMilliseconds(kMessageReaderSleepMs));
       }
     }
 
@@ -400,7 +402,7 @@
 
   // Sleep a bit, to let any extraneous reads be processed. (There shouldn't be
   // any, but we want to know about them.)
-  test::SleepMilliseconds(100u);
+  ThreadSleep(test::DeadlineFromMilliseconds(100u));
 
   // Wait for reading to finish.
   reader_delegate.Wait();
@@ -488,7 +490,7 @@
 
   // Sleep a bit, to make sure we don't get another |OnError()|
   // notification. (If we actually get another one, |OnError()| crashes.)
-  test::SleepMilliseconds(20u);
+  ThreadSleep(test::DeadlineFromMilliseconds(20u));
 
   io_thread()->PostTaskAndWait([&rc]() { rc->Shutdown(); });
 }
diff --git a/mojo/edk/system/remote_message_pipe_unittest.cc b/mojo/edk/system/remote_message_pipe_unittest.cc
index 0963c35..25097e5 100644
--- a/mojo/edk/system/remote_message_pipe_unittest.cc
+++ b/mojo/edk/system/remote_message_pipe_unittest.cc
@@ -15,6 +15,7 @@
 #include "mojo/edk/embedder/platform_shared_buffer.h"
 #include "mojo/edk/embedder/simple_platform_support.h"
 #include "mojo/edk/platform/scoped_platform_handle.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/channel.h"
 #include "mojo/edk/system/channel_endpoint.h"
 #include "mojo/edk/system/channel_endpoint_id.h"
@@ -25,7 +26,6 @@
 #include "mojo/edk/system/raw_channel.h"
 #include "mojo/edk/system/shared_buffer_dispatcher.h"
 #include "mojo/edk/system/test/scoped_test_dir.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/test_io_thread.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/waiter.h"
@@ -36,6 +36,7 @@
 #include "testing/gtest/include/gtest/gtest.h"
 
 using mojo::platform::ScopedPlatformHandle;
+using mojo::platform::ThreadSleep;
 using mojo::util::MakeRefCounted;
 using mojo::util::RefPtr;
 
@@ -1125,18 +1126,18 @@
     BootstrapChannelEndpointNoWait(1, std::move(ep1));
 
     if (i & 1u) {
-      io_thread()->PostTask([delay]() { test::Sleep(delay); });
+      io_thread()->PostTask([delay]() { ThreadSleep(delay); });
     }
     if (i & 2u)
-      test::Sleep(delay);
+      ThreadSleep(delay);
 
     mp0->Close(0);
 
     if (i & 4u) {
-      io_thread()->PostTask([delay]() { test::Sleep(delay); });
+      io_thread()->PostTask([delay]() { ThreadSleep(delay); });
     }
     if (i & 8u)
-      test::Sleep(delay);
+      ThreadSleep(delay);
 
     mp1->Close(1);
 
diff --git a/mojo/edk/system/simple_dispatcher_unittest.cc b/mojo/edk/system/simple_dispatcher_unittest.cc
index 305dcde..ae84b52 100644
--- a/mojo/edk/system/simple_dispatcher_unittest.cc
+++ b/mojo/edk/system/simple_dispatcher_unittest.cc
@@ -13,7 +13,7 @@
 #include <vector>
 
 #include "base/logging.h"
-#include "mojo/edk/system/test/sleep.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/test/stopwatch.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/system/waiter.h"
@@ -24,6 +24,7 @@
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
 using mojo::util::MakeRefCounted;
 using mojo::util::MakeUnique;
 using mojo::util::MutexLocker;
@@ -364,7 +365,7 @@
                                 &context, &hss);
       stopwatch.Start();
       thread.Start();
-      test::Sleep(2 * test::EpsilonTimeout());
+      ThreadSleep(2 * test::EpsilonTimeout());
       d->SetSatisfiedSignals(MOJO_HANDLE_SIGNAL_READABLE);
     }  // Joins the thread.
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
@@ -388,7 +389,7 @@
                                 &context, &hss);
       stopwatch.Start();
       thread.Start();
-      test::Sleep(2 * test::EpsilonTimeout());
+      ThreadSleep(2 * test::EpsilonTimeout());
       d->SetSatisfiableSignals(MOJO_HANDLE_SIGNAL_NONE);
     }  // Joins the thread.
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
@@ -410,7 +411,7 @@
                               &context, &hss);
     stopwatch.Start();
     thread.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
   }  // Joins the thread.
   elapsed = stopwatch.Elapsed();
@@ -431,7 +432,7 @@
                                 &result, &context, &hss);
       stopwatch.Start();
       thread.Start();
-      test::Sleep(1 * test::EpsilonTimeout());
+      ThreadSleep(1 * test::EpsilonTimeout());
       // Not what we're waiting for.
       d->SetSatisfiedSignals(MOJO_HANDLE_SIGNAL_WRITABLE);
     }  // Joins the thread (after its wait times out).
@@ -466,7 +467,7 @@
           &did_wait[i], &result[i], &context[i], &hss[i]));
       threads.back()->Start();
     }
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     d->SetSatisfiedSignals(MOJO_HANDLE_SIGNAL_READABLE);
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
   }  // Joins the threads.
@@ -495,7 +496,7 @@
           &did_wait[i], &result[i], &context[i], &hss[i]));
       threads.back()->Start();
     }
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     d->SetSatisfiedSignals(MOJO_HANDLE_SIGNAL_READABLE);
     // This will wake up the ones waiting to write.
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
@@ -532,9 +533,9 @@
           &did_wait[i], &result[i], &context[i], &hss[i]));
       threads.back()->Start();
     }
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
     d->SetSatisfiableSignals(MOJO_HANDLE_SIGNAL_READABLE);
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
     d->SetSatisfiedSignals(MOJO_HANDLE_SIGNAL_READABLE);
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
   }  // Joins the threads.
@@ -570,7 +571,7 @@
           &did_wait[i], &result[i], &context[i], &hss[i]));
       threads.back()->Start();
     }
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     d->SetSatisfiedSignals(MOJO_HANDLE_SIGNAL_READABLE);
     // All those waiting for writable should have timed out.
     EXPECT_EQ(MOJO_RESULT_OK, d->Close());
diff --git a/mojo/edk/system/test/BUILD.gn b/mojo/edk/system/test/BUILD.gn
index f90b4dc..69acdf1 100644
--- a/mojo/edk/system/test/BUILD.gn
+++ b/mojo/edk/system/test/BUILD.gn
@@ -16,8 +16,6 @@
     "scoped_test_dir.h",
     "simple_test_thread.cc",
     "simple_test_thread.h",
-    "sleep.cc",
-    "sleep.h",
     "stopwatch.cc",
     "stopwatch.h",
     "test_command_line.cc",
@@ -95,7 +93,6 @@
 mojo_edk_unittests("mojo_edk_system_test_unittests") {
   sources = [
     "random_unittest.cc",
-    "sleep_unittest.cc",
   ]
 
   deps = [
diff --git a/mojo/edk/system/test/sleep.cc b/mojo/edk/system/test/sleep.cc
deleted file mode 100644
index 45f0587..0000000
--- a/mojo/edk/system/test/sleep.cc
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/edk/system/test/sleep.h"
-
-#include <errno.h>
-#include <stdint.h>
-#include <time.h>
-
-#include <limits>
-
-#include "base/logging.h"
-#include "mojo/edk/system/test/timeouts.h"
-
-namespace mojo {
-namespace system {
-namespace test {
-
-void Sleep(MojoDeadline duration) {
-  // TODO(vtl): This doesn't handle |MOJO_DEADLINE_INDEFINITE|. Should it?
-  DCHECK_NE(duration, MOJO_DEADLINE_INDEFINITE);
-
-  const uint64_t kMicrosecondsPerSecond = 1000000ULL;
-  const uint64_t kNanosecondsPerMicrosecond = 1000ULL;
-
-  uint64_t sleep_time_seconds = duration / kMicrosecondsPerSecond;
-  // |sleep_time.tv_sec| is a |time_t|.
-  DCHECK_LE(sleep_time_seconds,
-            static_cast<uint64_t>(std::numeric_limits<time_t>::max()));
-  uint64_t sleep_time_nanoseconds =
-      (duration % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond;
-
-  struct timespec sleep_time;
-  sleep_time.tv_sec = static_cast<time_t>(sleep_time_seconds);
-  sleep_time.tv_nsec = static_cast<long>(sleep_time_nanoseconds);
-
-  struct timespec sleep_time_remaining;
-  while (nanosleep(&sleep_time, &sleep_time_remaining) == -1) {
-    PCHECK(errno == EINTR) << "nanosleep";
-    sleep_time = sleep_time_remaining;
-  }
-}
-
-void SleepMilliseconds(unsigned duration_milliseconds) {
-  Sleep(DeadlineFromMilliseconds(duration_milliseconds));
-}
-
-}  // namespace test
-}  // namespace system
-}  // namespace mojo
diff --git a/mojo/edk/system/test/sleep.h b/mojo/edk/system/test/sleep.h
deleted file mode 100644
index ff8e4f0..0000000
--- a/mojo/edk/system/test/sleep.h
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Function for sleeping in tests. (Please use sparingly.)
-
-#ifndef MOJO_EDK_SYSTEM_TEST_SLEEP_H_
-#define MOJO_EDK_SYSTEM_TEST_SLEEP_H_
-
-#include "mojo/public/c/system/types.h"
-
-namespace mojo {
-namespace system {
-namespace test {
-
-// Sleeps for at least the specified duration.
-void Sleep(MojoDeadline duration);
-void SleepMilliseconds(unsigned duration_milliseconds);
-
-}  // namespace test
-}  // namespace system
-}  // namespace mojo
-
-#endif  // MOJO_EDK_SYSTEM_TEST_SLEEP_H_
diff --git a/mojo/edk/system/test/sleep_unittest.cc b/mojo/edk/system/test/sleep_unittest.cc
deleted file mode 100644
index f95cfec..0000000
--- a/mojo/edk/system/test/sleep_unittest.cc
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/edk/system/test/sleep.h"
-
-#include "mojo/edk/system/test/stopwatch.h"
-#include "mojo/edk/system/test/timeouts.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace system {
-namespace test {
-namespace {
-
-TEST(SleepTest, Sleep) {
-  Stopwatch stopwatch;
-
-  stopwatch.Start();
-  Sleep(0ULL);
-  EXPECT_LT(stopwatch.Elapsed(), EpsilonTimeout());
-
-  stopwatch.Start();
-  Sleep(2 * EpsilonTimeout());
-  MojoDeadline elapsed = stopwatch.Elapsed();
-  EXPECT_GT(elapsed, EpsilonTimeout());
-  EXPECT_LT(elapsed, 3 * EpsilonTimeout());
-}
-
-TEST(SleepTest, SleepMilliseconds) {
-  Stopwatch stopwatch;
-
-  stopwatch.Start();
-  SleepMilliseconds(0U);
-  EXPECT_LT(stopwatch.Elapsed(), EpsilonTimeout());
-
-  const MojoDeadline kMillisecondsPerMicrosecond = 1000ULL;
-  unsigned epsilon_ms =
-      static_cast<unsigned>(EpsilonTimeout() / kMillisecondsPerMicrosecond);
-  stopwatch.Start();
-  SleepMilliseconds(2 * epsilon_ms);
-  MojoDeadline elapsed = stopwatch.Elapsed();
-  EXPECT_GT(elapsed, EpsilonTimeout());
-  EXPECT_LT(elapsed, 3 * EpsilonTimeout());
-}
-
-}  // namespace
-}  // namespace test
-}  // namespace system
-}  // namespace mojo
diff --git a/mojo/edk/system/waiter_unittest.cc b/mojo/edk/system/waiter_unittest.cc
index 9a344f5..c5c10d9 100644
--- a/mojo/edk/system/waiter_unittest.cc
+++ b/mojo/edk/system/waiter_unittest.cc
@@ -11,14 +11,15 @@
 
 #include <stdint.h>
 
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/test/simple_test_thread.h"
-#include "mojo/edk/system/test/sleep.h"
 #include "mojo/edk/system/test/stopwatch.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/util/mutex.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
 using mojo::util::Mutex;
 using mojo::util::MutexLocker;
 
@@ -54,7 +55,7 @@
         }
       }
 
-      test::SleepMilliseconds(kPollTimeMs);
+      ThreadSleep(test::DeadlineFromMilliseconds(kPollTimeMs));
     }
   }
 
@@ -125,7 +126,7 @@
   {
     WaitingThread thread(10 * test::EpsilonTimeout());
     thread.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     thread.waiter()->Awake(1, 3);
     thread.WaitUntilDone(&result, &context, &elapsed);
     EXPECT_EQ(1u, result);
@@ -138,7 +139,7 @@
   {
     WaitingThread thread(10 * test::EpsilonTimeout());
     thread.Start();
-    test::Sleep(5 * test::EpsilonTimeout());
+    ThreadSleep(5 * test::EpsilonTimeout());
     thread.waiter()->Awake(2, 4);
     thread.WaitUntilDone(&result, &context, &elapsed);
     EXPECT_EQ(2u, result);
@@ -186,7 +187,7 @@
   {
     WaitingThread thread(MOJO_DEADLINE_INDEFINITE);
     thread.Start();
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     thread.waiter()->Awake(1, 7);
     thread.WaitUntilDone(&result, &context, &elapsed);
     EXPECT_EQ(1u, result);
@@ -199,7 +200,7 @@
   {
     WaitingThread thread(MOJO_DEADLINE_INDEFINITE);
     thread.Start();
-    test::Sleep(5 * test::EpsilonTimeout());
+    ThreadSleep(5 * test::EpsilonTimeout());
     thread.waiter()->Awake(2, 8);
     thread.WaitUntilDone(&result, &context, &elapsed);
     EXPECT_EQ(2u, result);
@@ -274,7 +275,7 @@
     WaitingThread thread(MOJO_DEADLINE_INDEFINITE);
     thread.Start();
     thread.waiter()->Awake(10, 5);
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     thread.waiter()->Awake(20, 6);
     thread.WaitUntilDone(&result, &context, &elapsed);
     EXPECT_EQ(10u, result);
@@ -285,9 +286,9 @@
   {
     WaitingThread thread(10 * test::EpsilonTimeout());
     thread.Start();
-    test::Sleep(1 * test::EpsilonTimeout());
+    ThreadSleep(1 * test::EpsilonTimeout());
     thread.waiter()->Awake(MOJO_RESULT_FAILED_PRECONDITION, 7);
-    test::Sleep(2 * test::EpsilonTimeout());
+    ThreadSleep(2 * test::EpsilonTimeout());
     thread.waiter()->Awake(MOJO_RESULT_OK, 8);
     thread.WaitUntilDone(&result, &context, &elapsed);
     EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
diff --git a/mojo/edk/util/cond_var_unittest.cc b/mojo/edk/util/cond_var_unittest.cc
index ab966f1..4361a73 100644
--- a/mojo/edk/util/cond_var_unittest.cc
+++ b/mojo/edk/util/cond_var_unittest.cc
@@ -11,16 +11,16 @@
 #include <type_traits>
 #include <vector>
 
-#include "mojo/edk/system/test/sleep.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/test/stopwatch.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/edk/util/mutex.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
 using mojo::system::test::DeadlineFromMilliseconds;
 using mojo::system::test::EpsilonTimeout;
-using mojo::system::test::SleepMilliseconds;
 using mojo::system::test::Stopwatch;
 using mojo::system::test::TinyTimeout;
 
@@ -30,7 +30,7 @@
 
 // Sleeps for a "very small" amount of time.
 void EpsilonRandomSleep() {
-  SleepMilliseconds(static_cast<unsigned>(rand()) % 20u);
+  ThreadSleep(DeadlineFromMilliseconds(static_cast<unsigned>(rand()) % 20u));
 }
 
 // We'll use |MojoDeadline| with |uint64_t| (for |CondVar::WaitWithTimeout()|'s
diff --git a/mojo/edk/util/mutex_unittest.cc b/mojo/edk/util/mutex_unittest.cc
index 3a69d41..90656ca 100644
--- a/mojo/edk/util/mutex_unittest.cc
+++ b/mojo/edk/util/mutex_unittest.cc
@@ -9,10 +9,12 @@
 #include <thread>
 
 #include "build/build_config.h"
-#include "mojo/edk/system/test/sleep.h"
+#include "mojo/edk/platform/thread_utils.h"
+#include "mojo/edk/system/test/timeouts.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
-using mojo::system::test::SleepMilliseconds;
+using mojo::platform::ThreadSleep;
+using mojo::system::test::DeadlineFromMilliseconds;
 
 namespace mojo {
 namespace util {
@@ -20,7 +22,7 @@
 
 // Sleeps for a "very small" amount of time.
 void EpsilonRandomSleep() {
-  SleepMilliseconds(static_cast<unsigned>(rand()) % 20u);
+  ThreadSleep(DeadlineFromMilliseconds(static_cast<unsigned>(rand()) % 20u));
 }
 
 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ----------
diff --git a/mojo/edk/util/waitable_event_unittest.cc b/mojo/edk/util/waitable_event_unittest.cc
index 4db285c..3777613 100644
--- a/mojo/edk/util/waitable_event_unittest.cc
+++ b/mojo/edk/util/waitable_event_unittest.cc
@@ -12,17 +12,16 @@
 #include <type_traits>
 #include <vector>
 
-#include "mojo/edk/system/test/sleep.h"
+#include "mojo/edk/platform/thread_utils.h"
 #include "mojo/edk/system/test/stopwatch.h"
 #include "mojo/edk/system/test/timeouts.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::platform::ThreadSleep;
 using mojo::system::test::ActionTimeout;
 using mojo::system::test::DeadlineFromMilliseconds;
 using mojo::system::test::EpsilonTimeout;
-using mojo::system::test::Sleep;
-using mojo::system::test::SleepMilliseconds;
 using mojo::system::test::Stopwatch;
 using mojo::system::test::TinyTimeout;
 
@@ -32,7 +31,7 @@
 
 // Sleeps for a "very small" amount of time.
 void EpsilonRandomSleep() {
-  SleepMilliseconds(static_cast<unsigned>(rand()) % 20u);
+  ThreadSleep(DeadlineFromMilliseconds(static_cast<unsigned>(rand()) % 20u));
 }
 
 // We'll use |MojoDeadline| with |uint64_t| (for |WaitWithTimeout()|'s timeout
@@ -92,7 +91,7 @@
     // Unfortunately, we can't really wait for the threads to be waiting, so we
     // just sleep for a bit, and count on them having started and advanced to
     // waiting.
-    Sleep(2 * TinyTimeout());
+    ThreadSleep(2 * TinyTimeout());
 
     for (size_t j = 0u; j < threads.size(); j++) {
       unsigned old_wake_count = wake_count.load();
@@ -103,13 +102,13 @@
 
       // Poll for |wake_count| to change.
       while (wake_count.load() == old_wake_count)
-        Sleep(EpsilonTimeout());
+        ThreadSleep(EpsilonTimeout());
 
       EXPECT_FALSE(ev.IsSignaledForTest());
 
       // And once it's changed, wait a little longer, to see if any other
       // threads are awoken (they shouldn't be).
-      Sleep(EpsilonTimeout());
+      ThreadSleep(EpsilonTimeout());
 
       EXPECT_EQ(old_wake_count + 1u, wake_count.load());
 
@@ -118,7 +117,7 @@
 
     // Having done that, if we signal |ev| now, it should stay signaled.
     ev.Signal();
-    Sleep(EpsilonTimeout());
+    ThreadSleep(EpsilonTimeout());
     EXPECT_TRUE(ev.IsSignaledForTest());
 
     for (auto& thread : threads)
@@ -223,7 +222,7 @@
     // Unfortunately, we can't really wait for the threads to be waiting, so we
     // just sleep for a bit, and count on them having started and advanced to
     // waiting.
-    Sleep(2 * TinyTimeout());
+    ThreadSleep(2 * TinyTimeout());
 
     ev.Signal();