hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
tommi | cd255cc | 2016-01-19 13:13:14 -0800 | [diff] [blame] | 11 | // TODO(tommi): Remove completely. As is there is still some code for Windows |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 12 | // that relies on ConditionVariableEventWin, but code has been removed on other |
tommi | cd255cc | 2016-01-19 13:13:14 -0800 | [diff] [blame] | 13 | // platforms. |
| 14 | #if defined(WEBRTC_WIN) |
| 15 | |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/source/condition_variable_event_win.h" |
| 17 | |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 19 | #include "webrtc/base/platform_thread.h" |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 20 | #include "webrtc/base/timeutils.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/include/trace.h" |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | namespace { |
| 27 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 28 | const int kLongWaitMs = 100 * 1000; // A long time in testing terms |
| 29 | const int kShortWaitMs = 2 * 1000; // Long enough for process switches to happen |
hta | 3866c4f | 2015-10-20 09:31:01 -0700 | [diff] [blame] | 30 | const int kVeryShortWaitMs = 20; // Used when we want a timeout |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 31 | |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 32 | // A Baton is one possible control structure one can build using |
| 33 | // conditional variables. |
| 34 | // A Baton is always held by one and only one active thread - unlike |
| 35 | // a lock, it can never be free. |
| 36 | // One can pass it or grab it - both calls have timeouts. |
| 37 | // Note - a production tool would guard against passing it without |
| 38 | // grabbing it first. This one is for testing, so it doesn't. |
| 39 | class Baton { |
| 40 | public: |
| 41 | Baton() |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 42 | : being_passed_(false), |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 43 | pass_count_(0) { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 44 | InitializeCriticalSection(&crit_sect_); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ~Baton() { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 48 | DeleteCriticalSection(&crit_sect_); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Pass the baton. Returns false if baton is not picked up in |max_msecs|. |
| 52 | // Only one process can pass at the same time; this property is |
| 53 | // ensured by the |giver_sect_| lock. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 54 | bool Pass(uint32_t max_msecs) { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 55 | CriticalSectionScoped cs_giver(&giver_sect_); |
| 56 | EnterCriticalSection(&crit_sect_); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 57 | SignalBatonAvailable(); |
| 58 | const bool result = TakeBatonIfStillFree(max_msecs); |
| 59 | if (result) { |
| 60 | ++pass_count_; |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 61 | } |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 62 | LeaveCriticalSection(&crit_sect_); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 63 | return result; |
| 64 | } |
| 65 | |
| 66 | // Grab the baton. Returns false if baton is not passed. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 67 | bool Grab(uint32_t max_msecs) { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 68 | EnterCriticalSection(&crit_sect_); |
| 69 | bool ret = WaitUntilBatonOffered(max_msecs); |
| 70 | LeaveCriticalSection(&crit_sect_); |
| 71 | return ret; |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int PassCount() { |
| 75 | // We don't allow polling PassCount() during a Pass()-call since there is |
| 76 | // no guarantee that |pass_count_| is incremented until the Pass()-call |
| 77 | // finishes. I.e. the Grab()-call may finish before |pass_count_| has been |
| 78 | // incremented. |
| 79 | // Thus, this function waits on giver_sect_. |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 80 | CriticalSectionScoped cs(&giver_sect_); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 81 | return pass_count_; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | // Wait/Signal forms a classical semaphore on |being_passed_|. |
| 86 | // These functions must be called with crit_sect_ held. |
| 87 | bool WaitUntilBatonOffered(int timeout_ms) { |
| 88 | while (!being_passed_) { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 89 | if (!cond_var_.SleepCS(&crit_sect_, timeout_ms)) { |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | being_passed_ = false; |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 94 | cond_var_.Wake(); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void SignalBatonAvailable() { |
| 99 | assert(!being_passed_); |
| 100 | being_passed_ = true; |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 101 | cond_var_.Wake(); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Timeout extension: Wait for a limited time for someone else to |
| 105 | // take it, and take it if it's not taken. |
| 106 | // Returns true if resource is taken by someone else, false |
| 107 | // if it is taken back by the caller. |
| 108 | // This function must be called with both |giver_sect_| and |
| 109 | // |crit_sect_| held. |
| 110 | bool TakeBatonIfStillFree(int timeout_ms) { |
| 111 | bool not_timeout = true; |
| 112 | while (being_passed_ && not_timeout) { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 113 | not_timeout = cond_var_.SleepCS(&crit_sect_, timeout_ms); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 114 | // If we're woken up while variable is still held, we may have |
| 115 | // gotten a wakeup destined for a grabber thread. |
| 116 | // This situation is not treated specially here. |
| 117 | } |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 118 | if (!being_passed_) |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 119 | return true; |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 120 | assert(!not_timeout); |
| 121 | being_passed_ = false; |
| 122 | return false; |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Lock that ensures that there is only one thread in the active |
| 126 | // part of Pass() at a time. |
| 127 | // |giver_sect_| must always be acquired before |cond_var_|. |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 128 | CriticalSectionWrapper giver_sect_; |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 129 | // Lock that protects |being_passed_|. |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 130 | CRITICAL_SECTION crit_sect_; |
| 131 | ConditionVariableEventWin cond_var_; |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 132 | bool being_passed_; |
| 133 | // Statistics information: Number of successfull passes. |
| 134 | int pass_count_; |
| 135 | }; |
| 136 | |
| 137 | // Function that waits on a Baton, and passes it right back. |
| 138 | // We expect these calls never to time out. |
| 139 | bool WaitingRunFunction(void* obj) { |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 140 | Baton* the_baton = static_cast<Baton*> (obj); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 141 | EXPECT_TRUE(the_baton->Grab(kLongWaitMs)); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 142 | EXPECT_TRUE(the_baton->Pass(kLongWaitMs)); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | class CondVarTest : public ::testing::Test { |
| 147 | public: |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 148 | CondVarTest() : thread_(&WaitingRunFunction, &baton_, "CondVarTest") {} |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 149 | |
| 150 | virtual void SetUp() { |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 151 | thread_.Start(); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | virtual void TearDown() { |
| 155 | // We have to wake the thread in order to make it obey the stop order. |
| 156 | // But we don't know if the thread has completed the run function, so |
| 157 | // we don't know if it will exit before or after the Pass. |
| 158 | // Thus, we need to pin it down inside its Run function (between Grab |
| 159 | // and Pass). |
| 160 | ASSERT_TRUE(baton_.Pass(kShortWaitMs)); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 161 | ASSERT_TRUE(baton_.Grab(kShortWaitMs)); |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 162 | thread_.Stop(); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | protected: |
| 166 | Baton baton_; |
| 167 | |
| 168 | private: |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 169 | rtc::PlatformThread thread_; |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | // The SetUp and TearDown functions use condition variables. |
| 173 | // This test verifies those pieces in isolation. |
tommi@webrtc.org | 5d32f43 | 2015-02-05 06:25:35 +0000 | [diff] [blame] | 174 | // Disabled due to flakiness. See bug 4262 for details. |
| 175 | TEST_F(CondVarTest, DISABLED_InitFunctionsWork) { |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 176 | // All relevant asserts are in the SetUp and TearDown functions. |
| 177 | } |
| 178 | |
| 179 | // This test verifies that one can use the baton multiple times. |
bjornv@webrtc.org | dc096f2 | 2015-02-04 09:14:14 +0000 | [diff] [blame] | 180 | TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) { |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 181 | const int kNumberOfRounds = 2; |
| 182 | for (int i = 0; i < kNumberOfRounds; ++i) { |
| 183 | ASSERT_TRUE(baton_.Pass(kShortWaitMs)); |
| 184 | ASSERT_TRUE(baton_.Grab(kShortWaitMs)); |
| 185 | } |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 186 | EXPECT_EQ(2 * kNumberOfRounds, baton_.PassCount()); |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 187 | } |
| 188 | |
hta | 3866c4f | 2015-10-20 09:31:01 -0700 | [diff] [blame] | 189 | TEST(CondVarWaitTest, WaitingWaits) { |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 190 | CRITICAL_SECTION crit_sect; |
| 191 | InitializeCriticalSection(&crit_sect); |
| 192 | ConditionVariableEventWin cond_var; |
| 193 | EnterCriticalSection(&crit_sect); |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 194 | int64_t start_ms = rtc::TimeMillis(); |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 195 | EXPECT_FALSE(cond_var.SleepCS(&crit_sect, kVeryShortWaitMs)); |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 196 | int64_t end_ms = rtc::TimeMillis(); |
hta | 3866c4f | 2015-10-20 09:31:01 -0700 | [diff] [blame] | 197 | EXPECT_LE(start_ms + kVeryShortWaitMs, end_ms) |
| 198 | << "actual elapsed:" << end_ms - start_ms; |
Tommi | e849332 | 2016-01-20 13:36:31 +0100 | [diff] [blame] | 199 | LeaveCriticalSection(&crit_sect); |
| 200 | DeleteCriticalSection(&crit_sect); |
hta | 3866c4f | 2015-10-20 09:31:01 -0700 | [diff] [blame] | 201 | } |
| 202 | |
hta@webrtc.org | 72e3a89 | 2012-06-19 13:49:48 +0000 | [diff] [blame] | 203 | } // anonymous namespace |
| 204 | |
| 205 | } // namespace webrtc |
tommi | cd255cc | 2016-01-19 13:13:14 -0800 | [diff] [blame] | 206 | |
| 207 | #endif // defined(WEBRTC_WIN) |