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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 TEST(WaitableEventTest, ManualBasics) { | 17 TEST(WaitableEventTest, ManualBasics) { |
18 WaitableEvent event(true, false); | 18 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 19 WaitableEvent::InitialState::NOT_SIGNALED); |
19 | 20 |
20 EXPECT_FALSE(event.IsSignaled()); | 21 EXPECT_FALSE(event.IsSignaled()); |
21 | 22 |
22 event.Signal(); | 23 event.Signal(); |
23 EXPECT_TRUE(event.IsSignaled()); | 24 EXPECT_TRUE(event.IsSignaled()); |
24 EXPECT_TRUE(event.IsSignaled()); | 25 EXPECT_TRUE(event.IsSignaled()); |
25 | 26 |
26 event.Reset(); | 27 event.Reset(); |
27 EXPECT_FALSE(event.IsSignaled()); | 28 EXPECT_FALSE(event.IsSignaled()); |
28 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); | 29 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); |
29 | 30 |
30 event.Signal(); | 31 event.Signal(); |
31 event.Wait(); | 32 event.Wait(); |
32 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10))); | 33 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10))); |
33 } | 34 } |
34 | 35 |
35 TEST(WaitableEventTest, AutoBasics) { | 36 TEST(WaitableEventTest, AutoBasics) { |
36 WaitableEvent event(false, false); | 37 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 38 WaitableEvent::InitialState::NOT_SIGNALED); |
37 | 39 |
38 EXPECT_FALSE(event.IsSignaled()); | 40 EXPECT_FALSE(event.IsSignaled()); |
39 | 41 |
40 event.Signal(); | 42 event.Signal(); |
41 EXPECT_TRUE(event.IsSignaled()); | 43 EXPECT_TRUE(event.IsSignaled()); |
42 EXPECT_FALSE(event.IsSignaled()); | 44 EXPECT_FALSE(event.IsSignaled()); |
43 | 45 |
44 event.Reset(); | 46 event.Reset(); |
45 EXPECT_FALSE(event.IsSignaled()); | 47 EXPECT_FALSE(event.IsSignaled()); |
46 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); | 48 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); |
47 | 49 |
48 event.Signal(); | 50 event.Signal(); |
49 event.Wait(); | 51 event.Wait(); |
50 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); | 52 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); |
51 | 53 |
52 event.Signal(); | 54 event.Signal(); |
53 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10))); | 55 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10))); |
54 } | 56 } |
55 | 57 |
56 TEST(WaitableEventTest, WaitManyShortcut) { | 58 TEST(WaitableEventTest, WaitManyShortcut) { |
57 WaitableEvent* ev[5]; | 59 WaitableEvent* ev[5]; |
58 for (unsigned i = 0; i < 5; ++i) | 60 for (unsigned i = 0; i < 5; ++i) { |
59 ev[i] = new WaitableEvent(false, false); | 61 ev[i] = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 62 WaitableEvent::InitialState::NOT_SIGNALED); |
| 63 } |
60 | 64 |
61 ev[3]->Signal(); | 65 ev[3]->Signal(); |
62 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u); | 66 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u); |
63 | 67 |
64 ev[3]->Signal(); | 68 ev[3]->Signal(); |
65 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u); | 69 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u); |
66 | 70 |
67 ev[4]->Signal(); | 71 ev[4]->Signal(); |
68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u); | 72 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u); |
69 | 73 |
(...skipping 17 matching lines...) Expand all Loading... |
87 } | 91 } |
88 | 92 |
89 private: | 93 private: |
90 const TimeDelta delay_; | 94 const TimeDelta delay_; |
91 WaitableEvent* event_; | 95 WaitableEvent* event_; |
92 }; | 96 }; |
93 | 97 |
94 // Tests that a WaitableEvent can be safely deleted when |Wait| is done without | 98 // Tests that a WaitableEvent can be safely deleted when |Wait| is done without |
95 // additional synchronization. | 99 // additional synchronization. |
96 TEST(WaitableEventTest, WaitAndDelete) { | 100 TEST(WaitableEventTest, WaitAndDelete) { |
97 WaitableEvent* ev = new WaitableEvent(false, false); | 101 WaitableEvent* ev = |
| 102 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 103 WaitableEvent::InitialState::NOT_SIGNALED); |
98 | 104 |
99 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev); | 105 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev); |
100 PlatformThreadHandle thread; | 106 PlatformThreadHandle thread; |
101 PlatformThread::Create(0, &signaler, &thread); | 107 PlatformThread::Create(0, &signaler, &thread); |
102 | 108 |
103 ev->Wait(); | 109 ev->Wait(); |
104 delete ev; | 110 delete ev; |
105 | 111 |
106 PlatformThread::Join(thread); | 112 PlatformThread::Join(thread); |
107 } | 113 } |
108 | 114 |
109 // Tests that a WaitableEvent can be safely deleted when |WaitMany| is done | 115 // Tests that a WaitableEvent can be safely deleted when |WaitMany| is done |
110 // without additional synchronization. | 116 // without additional synchronization. |
111 TEST(WaitableEventTest, WaitMany) { | 117 TEST(WaitableEventTest, WaitMany) { |
112 WaitableEvent* ev[5]; | 118 WaitableEvent* ev[5]; |
113 for (unsigned i = 0; i < 5; ++i) | 119 for (unsigned i = 0; i < 5; ++i) { |
114 ev[i] = new WaitableEvent(false, false); | 120 ev[i] = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 121 WaitableEvent::InitialState::NOT_SIGNALED); |
| 122 } |
115 | 123 |
116 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev[2]); | 124 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev[2]); |
117 PlatformThreadHandle thread; | 125 PlatformThreadHandle thread; |
118 PlatformThread::Create(0, &signaler, &thread); | 126 PlatformThread::Create(0, &signaler, &thread); |
119 | 127 |
120 size_t index = WaitableEvent::WaitMany(ev, 5); | 128 size_t index = WaitableEvent::WaitMany(ev, 5); |
121 | 129 |
122 for (unsigned i = 0; i < 5; ++i) | 130 for (unsigned i = 0; i < 5; ++i) |
123 delete ev[i]; | 131 delete ev[i]; |
124 | 132 |
125 PlatformThread::Join(thread); | 133 PlatformThread::Join(thread); |
126 EXPECT_EQ(2u, index); | 134 EXPECT_EQ(2u, index); |
127 } | 135 } |
128 | 136 |
129 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing | 137 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing |
130 // a timeout of 0. (crbug.com/465948) | 138 // a timeout of 0. (crbug.com/465948) |
131 #if defined(OS_POSIX) | 139 #if defined(OS_POSIX) |
132 // crbug.com/465948 not fixed yet. | 140 // crbug.com/465948 not fixed yet. |
133 #define MAYBE_TimedWait DISABLED_TimedWait | 141 #define MAYBE_TimedWait DISABLED_TimedWait |
134 #else | 142 #else |
135 #define MAYBE_TimedWait TimedWait | 143 #define MAYBE_TimedWait TimedWait |
136 #endif | 144 #endif |
137 TEST(WaitableEventTest, MAYBE_TimedWait) { | 145 TEST(WaitableEventTest, MAYBE_TimedWait) { |
138 WaitableEvent* ev = new WaitableEvent(false, false); | 146 WaitableEvent* ev = |
| 147 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 148 WaitableEvent::InitialState::NOT_SIGNALED); |
139 | 149 |
140 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10); | 150 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10); |
141 WaitableEventSignaler signaler(thread_delay, ev); | 151 WaitableEventSignaler signaler(thread_delay, ev); |
142 PlatformThreadHandle thread; | 152 PlatformThreadHandle thread; |
143 TimeTicks start = TimeTicks::Now(); | 153 TimeTicks start = TimeTicks::Now(); |
144 PlatformThread::Create(0, &signaler, &thread); | 154 PlatformThread::Create(0, &signaler, &thread); |
145 | 155 |
146 ev->TimedWait(TimeDelta::Max()); | 156 ev->TimedWait(TimeDelta::Max()); |
147 EXPECT_GE(TimeTicks::Now() - start, thread_delay); | 157 EXPECT_GE(TimeTicks::Now() - start, thread_delay); |
148 delete ev; | 158 delete ev; |
149 | 159 |
150 PlatformThread::Join(thread); | 160 PlatformThread::Join(thread); |
151 } | 161 } |
152 | 162 |
153 } // namespace base | 163 } // namespace base |
OLD | NEW |