| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/time.h" | 5 #include "base/time.h" |
| 6 #include "base/waitable_event.h" | 6 #include "base/waitable_event.h" |
| 7 #include "base/platform_thread.h" | 7 #include "base/threading/platform_thread.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 using base::TimeDelta; | 10 using base::TimeDelta; |
| 11 using base::WaitableEvent; | 11 using base::WaitableEvent; |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 typedef testing::Test WaitableEventTest; | 14 typedef testing::Test WaitableEventTest; |
| 15 } | 15 } |
| 16 | 16 |
| 17 TEST(WaitableEventTest, ManualBasics) { | 17 TEST(WaitableEventTest, ManualBasics) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 ev[4]->Signal(); | 67 ev[4]->Signal(); |
| 68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u); | 68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u); |
| 69 | 69 |
| 70 ev[0]->Signal(); | 70 ev[0]->Signal(); |
| 71 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u); | 71 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u); |
| 72 | 72 |
| 73 for (unsigned i = 0; i < 5; ++i) | 73 for (unsigned i = 0; i < 5; ++i) |
| 74 delete ev[i]; | 74 delete ev[i]; |
| 75 } | 75 } |
| 76 | 76 |
| 77 class WaitableEventSignaler : public PlatformThread::Delegate { | 77 class WaitableEventSignaler : public base::PlatformThread::Delegate { |
| 78 public: | 78 public: |
| 79 WaitableEventSignaler(double seconds, WaitableEvent* ev) | 79 WaitableEventSignaler(double seconds, WaitableEvent* ev) |
| 80 : seconds_(seconds), | 80 : seconds_(seconds), |
| 81 ev_(ev) { | 81 ev_(ev) { |
| 82 } | 82 } |
| 83 | 83 |
| 84 void ThreadMain() { | 84 void ThreadMain() { |
| 85 PlatformThread::Sleep(static_cast<int>(seconds_ * 1000)); | 85 base::PlatformThread::Sleep(static_cast<int>(seconds_ * 1000)); |
| 86 ev_->Signal(); | 86 ev_->Signal(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 private: | 89 private: |
| 90 const double seconds_; | 90 const double seconds_; |
| 91 WaitableEvent *const ev_; | 91 WaitableEvent *const ev_; |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 TEST(WaitableEventTest, WaitMany) { | 94 TEST(WaitableEventTest, WaitMany) { |
| 95 WaitableEvent* ev[5]; | 95 WaitableEvent* ev[5]; |
| 96 for (unsigned i = 0; i < 5; ++i) | 96 for (unsigned i = 0; i < 5; ++i) |
| 97 ev[i] = new WaitableEvent(false, false); | 97 ev[i] = new WaitableEvent(false, false); |
| 98 | 98 |
| 99 WaitableEventSignaler signaler(0.1, ev[2]); | 99 WaitableEventSignaler signaler(0.1, ev[2]); |
| 100 PlatformThreadHandle thread; | 100 base::PlatformThreadHandle thread; |
| 101 PlatformThread::Create(0, &signaler, &thread); | 101 base::PlatformThread::Create(0, &signaler, &thread); |
| 102 | 102 |
| 103 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 2u); | 103 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 2u); |
| 104 | 104 |
| 105 PlatformThread::Join(thread); | 105 base::PlatformThread::Join(thread); |
| 106 | 106 |
| 107 for (unsigned i = 0; i < 5; ++i) | 107 for (unsigned i = 0; i < 5; ++i) |
| 108 delete ev[i]; | 108 delete ev[i]; |
| 109 } | 109 } |
| OLD | NEW |