blob: 4b1b6cc608998d8721323e1e19bb929e3b9c3c5c [file] [log] [blame]
hta@webrtc.org72e3a892012-06-19 13:49:48 +00001/*
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
tommicd255cc2016-01-19 13:13:14 -080011// TODO(tommi): Remove completely. As is there is still some code for Windows
Tommie8493322016-01-20 13:36:31 +010012// that relies on ConditionVariableEventWin, but code has been removed on other
tommicd255cc2016-01-19 13:13:14 -080013// platforms.
14#if defined(WEBRTC_WIN)
15
Tommie8493322016-01-20 13:36:31 +010016#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
17
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000018#include "testing/gtest/include/gtest/gtest.h"
pbos12411ef2015-11-23 14:47:56 -080019#include "webrtc/base/platform_thread.h"
hta3866c4f2015-10-20 09:31:01 -070020#include "webrtc/base/scoped_ptr.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/tick_util.h"
23#include "webrtc/system_wrappers/include/trace.h"
hta@webrtc.org72e3a892012-06-19 13:49:48 +000024
25namespace webrtc {
26
27namespace {
28
phoglund@webrtc.orga36d75a2012-11-14 09:55:04 +000029const int kLongWaitMs = 100 * 1000; // A long time in testing terms
30const int kShortWaitMs = 2 * 1000; // Long enough for process switches to happen
hta3866c4f2015-10-20 09:31:01 -070031const int kVeryShortWaitMs = 20; // Used when we want a timeout
hta@webrtc.org72e3a892012-06-19 13:49:48 +000032
hta@webrtc.org72e3a892012-06-19 13:49:48 +000033// A Baton is one possible control structure one can build using
34// conditional variables.
35// A Baton is always held by one and only one active thread - unlike
36// a lock, it can never be free.
37// One can pass it or grab it - both calls have timeouts.
38// Note - a production tool would guard against passing it without
39// grabbing it first. This one is for testing, so it doesn't.
40class Baton {
41 public:
42 Baton()
Tommie8493322016-01-20 13:36:31 +010043 : being_passed_(false),
hta@webrtc.org72e3a892012-06-19 13:49:48 +000044 pass_count_(0) {
Tommie8493322016-01-20 13:36:31 +010045 InitializeCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000046 }
47
48 ~Baton() {
Tommie8493322016-01-20 13:36:31 +010049 DeleteCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000050 }
51
52 // Pass the baton. Returns false if baton is not picked up in |max_msecs|.
53 // Only one process can pass at the same time; this property is
54 // ensured by the |giver_sect_| lock.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000055 bool Pass(uint32_t max_msecs) {
Tommie8493322016-01-20 13:36:31 +010056 CriticalSectionScoped cs_giver(&giver_sect_);
57 EnterCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000058 SignalBatonAvailable();
59 const bool result = TakeBatonIfStillFree(max_msecs);
60 if (result) {
61 ++pass_count_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +000062 }
Tommie8493322016-01-20 13:36:31 +010063 LeaveCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000064 return result;
65 }
66
67 // Grab the baton. Returns false if baton is not passed.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000068 bool Grab(uint32_t max_msecs) {
Tommie8493322016-01-20 13:36:31 +010069 EnterCriticalSection(&crit_sect_);
70 bool ret = WaitUntilBatonOffered(max_msecs);
71 LeaveCriticalSection(&crit_sect_);
72 return ret;
hta@webrtc.org72e3a892012-06-19 13:49:48 +000073 }
74
75 int PassCount() {
76 // We don't allow polling PassCount() during a Pass()-call since there is
77 // no guarantee that |pass_count_| is incremented until the Pass()-call
78 // finishes. I.e. the Grab()-call may finish before |pass_count_| has been
79 // incremented.
80 // Thus, this function waits on giver_sect_.
Tommie8493322016-01-20 13:36:31 +010081 CriticalSectionScoped cs(&giver_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000082 return pass_count_;
83 }
84
85 private:
86 // Wait/Signal forms a classical semaphore on |being_passed_|.
87 // These functions must be called with crit_sect_ held.
88 bool WaitUntilBatonOffered(int timeout_ms) {
89 while (!being_passed_) {
Tommie8493322016-01-20 13:36:31 +010090 if (!cond_var_.SleepCS(&crit_sect_, timeout_ms)) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +000091 return false;
92 }
93 }
94 being_passed_ = false;
Tommie8493322016-01-20 13:36:31 +010095 cond_var_.Wake();
hta@webrtc.org72e3a892012-06-19 13:49:48 +000096 return true;
97 }
98
99 void SignalBatonAvailable() {
100 assert(!being_passed_);
101 being_passed_ = true;
Tommie8493322016-01-20 13:36:31 +0100102 cond_var_.Wake();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000103 }
104
105 // Timeout extension: Wait for a limited time for someone else to
106 // take it, and take it if it's not taken.
107 // Returns true if resource is taken by someone else, false
108 // if it is taken back by the caller.
109 // This function must be called with both |giver_sect_| and
110 // |crit_sect_| held.
111 bool TakeBatonIfStillFree(int timeout_ms) {
112 bool not_timeout = true;
113 while (being_passed_ && not_timeout) {
Tommie8493322016-01-20 13:36:31 +0100114 not_timeout = cond_var_.SleepCS(&crit_sect_, timeout_ms);
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000115 // If we're woken up while variable is still held, we may have
116 // gotten a wakeup destined for a grabber thread.
117 // This situation is not treated specially here.
118 }
Tommie8493322016-01-20 13:36:31 +0100119 if (!being_passed_)
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000120 return true;
Tommie8493322016-01-20 13:36:31 +0100121 assert(!not_timeout);
122 being_passed_ = false;
123 return false;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000124 }
125
126 // Lock that ensures that there is only one thread in the active
127 // part of Pass() at a time.
128 // |giver_sect_| must always be acquired before |cond_var_|.
Tommie8493322016-01-20 13:36:31 +0100129 CriticalSectionWrapper giver_sect_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000130 // Lock that protects |being_passed_|.
Tommie8493322016-01-20 13:36:31 +0100131 CRITICAL_SECTION crit_sect_;
132 ConditionVariableEventWin cond_var_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000133 bool being_passed_;
134 // Statistics information: Number of successfull passes.
135 int pass_count_;
136};
137
138// Function that waits on a Baton, and passes it right back.
139// We expect these calls never to time out.
140bool WaitingRunFunction(void* obj) {
andrew@webrtc.org50419b02012-11-14 19:07:54 +0000141 Baton* the_baton = static_cast<Baton*> (obj);
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000142 EXPECT_TRUE(the_baton->Grab(kLongWaitMs));
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000143 EXPECT_TRUE(the_baton->Pass(kLongWaitMs));
144 return true;
145}
146
147class CondVarTest : public ::testing::Test {
148 public:
Peter Boström8c38e8b2015-11-26 17:45:47 +0100149 CondVarTest() : thread_(&WaitingRunFunction, &baton_, "CondVarTest") {}
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000150
151 virtual void SetUp() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100152 thread_.Start();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000153 }
154
155 virtual void TearDown() {
156 // We have to wake the thread in order to make it obey the stop order.
157 // But we don't know if the thread has completed the run function, so
158 // we don't know if it will exit before or after the Pass.
159 // Thus, we need to pin it down inside its Run function (between Grab
160 // and Pass).
161 ASSERT_TRUE(baton_.Pass(kShortWaitMs));
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000162 ASSERT_TRUE(baton_.Grab(kShortWaitMs));
Peter Boström8c38e8b2015-11-26 17:45:47 +0100163 thread_.Stop();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000164 }
165
166 protected:
167 Baton baton_;
168
169 private:
Peter Boström8c38e8b2015-11-26 17:45:47 +0100170 rtc::PlatformThread thread_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000171};
172
173// The SetUp and TearDown functions use condition variables.
174// This test verifies those pieces in isolation.
tommi@webrtc.org5d32f432015-02-05 06:25:35 +0000175// Disabled due to flakiness. See bug 4262 for details.
176TEST_F(CondVarTest, DISABLED_InitFunctionsWork) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000177 // All relevant asserts are in the SetUp and TearDown functions.
178}
179
180// This test verifies that one can use the baton multiple times.
bjornv@webrtc.orgdc096f22015-02-04 09:14:14 +0000181TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000182 const int kNumberOfRounds = 2;
183 for (int i = 0; i < kNumberOfRounds; ++i) {
184 ASSERT_TRUE(baton_.Pass(kShortWaitMs));
185 ASSERT_TRUE(baton_.Grab(kShortWaitMs));
186 }
phoglund@webrtc.orga36d75a2012-11-14 09:55:04 +0000187 EXPECT_EQ(2 * kNumberOfRounds, baton_.PassCount());
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000188}
189
hta3866c4f2015-10-20 09:31:01 -0700190TEST(CondVarWaitTest, WaitingWaits) {
Tommie8493322016-01-20 13:36:31 +0100191 CRITICAL_SECTION crit_sect;
192 InitializeCriticalSection(&crit_sect);
193 ConditionVariableEventWin cond_var;
194 EnterCriticalSection(&crit_sect);
hta3866c4f2015-10-20 09:31:01 -0700195 int64_t start_ms = TickTime::MillisecondTimestamp();
Tommie8493322016-01-20 13:36:31 +0100196 EXPECT_FALSE(cond_var.SleepCS(&crit_sect, kVeryShortWaitMs));
hta3866c4f2015-10-20 09:31:01 -0700197 int64_t end_ms = TickTime::MillisecondTimestamp();
198 EXPECT_LE(start_ms + kVeryShortWaitMs, end_ms)
199 << "actual elapsed:" << end_ms - start_ms;
Tommie8493322016-01-20 13:36:31 +0100200 LeaveCriticalSection(&crit_sect);
201 DeleteCriticalSection(&crit_sect);
hta3866c4f2015-10-20 09:31:01 -0700202}
203
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000204} // anonymous namespace
205
206} // namespace webrtc
tommicd255cc2016-01-19 13:13:14 -0800207
208#endif // defined(WEBRTC_WIN)