blob: 67e473e2fb054218c82119c6ab8142a1f62198fd [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
pbos12411ef2015-11-23 14:47:56 -080016#include "webrtc/base/platform_thread.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020017#include "webrtc/base/timeutils.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/trace.h"
kwibergac9f8762016-09-30 22:29:43 -070020#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
21#include "webrtc/test/gtest.h"
hta@webrtc.org72e3a892012-06-19 13:49:48 +000022
23namespace webrtc {
24
25namespace {
26
phoglund@webrtc.orga36d75a2012-11-14 09:55:04 +000027const int kLongWaitMs = 100 * 1000; // A long time in testing terms
28const int kShortWaitMs = 2 * 1000; // Long enough for process switches to happen
hta3866c4f2015-10-20 09:31:01 -070029const int kVeryShortWaitMs = 20; // Used when we want a timeout
hta@webrtc.org72e3a892012-06-19 13:49:48 +000030
hta@webrtc.org72e3a892012-06-19 13:49:48 +000031// A Baton is one possible control structure one can build using
32// conditional variables.
33// A Baton is always held by one and only one active thread - unlike
34// a lock, it can never be free.
35// One can pass it or grab it - both calls have timeouts.
36// Note - a production tool would guard against passing it without
37// grabbing it first. This one is for testing, so it doesn't.
38class Baton {
39 public:
40 Baton()
Tommie8493322016-01-20 13:36:31 +010041 : being_passed_(false),
hta@webrtc.org72e3a892012-06-19 13:49:48 +000042 pass_count_(0) {
Tommie8493322016-01-20 13:36:31 +010043 InitializeCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000044 }
45
46 ~Baton() {
Tommie8493322016-01-20 13:36:31 +010047 DeleteCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000048 }
49
50 // Pass the baton. Returns false if baton is not picked up in |max_msecs|.
51 // Only one process can pass at the same time; this property is
52 // ensured by the |giver_sect_| lock.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000053 bool Pass(uint32_t max_msecs) {
Tommie8493322016-01-20 13:36:31 +010054 CriticalSectionScoped cs_giver(&giver_sect_);
55 EnterCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000056 SignalBatonAvailable();
57 const bool result = TakeBatonIfStillFree(max_msecs);
58 if (result) {
59 ++pass_count_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +000060 }
Tommie8493322016-01-20 13:36:31 +010061 LeaveCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000062 return result;
63 }
64
65 // Grab the baton. Returns false if baton is not passed.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000066 bool Grab(uint32_t max_msecs) {
Tommie8493322016-01-20 13:36:31 +010067 EnterCriticalSection(&crit_sect_);
68 bool ret = WaitUntilBatonOffered(max_msecs);
69 LeaveCriticalSection(&crit_sect_);
70 return ret;
hta@webrtc.org72e3a892012-06-19 13:49:48 +000071 }
72
73 int PassCount() {
74 // We don't allow polling PassCount() during a Pass()-call since there is
75 // no guarantee that |pass_count_| is incremented until the Pass()-call
76 // finishes. I.e. the Grab()-call may finish before |pass_count_| has been
77 // incremented.
78 // Thus, this function waits on giver_sect_.
Tommie8493322016-01-20 13:36:31 +010079 CriticalSectionScoped cs(&giver_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000080 return pass_count_;
81 }
82
83 private:
84 // Wait/Signal forms a classical semaphore on |being_passed_|.
85 // These functions must be called with crit_sect_ held.
86 bool WaitUntilBatonOffered(int timeout_ms) {
87 while (!being_passed_) {
Tommie8493322016-01-20 13:36:31 +010088 if (!cond_var_.SleepCS(&crit_sect_, timeout_ms)) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +000089 return false;
90 }
91 }
92 being_passed_ = false;
Tommie8493322016-01-20 13:36:31 +010093 cond_var_.Wake();
hta@webrtc.org72e3a892012-06-19 13:49:48 +000094 return true;
95 }
96
97 void SignalBatonAvailable() {
98 assert(!being_passed_);
99 being_passed_ = true;
Tommie8493322016-01-20 13:36:31 +0100100 cond_var_.Wake();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000101 }
102
103 // Timeout extension: Wait for a limited time for someone else to
104 // take it, and take it if it's not taken.
105 // Returns true if resource is taken by someone else, false
106 // if it is taken back by the caller.
107 // This function must be called with both |giver_sect_| and
108 // |crit_sect_| held.
109 bool TakeBatonIfStillFree(int timeout_ms) {
110 bool not_timeout = true;
111 while (being_passed_ && not_timeout) {
Tommie8493322016-01-20 13:36:31 +0100112 not_timeout = cond_var_.SleepCS(&crit_sect_, timeout_ms);
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000113 // If we're woken up while variable is still held, we may have
114 // gotten a wakeup destined for a grabber thread.
115 // This situation is not treated specially here.
116 }
Tommie8493322016-01-20 13:36:31 +0100117 if (!being_passed_)
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000118 return true;
Tommie8493322016-01-20 13:36:31 +0100119 assert(!not_timeout);
120 being_passed_ = false;
121 return false;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000122 }
123
124 // Lock that ensures that there is only one thread in the active
125 // part of Pass() at a time.
126 // |giver_sect_| must always be acquired before |cond_var_|.
Tommie8493322016-01-20 13:36:31 +0100127 CriticalSectionWrapper giver_sect_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000128 // Lock that protects |being_passed_|.
Tommie8493322016-01-20 13:36:31 +0100129 CRITICAL_SECTION crit_sect_;
130 ConditionVariableEventWin cond_var_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000131 bool being_passed_;
132 // Statistics information: Number of successfull passes.
133 int pass_count_;
134};
135
136// Function that waits on a Baton, and passes it right back.
137// We expect these calls never to time out.
138bool WaitingRunFunction(void* obj) {
andrew@webrtc.org50419b02012-11-14 19:07:54 +0000139 Baton* the_baton = static_cast<Baton*> (obj);
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000140 EXPECT_TRUE(the_baton->Grab(kLongWaitMs));
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000141 EXPECT_TRUE(the_baton->Pass(kLongWaitMs));
142 return true;
143}
144
145class CondVarTest : public ::testing::Test {
146 public:
Peter Boström8c38e8b2015-11-26 17:45:47 +0100147 CondVarTest() : thread_(&WaitingRunFunction, &baton_, "CondVarTest") {}
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000148
149 virtual void SetUp() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100150 thread_.Start();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000151 }
152
153 virtual void TearDown() {
154 // We have to wake the thread in order to make it obey the stop order.
155 // But we don't know if the thread has completed the run function, so
156 // we don't know if it will exit before or after the Pass.
157 // Thus, we need to pin it down inside its Run function (between Grab
158 // and Pass).
159 ASSERT_TRUE(baton_.Pass(kShortWaitMs));
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000160 ASSERT_TRUE(baton_.Grab(kShortWaitMs));
Peter Boström8c38e8b2015-11-26 17:45:47 +0100161 thread_.Stop();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000162 }
163
164 protected:
165 Baton baton_;
166
167 private:
Peter Boström8c38e8b2015-11-26 17:45:47 +0100168 rtc::PlatformThread thread_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000169};
170
171// The SetUp and TearDown functions use condition variables.
172// This test verifies those pieces in isolation.
tommi@webrtc.org5d32f432015-02-05 06:25:35 +0000173// Disabled due to flakiness. See bug 4262 for details.
174TEST_F(CondVarTest, DISABLED_InitFunctionsWork) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000175 // All relevant asserts are in the SetUp and TearDown functions.
176}
177
178// This test verifies that one can use the baton multiple times.
bjornv@webrtc.orgdc096f22015-02-04 09:14:14 +0000179TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000180 const int kNumberOfRounds = 2;
181 for (int i = 0; i < kNumberOfRounds; ++i) {
182 ASSERT_TRUE(baton_.Pass(kShortWaitMs));
183 ASSERT_TRUE(baton_.Grab(kShortWaitMs));
184 }
phoglund@webrtc.orga36d75a2012-11-14 09:55:04 +0000185 EXPECT_EQ(2 * kNumberOfRounds, baton_.PassCount());
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000186}
187
hta3866c4f2015-10-20 09:31:01 -0700188TEST(CondVarWaitTest, WaitingWaits) {
Tommie8493322016-01-20 13:36:31 +0100189 CRITICAL_SECTION crit_sect;
190 InitializeCriticalSection(&crit_sect);
191 ConditionVariableEventWin cond_var;
192 EnterCriticalSection(&crit_sect);
Niels Möllerd28db7f2016-05-10 16:31:47 +0200193 int64_t start_ms = rtc::TimeMillis();
Tommie8493322016-01-20 13:36:31 +0100194 EXPECT_FALSE(cond_var.SleepCS(&crit_sect, kVeryShortWaitMs));
Niels Möllerd28db7f2016-05-10 16:31:47 +0200195 int64_t end_ms = rtc::TimeMillis();
hta3866c4f2015-10-20 09:31:01 -0700196 EXPECT_LE(start_ms + kVeryShortWaitMs, end_ms)
197 << "actual elapsed:" << end_ms - start_ms;
Tommie8493322016-01-20 13:36:31 +0100198 LeaveCriticalSection(&crit_sect);
199 DeleteCriticalSection(&crit_sect);
hta3866c4f2015-10-20 09:31:01 -0700200}
201
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000202} // anonymous namespace
203
204} // namespace webrtc
tommicd255cc2016-01-19 13:13:14 -0800205
206#endif // defined(WEBRTC_WIN)