| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "sync/internal_api/public/base/cancelation_signal.h" | 5 #include "sync/internal_api/public/base/cancelation_signal.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 // Blocks until canceled. Signals |task_done_signal| when finished (either | 27 // Blocks until canceled. Signals |task_done_signal| when finished (either |
| 28 // via early cancel or cancel after start). Signals |task_start_signal| if | 28 // via early cancel or cancel after start). Signals |task_start_signal| if |
| 29 // and when the task starts successfully (which will not happen if the task | 29 // and when the task starts successfully (which will not happen if the task |
| 30 // was cancelled early). | 30 // was cancelled early). |
| 31 void Run(base::WaitableEvent* task_start_signal, | 31 void Run(base::WaitableEvent* task_start_signal, |
| 32 base::WaitableEvent* task_done_signal); | 32 base::WaitableEvent* task_done_signal); |
| 33 | 33 |
| 34 // Implementation of CancelationObserver. | 34 // Implementation of CancelationObserver. |
| 35 // Wakes up the thread blocked in Run(). | 35 // Wakes up the thread blocked in Run(). |
| 36 virtual void OnSignalReceived() OVERRIDE; | 36 virtual void OnSignalReceived() override; |
| 37 | 37 |
| 38 // Checks if we ever did successfully start waiting for |event_|. Be careful | 38 // Checks if we ever did successfully start waiting for |event_|. Be careful |
| 39 // with this. The flag itself is thread-unsafe, and the event that flips it | 39 // with this. The flag itself is thread-unsafe, and the event that flips it |
| 40 // is racy. | 40 // is racy. |
| 41 bool WasStarted(); | 41 bool WasStarted(); |
| 42 | 42 |
| 43 private: | 43 private: |
| 44 base::WaitableEvent event_; | 44 base::WaitableEvent event_; |
| 45 base::Thread exec_thread_; | 45 base::Thread exec_thread_; |
| 46 CancelationSignal* cancel_signal_; | 46 CancelationSignal* cancel_signal_; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 143 |
| 144 bool CancelationSignalTest::VerifyTaskNotStarted() { | 144 bool CancelationSignalTest::VerifyTaskNotStarted() { |
| 145 // Wait until BlockingTask::Run() has finished. | 145 // Wait until BlockingTask::Run() has finished. |
| 146 task_done_event_.Wait(); | 146 task_done_event_.Wait(); |
| 147 | 147 |
| 148 // Verify the background thread never started blocking. | 148 // Verify the background thread never started blocking. |
| 149 return !blocking_task_.WasStarted(); | 149 return !blocking_task_.WasStarted(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 class FakeCancelationObserver : public CancelationObserver { | 152 class FakeCancelationObserver : public CancelationObserver { |
| 153 virtual void OnSignalReceived() OVERRIDE { } | 153 virtual void OnSignalReceived() override { } |
| 154 }; | 154 }; |
| 155 | 155 |
| 156 TEST(CancelationSignalTest_SingleThread, CheckFlags) { | 156 TEST(CancelationSignalTest_SingleThread, CheckFlags) { |
| 157 FakeCancelationObserver observer; | 157 FakeCancelationObserver observer; |
| 158 CancelationSignal signal; | 158 CancelationSignal signal; |
| 159 | 159 |
| 160 EXPECT_FALSE(signal.IsSignalled()); | 160 EXPECT_FALSE(signal.IsSignalled()); |
| 161 signal.Signal(); | 161 signal.Signal(); |
| 162 EXPECT_TRUE(signal.IsSignalled()); | 162 EXPECT_TRUE(signal.IsSignalled()); |
| 163 EXPECT_FALSE(signal.TryRegisterHandler(&observer)); | 163 EXPECT_FALSE(signal.TryRegisterHandler(&observer)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 177 // progress. | 177 // progress. |
| 178 TEST_F(CancelationSignalTest, Cancel) { | 178 TEST_F(CancelationSignalTest, Cancel) { |
| 179 StartBlockingTaskAndWaitForItToStart(); | 179 StartBlockingTaskAndWaitForItToStart(); |
| 180 | 180 |
| 181 // Wait for the task to finish and let verify it has been started. | 181 // Wait for the task to finish and let verify it has been started. |
| 182 CancelBlocking(); | 182 CancelBlocking(); |
| 183 EXPECT_FALSE(VerifyTaskNotStarted()); | 183 EXPECT_FALSE(VerifyTaskNotStarted()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 } // namespace syncer | 186 } // namespace syncer |
| OLD | NEW |