OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/threading/thread.h" | 5 #include "base/threading/thread.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 public: | 28 public: |
29 SleepInsideInitThread() : Thread("none") { | 29 SleepInsideInitThread() : Thread("none") { |
30 init_called_ = false; | 30 init_called_ = false; |
31 ANNOTATE_BENIGN_RACE( | 31 ANNOTATE_BENIGN_RACE( |
32 this, "Benign test-only data race on vptr - http://crbug.com/98219"); | 32 this, "Benign test-only data race on vptr - http://crbug.com/98219"); |
33 } | 33 } |
34 virtual ~SleepInsideInitThread() { | 34 virtual ~SleepInsideInitThread() { |
35 Stop(); | 35 Stop(); |
36 } | 36 } |
37 | 37 |
38 virtual void Init() { | 38 virtual void Init() OVERRIDE { |
39 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); | 39 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); |
40 init_called_ = true; | 40 init_called_ = true; |
41 } | 41 } |
42 bool InitCalled() { return init_called_; } | 42 bool InitCalled() { return init_called_; } |
43 private: | 43 private: |
44 bool init_called_; | 44 bool init_called_; |
45 }; | 45 }; |
46 | 46 |
47 enum ThreadEvent { | 47 enum ThreadEvent { |
48 // Thread::Init() was called. | 48 // Thread::Init() was called. |
(...skipping 10 matching lines...) Expand all Loading... |
59 }; | 59 }; |
60 | 60 |
61 typedef std::vector<ThreadEvent> EventList; | 61 typedef std::vector<ThreadEvent> EventList; |
62 | 62 |
63 class CaptureToEventList : public Thread { | 63 class CaptureToEventList : public Thread { |
64 public: | 64 public: |
65 // This Thread pushes events into the vector |event_list| to show | 65 // This Thread pushes events into the vector |event_list| to show |
66 // the order they occured in. |event_list| must remain valid for the | 66 // the order they occured in. |event_list| must remain valid for the |
67 // lifetime of this thread. | 67 // lifetime of this thread. |
68 explicit CaptureToEventList(EventList* event_list) | 68 explicit CaptureToEventList(EventList* event_list) |
69 : Thread("none"), event_list_(event_list) { | 69 : Thread("none"), |
| 70 event_list_(event_list) { |
70 } | 71 } |
71 | 72 |
72 virtual ~CaptureToEventList() { | 73 virtual ~CaptureToEventList() { |
73 Stop(); | 74 Stop(); |
74 } | 75 } |
75 | 76 |
76 virtual void Init() { | 77 virtual void Init() OVERRIDE { |
77 event_list_->push_back(THREAD_EVENT_INIT); | 78 event_list_->push_back(THREAD_EVENT_INIT); |
78 } | 79 } |
79 | 80 |
80 virtual void CleanUp() { | 81 virtual void CleanUp() OVERRIDE { |
81 event_list_->push_back(THREAD_EVENT_CLEANUP); | 82 event_list_->push_back(THREAD_EVENT_CLEANUP); |
82 } | 83 } |
83 | 84 |
84 private: | 85 private: |
85 EventList* event_list_; | 86 EventList* event_list_; |
86 }; | 87 }; |
87 | 88 |
88 // Observer that writes a value into |event_list| when a message loop has been | 89 // Observer that writes a value into |event_list| when a message loop has been |
89 // destroyed. | 90 // destroyed. |
90 class CapturingDestructionObserver : public MessageLoop::DestructionObserver { | 91 class CapturingDestructionObserver : public MessageLoop::DestructionObserver { |
91 public: | 92 public: |
92 // |event_list| must remain valid throughout the observer's lifetime. | 93 // |event_list| must remain valid throughout the observer's lifetime. |
93 explicit CapturingDestructionObserver(EventList* event_list) | 94 explicit CapturingDestructionObserver(EventList* event_list) |
94 : event_list_(event_list) { | 95 : event_list_(event_list) { |
95 } | 96 } |
96 | 97 |
97 // DestructionObserver implementation: | 98 // DestructionObserver implementation: |
98 virtual void WillDestroyCurrentMessageLoop() { | 99 virtual void WillDestroyCurrentMessageLoop() OVERRIDE { |
99 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); | 100 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); |
100 event_list_ = NULL; | 101 event_list_ = NULL; |
101 } | 102 } |
102 | 103 |
103 private: | 104 private: |
104 EventList* event_list_; | 105 EventList* event_list_; |
105 }; | 106 }; |
106 | 107 |
107 // Task that adds a destruction observer to the current message loop. | 108 // Task that adds a destruction observer to the current message loop. |
108 void RegisterDestructionObserver(MessageLoop::DestructionObserver* observer) { | 109 void RegisterDestructionObserver(MessageLoop::DestructionObserver* observer) { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 228 |
228 // Upon leaving this scope, the thread is deleted. | 229 // Upon leaving this scope, the thread is deleted. |
229 } | 230 } |
230 | 231 |
231 // Check the order of events during shutdown. | 232 // Check the order of events during shutdown. |
232 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); | 233 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); |
233 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 234 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
234 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 235 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
235 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 236 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
236 } | 237 } |
OLD | NEW |