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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 *value = !*value; | 24 *value = !*value; |
25 } | 25 } |
26 | 26 |
27 class SleepInsideInitThread : public Thread { | 27 class SleepInsideInitThread : public Thread { |
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 ~SleepInsideInitThread() override { Stop(); } |
35 Stop(); | |
36 } | |
37 | 35 |
38 virtual void Init() override { | 36 void Init() override { |
39 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); | 37 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); |
40 init_called_ = true; | 38 init_called_ = true; |
41 } | 39 } |
42 bool InitCalled() { return init_called_; } | 40 bool InitCalled() { return init_called_; } |
43 private: | 41 private: |
44 bool init_called_; | 42 bool init_called_; |
45 }; | 43 }; |
46 | 44 |
47 enum ThreadEvent { | 45 enum ThreadEvent { |
48 // Thread::Init() was called. | 46 // Thread::Init() was called. |
(...skipping 14 matching lines...) Expand all Loading... |
63 class CaptureToEventList : public Thread { | 61 class CaptureToEventList : public Thread { |
64 public: | 62 public: |
65 // This Thread pushes events into the vector |event_list| to show | 63 // This Thread pushes events into the vector |event_list| to show |
66 // the order they occured in. |event_list| must remain valid for the | 64 // the order they occured in. |event_list| must remain valid for the |
67 // lifetime of this thread. | 65 // lifetime of this thread. |
68 explicit CaptureToEventList(EventList* event_list) | 66 explicit CaptureToEventList(EventList* event_list) |
69 : Thread("none"), | 67 : Thread("none"), |
70 event_list_(event_list) { | 68 event_list_(event_list) { |
71 } | 69 } |
72 | 70 |
73 virtual ~CaptureToEventList() { | 71 ~CaptureToEventList() override { Stop(); } |
74 Stop(); | |
75 } | |
76 | 72 |
77 virtual void Init() override { | 73 void Init() override { event_list_->push_back(THREAD_EVENT_INIT); } |
78 event_list_->push_back(THREAD_EVENT_INIT); | |
79 } | |
80 | 74 |
81 virtual void CleanUp() override { | 75 void CleanUp() override { event_list_->push_back(THREAD_EVENT_CLEANUP); } |
82 event_list_->push_back(THREAD_EVENT_CLEANUP); | |
83 } | |
84 | 76 |
85 private: | 77 private: |
86 EventList* event_list_; | 78 EventList* event_list_; |
87 }; | 79 }; |
88 | 80 |
89 // Observer that writes a value into |event_list| when a message loop has been | 81 // Observer that writes a value into |event_list| when a message loop has been |
90 // destroyed. | 82 // destroyed. |
91 class CapturingDestructionObserver | 83 class CapturingDestructionObserver |
92 : public base::MessageLoop::DestructionObserver { | 84 : public base::MessageLoop::DestructionObserver { |
93 public: | 85 public: |
94 // |event_list| must remain valid throughout the observer's lifetime. | 86 // |event_list| must remain valid throughout the observer's lifetime. |
95 explicit CapturingDestructionObserver(EventList* event_list) | 87 explicit CapturingDestructionObserver(EventList* event_list) |
96 : event_list_(event_list) { | 88 : event_list_(event_list) { |
97 } | 89 } |
98 | 90 |
99 // DestructionObserver implementation: | 91 // DestructionObserver implementation: |
100 virtual void WillDestroyCurrentMessageLoop() override { | 92 void WillDestroyCurrentMessageLoop() override { |
101 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); | 93 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); |
102 event_list_ = NULL; | 94 event_list_ = NULL; |
103 } | 95 } |
104 | 96 |
105 private: | 97 private: |
106 EventList* event_list_; | 98 EventList* event_list_; |
107 }; | 99 }; |
108 | 100 |
109 // Task that adds a destruction observer to the current message loop. | 101 // Task that adds a destruction observer to the current message loop. |
110 void RegisterDestructionObserver( | 102 void RegisterDestructionObserver( |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 222 |
231 // Upon leaving this scope, the thread is deleted. | 223 // Upon leaving this scope, the thread is deleted. |
232 } | 224 } |
233 | 225 |
234 // Check the order of events during shutdown. | 226 // Check the order of events during shutdown. |
235 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); | 227 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); |
236 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 228 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
237 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 229 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
238 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 230 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
239 } | 231 } |
OLD | NEW |