1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/compiler_specific.h" 6 #include "base/threading/platform_thread.h" 7 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace base { 11 12 // Trivial tests that thread runs and doesn't crash on create and join --------- 13 14 class TrivialThread : public PlatformThread::Delegate { 15 public: 16 TrivialThread() : did_run_(false) {} 17 18 virtual void ThreadMain() OVERRIDE { 19 did_run_ = true; 20 } 21 22 bool did_run() const { return did_run_; } 23 24 private: 25 bool did_run_; 26 27 DISALLOW_COPY_AND_ASSIGN(TrivialThread); 28 }; 29 30 TEST(PlatformThreadTest, Trivial) { 31 TrivialThread thread; 32 PlatformThreadHandle handle; 33 34 ASSERT_FALSE(thread.did_run()); 35 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 36 PlatformThread::Join(handle); 37 ASSERT_TRUE(thread.did_run()); 38 } 39 40 TEST(PlatformThreadTest, TrivialTimesTen) { 41 TrivialThread thread[10]; 42 PlatformThreadHandle handle[arraysize(thread)]; 43 44 for (size_t n = 0; n < arraysize(thread); n++) 45 ASSERT_FALSE(thread[n].did_run()); 46 for (size_t n = 0; n < arraysize(thread); n++) 47 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 48 for (size_t n = 0; n < arraysize(thread); n++) 49 PlatformThread::Join(handle[n]); 50 for (size_t n = 0; n < arraysize(thread); n++) 51 ASSERT_TRUE(thread[n].did_run()); 52 } 53 54 // Tests of basic thread functions --------------------------------------------- 55 56 class FunctionTestThread : public TrivialThread { 57 public: 58 FunctionTestThread() : thread_id_(0) {} 59 60 virtual void ThreadMain() OVERRIDE { 61 thread_id_ = PlatformThread::CurrentId(); 62 PlatformThread::YieldCurrentThread(); 63 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); 64 65 // Make sure that the thread ID is the same across calls. 66 EXPECT_EQ(thread_id_, PlatformThread::CurrentId()); 67 68 TrivialThread::ThreadMain(); 69 } 70 71 PlatformThreadId thread_id() const { return thread_id_; } 72 73 private: 74 PlatformThreadId thread_id_; 75 76 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); 77 }; 78 79 TEST(PlatformThreadTest, Function) { 80 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); 81 82 FunctionTestThread thread; 83 PlatformThreadHandle handle; 84 85 ASSERT_FALSE(thread.did_run()); 86 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 87 PlatformThread::Join(handle); 88 ASSERT_TRUE(thread.did_run()); 89 EXPECT_NE(thread.thread_id(), main_thread_id); 90 91 // Make sure that the thread ID is the same across calls. 92 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); 93 } 94 95 TEST(PlatformThreadTest, FunctionTimesTen) { 96 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); 97 98 FunctionTestThread thread[10]; 99 PlatformThreadHandle handle[arraysize(thread)]; 100 101 for (size_t n = 0; n < arraysize(thread); n++) 102 ASSERT_FALSE(thread[n].did_run()); 103 for (size_t n = 0; n < arraysize(thread); n++) 104 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 105 for (size_t n = 0; n < arraysize(thread); n++) 106 PlatformThread::Join(handle[n]); 107 for (size_t n = 0; n < arraysize(thread); n++) { 108 ASSERT_TRUE(thread[n].did_run()); 109 EXPECT_NE(thread[n].thread_id(), main_thread_id); 110 111 // Make sure no two threads get the same ID. 112 for (size_t i = 0; i < n; ++i) { 113 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); 114 } 115 } 116 117 // Make sure that the thread ID is the same across calls. 118 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); 119 } 120 121 } // namespace base 122