| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/thread.h" | 5 #include "base/thread.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 } | 28 } |
| 29 private: | 29 private: |
| 30 bool* value_; | 30 bool* value_; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 class SleepSome : public Task { | 33 class SleepSome : public Task { |
| 34 public: | 34 public: |
| 35 explicit SleepSome(int msec) : msec_(msec) { | 35 explicit SleepSome(int msec) : msec_(msec) { |
| 36 } | 36 } |
| 37 virtual void Run() { | 37 virtual void Run() { |
| 38 PlatformThread::Sleep(msec_); | 38 base::PlatformThread::Sleep(msec_); |
| 39 } | 39 } |
| 40 private: | 40 private: |
| 41 int msec_; | 41 int msec_; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 class SleepInsideInitThread : public Thread { | 44 class SleepInsideInitThread : public Thread { |
| 45 public: | 45 public: |
| 46 SleepInsideInitThread() : Thread("none") { init_called_ = false; } | 46 SleepInsideInitThread() : Thread("none") { init_called_ = false; } |
| 47 virtual ~SleepInsideInitThread() { } | 47 virtual ~SleepInsideInitThread() { } |
| 48 | 48 |
| 49 virtual void Init() { | 49 virtual void Init() { |
| 50 PlatformThread::Sleep(500); | 50 base::PlatformThread::Sleep(500); |
| 51 init_called_ = true; | 51 init_called_ = true; |
| 52 } | 52 } |
| 53 bool InitCalled() { return init_called_; } | 53 bool InitCalled() { return init_called_; } |
| 54 private: | 54 private: |
| 55 bool init_called_; | 55 bool init_called_; |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 enum ThreadEvent { | 58 enum ThreadEvent { |
| 59 // Thread::Init() was called. | 59 // Thread::Init() was called. |
| 60 THREAD_EVENT_INIT, | 60 THREAD_EVENT_INIT, |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 EXPECT_TRUE(a.message_loop()); | 171 EXPECT_TRUE(a.message_loop()); |
| 172 EXPECT_TRUE(a.IsRunning()); | 172 EXPECT_TRUE(a.IsRunning()); |
| 173 | 173 |
| 174 bool was_invoked = false; | 174 bool was_invoked = false; |
| 175 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); | 175 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); |
| 176 | 176 |
| 177 // wait for the task to run (we could use a kernel event here | 177 // wait for the task to run (we could use a kernel event here |
| 178 // instead to avoid busy waiting, but this is sufficient for | 178 // instead to avoid busy waiting, but this is sufficient for |
| 179 // testing purposes). | 179 // testing purposes). |
| 180 for (int i = 100; i >= 0 && !was_invoked; --i) { | 180 for (int i = 100; i >= 0 && !was_invoked; --i) { |
| 181 PlatformThread::Sleep(10); | 181 base::PlatformThread::Sleep(10); |
| 182 } | 182 } |
| 183 EXPECT_TRUE(was_invoked); | 183 EXPECT_TRUE(was_invoked); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(ThreadTest, TwoTasks) { | 186 TEST_F(ThreadTest, TwoTasks) { |
| 187 bool was_invoked = false; | 187 bool was_invoked = false; |
| 188 { | 188 { |
| 189 Thread a("TwoTasks"); | 189 Thread a("TwoTasks"); |
| 190 EXPECT_TRUE(a.Start()); | 190 EXPECT_TRUE(a.Start()); |
| 191 EXPECT_TRUE(a.message_loop()); | 191 EXPECT_TRUE(a.message_loop()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // Upon leaving this scope, the thread is deleted. | 251 // Upon leaving this scope, the thread is deleted. |
| 252 } | 252 } |
| 253 | 253 |
| 254 // Check the order of events during shutdown. | 254 // Check the order of events during shutdown. |
| 255 ASSERT_EQ(4u, captured_events.size()); | 255 ASSERT_EQ(4u, captured_events.size()); |
| 256 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 256 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
| 257 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 257 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
| 258 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 258 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
| 259 EXPECT_EQ(THREAD_EVENT_CLEANUP_AFTER_LOOP, captured_events[3]); | 259 EXPECT_EQ(THREAD_EVENT_CLEANUP_AFTER_LOOP, captured_events[3]); |
| 260 } | 260 } |
| OLD | NEW |