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