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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 ev[0]->Signal(); | 67 ev[0]->Signal(); |
68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u); | 68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u); |
69 | 69 |
70 for (unsigned i = 0; i < 5; ++i) | 70 for (unsigned i = 0; i < 5; ++i) |
71 delete ev[i]; | 71 delete ev[i]; |
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(TimeDelta delay, WaitableEvent* event) |
77 : seconds_(seconds), | 77 : delay_(delay), |
78 ev_(ev) { | 78 event_(event) { |
79 } | 79 } |
80 | 80 |
81 void ThreadMain() override { | 81 void ThreadMain() override { |
82 PlatformThread::Sleep(TimeDelta::FromSecondsD(seconds_)); | 82 PlatformThread::Sleep(delay_); |
83 ev_->Signal(); | 83 event_->Signal(); |
84 } | 84 } |
85 | 85 |
86 private: | 86 private: |
87 const double seconds_; | 87 const TimeDelta delay_; |
88 WaitableEvent *const ev_; | 88 WaitableEvent* event_; |
89 }; | 89 }; |
90 | 90 |
| 91 // Tests that a WaitableEvent can be safely deleted when |Wait| is done without |
| 92 // additional synchronization. |
91 TEST(WaitableEventTest, WaitAndDelete) { | 93 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); | 94 WaitableEvent* ev = new WaitableEvent(false, false); |
97 | 95 |
98 WaitableEventSignaler signaler(0.01, ev); | 96 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev); |
99 PlatformThreadHandle thread; | 97 PlatformThreadHandle thread; |
100 PlatformThread::Create(0, &signaler, &thread); | 98 PlatformThread::Create(0, &signaler, &thread); |
101 | 99 |
102 ev->Wait(); | 100 ev->Wait(); |
103 delete ev; | 101 delete ev; |
104 | 102 |
105 PlatformThread::Join(thread); | 103 PlatformThread::Join(thread); |
106 } | 104 } |
107 | 105 |
| 106 // Tests that a WaitableEvent can be safely deleted when |WaitMany| is done |
| 107 // without additional synchronization. |
108 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 | |
113 WaitableEvent* ev[5]; | 109 WaitableEvent* ev[5]; |
114 for (unsigned i = 0; i < 5; ++i) | 110 for (unsigned i = 0; i < 5; ++i) |
115 ev[i] = new WaitableEvent(false, false); | 111 ev[i] = new WaitableEvent(false, false); |
116 | 112 |
117 WaitableEventSignaler signaler(0.01, ev[2]); | 113 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev[2]); |
118 PlatformThreadHandle thread; | 114 PlatformThreadHandle thread; |
119 PlatformThread::Create(0, &signaler, &thread); | 115 PlatformThread::Create(0, &signaler, &thread); |
120 | 116 |
121 size_t index = WaitableEvent::WaitMany(ev, 5); | 117 size_t index = WaitableEvent::WaitMany(ev, 5); |
122 | 118 |
123 for (unsigned i = 0; i < 5; ++i) | 119 for (unsigned i = 0; i < 5; ++i) |
124 delete ev[i]; | 120 delete ev[i]; |
125 | 121 |
126 PlatformThread::Join(thread); | 122 PlatformThread::Join(thread); |
127 EXPECT_EQ(2u, index); | 123 EXPECT_EQ(2u, index); |
128 } | 124 } |
129 | 125 |
| 126 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing |
| 127 // a timeout of 0. (crbug.com/465948) |
| 128 #if defined(OS_POSIX) |
| 129 // crbug.com/465948 not fixed yet. |
| 130 #define MAYBE_TimedWait DISABLED_TimedWait |
| 131 #else |
| 132 #define MAYBE_TimedWait TimedWait |
| 133 #endif |
| 134 TEST(WaitableEventTest, MAYBE_TimedWait) { |
| 135 WaitableEvent* ev = new WaitableEvent(false, false); |
| 136 |
| 137 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10); |
| 138 WaitableEventSignaler signaler(thread_delay, ev); |
| 139 PlatformThreadHandle thread; |
| 140 TimeTicks start = TimeTicks::Now(); |
| 141 PlatformThread::Create(0, &signaler, &thread); |
| 142 |
| 143 ev->TimedWait(TimeDelta::Max()); |
| 144 EXPECT_GE(TimeTicks::Now() - start, thread_delay); |
| 145 delete ev; |
| 146 |
| 147 PlatformThread::Join(thread); |
| 148 } |
| 149 |
130 } // namespace base | 150 } // namespace base |
OLD | NEW |