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/synchronization/waitable_event.h" | 5 #include "base/synchronization/waitable_event.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 | 73 |
74 class WaitableEventSignaler : public PlatformThread::Delegate { | 74 class WaitableEventSignaler : public PlatformThread::Delegate { |
75 public: | 75 public: |
76 WaitableEventSignaler(double seconds, WaitableEvent* ev) | 76 WaitableEventSignaler(double seconds, WaitableEvent* ev) |
77 : seconds_(seconds), | 77 : seconds_(seconds), |
78 ev_(ev) { | 78 ev_(ev) { |
79 } | 79 } |
80 | 80 |
81 virtual void ThreadMain() OVERRIDE { | 81 virtual void ThreadMain() OVERRIDE { |
82 PlatformThread::Sleep(TimeDelta::FromSeconds(static_cast<int>(seconds_))); | 82 PlatformThread::Sleep(TimeDelta::FromSecondsD(seconds_)); |
83 ev_->Signal(); | 83 ev_->Signal(); |
84 } | 84 } |
85 | 85 |
86 private: | 86 private: |
87 const double seconds_; | 87 const double seconds_; |
88 WaitableEvent *const ev_; | 88 WaitableEvent *const ev_; |
89 }; | 89 }; |
90 | 90 |
| 91 TEST(WaitableEventTest, WaitAndDelete) { |
| 92 // This test tests that if a WaitableEvent can be safely deleted |
| 93 // when |Wait| is done without additional synchrnization. |
| 94 // If this test crashes, it is a bug. |
| 95 |
| 96 WaitableEvent* ev = new WaitableEvent(false, false); |
| 97 |
| 98 WaitableEventSignaler signaler(0.01, ev); |
| 99 PlatformThreadHandle thread; |
| 100 PlatformThread::Create(0, &signaler, &thread); |
| 101 |
| 102 ev->Wait(); |
| 103 delete ev; |
| 104 |
| 105 PlatformThread::Join(thread); |
| 106 } |
| 107 |
91 TEST(WaitableEventTest, WaitMany) { | 108 TEST(WaitableEventTest, WaitMany) { |
| 109 // This test tests that if a WaitableEvent can be safely deleted |
| 110 // when |WaitMany| is done without additional synchrnization. |
| 111 // If this test crashes, it is a bug. |
| 112 |
92 WaitableEvent* ev[5]; | 113 WaitableEvent* ev[5]; |
93 for (unsigned i = 0; i < 5; ++i) | 114 for (unsigned i = 0; i < 5; ++i) |
94 ev[i] = new WaitableEvent(false, false); | 115 ev[i] = new WaitableEvent(false, false); |
95 | 116 |
96 WaitableEventSignaler signaler(0.1, ev[2]); | 117 WaitableEventSignaler signaler(0.01, ev[2]); |
97 PlatformThreadHandle thread; | 118 PlatformThreadHandle thread; |
98 PlatformThread::Create(0, &signaler, &thread); | 119 PlatformThread::Create(0, &signaler, &thread); |
99 | 120 |
100 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 2u); | 121 size_t index = WaitableEvent::WaitMany(ev, 5); |
101 | |
102 PlatformThread::Join(thread); | |
103 | 122 |
104 for (unsigned i = 0; i < 5; ++i) | 123 for (unsigned i = 0; i < 5; ++i) |
105 delete ev[i]; | 124 delete ev[i]; |
| 125 |
| 126 PlatformThread::Join(thread); |
| 127 EXPECT_EQ(2u, index); |
106 } | 128 } |
107 | 129 |
108 } // namespace base | 130 } // namespace base |
OLD | NEW |