Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: mojo/edk/util/mutex_unittest.cc

Issue 1639093002: Add //mojo/edk/platform/thread_utils.* containing "yield" and "sleep". (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/util/cond_var_unittest.cc ('k') | mojo/edk/util/waitable_event_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/util/mutex.h" 5 #include "mojo/edk/util/mutex.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <thread> 9 #include <thread>
10 10
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "mojo/edk/system/test/sleep.h" 12 #include "mojo/edk/platform/thread_utils.h"
13 #include "mojo/edk/system/test/timeouts.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 using mojo::system::test::SleepMilliseconds; 16 using mojo::platform::ThreadSleep;
17 using mojo::system::test::DeadlineFromMilliseconds;
16 18
17 namespace mojo { 19 namespace mojo {
18 namespace util { 20 namespace util {
19 namespace { 21 namespace {
20 22
21 // Sleeps for a "very small" amount of time. 23 // Sleeps for a "very small" amount of time.
22 void EpsilonRandomSleep() { 24 void EpsilonRandomSleep() {
23 SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); 25 ThreadSleep(DeadlineFromMilliseconds(static_cast<unsigned>(rand()) % 20u));
24 } 26 }
25 27
26 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ---------- 28 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ----------
27 29
28 TEST(MutexTest, Basic) { 30 TEST(MutexTest, Basic) {
29 Mutex mutex; 31 Mutex mutex;
30 32
31 int thread_acquired = 0; 33 int thread_acquired = 0;
32 auto thread = std::thread([&mutex, &thread_acquired]() { 34 auto thread = std::thread([&mutex, &thread_acquired]() {
33 for (int i = 0; i < 10; i++) { 35 for (int i = 0; i < 10; i++) {
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 204
203 // The destruction of |locker| should unlock |mutex|. 205 // The destruction of |locker| should unlock |mutex|.
204 ASSERT_TRUE(mutex.TryLock()); 206 ASSERT_TRUE(mutex.TryLock());
205 mutex.AssertHeld(); 207 mutex.AssertHeld();
206 mutex.Unlock(); 208 mutex.Unlock();
207 } 209 }
208 210
209 } // namespace 211 } // namespace
210 } // namespace util 212 } // namespace util
211 } // namespace mojo 213 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/util/cond_var_unittest.cc ('k') | mojo/edk/util/waitable_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698