blob: 90779ff7f75e115cb4cac532d412df36a04a7bc0 [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"
kwibergac9f8762016-09-30 22:29:43 -070019#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
20#include "webrtc/test/gtest.h"
hta@webrtc.org72e3a892012-06-19 13:49:48 +000021
22namespace webrtc {
23
24namespace {
25
phoglund@webrtc.orga36d75a2012-11-14 09:55:04 +000026const int kLongWaitMs = 100 * 1000; // A long time in testing terms
27const int kShortWaitMs = 2 * 1000; // Long enough for process switches to happen
hta3866c4f2015-10-20 09:31:01 -070028const int kVeryShortWaitMs = 20; // Used when we want a timeout
hta@webrtc.org72e3a892012-06-19 13:49:48 +000029
hta@webrtc.org72e3a892012-06-19 13:49:48 +000030// A Baton is one possible control structure one can build using
31// conditional variables.
32// A Baton is always held by one and only one active thread - unlike
33// a lock, it can never be free.
34// One can pass it or grab it - both calls have timeouts.
35// Note - a production tool would guard against passing it without
36// grabbing it first. This one is for testing, so it doesn't.
37class Baton {
38 public:
39 Baton()
Tommie8493322016-01-20 13:36:31 +010040 : being_passed_(false),
hta@webrtc.org72e3a892012-06-19 13:49:48 +000041 pass_count_(0) {
Tommie8493322016-01-20 13:36:31 +010042 InitializeCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000043 }
44
45 ~Baton() {
Tommie8493322016-01-20 13:36:31 +010046 DeleteCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000047 }
48
49 // Pass the baton. Returns false if baton is not picked up in |max_msecs|.
50 // Only one process can pass at the same time; this property is
51 // ensured by the |giver_sect_| lock.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000052 bool Pass(uint32_t max_msecs) {
Tommie8493322016-01-20 13:36:31 +010053 CriticalSectionScoped cs_giver(&giver_sect_);
54 EnterCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000055 SignalBatonAvailable();
56 const bool result = TakeBatonIfStillFree(max_msecs);
57 if (result) {
58 ++pass_count_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +000059 }
Tommie8493322016-01-20 13:36:31 +010060 LeaveCriticalSection(&crit_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000061 return result;
62 }
63
64 // Grab the baton. Returns false if baton is not passed.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000065 bool Grab(uint32_t max_msecs) {
Tommie8493322016-01-20 13:36:31 +010066 EnterCriticalSection(&crit_sect_);
67 bool ret = WaitUntilBatonOffered(max_msecs);
68 LeaveCriticalSection(&crit_sect_);
69 return ret;
hta@webrtc.org72e3a892012-06-19 13:49:48 +000070 }
71
72 int PassCount() {
73 // We don't allow polling PassCount() during a Pass()-call since there is
74 // no guarantee that |pass_count_| is incremented until the Pass()-call
75 // finishes. I.e. the Grab()-call may finish before |pass_count_| has been
76 // incremented.
77 // Thus, this function waits on giver_sect_.
Tommie8493322016-01-20 13:36:31 +010078 CriticalSectionScoped cs(&giver_sect_);
hta@webrtc.org72e3a892012-06-19 13:49:48 +000079 return pass_count_;
80 }
81
82 private:
83 // Wait/Signal forms a classical semaphore on |being_passed_|.
84 // These functions must be called with crit_sect_ held.
85 bool WaitUntilBatonOffered(int timeout_ms) {
86 while (!being_passed_) {
Tommie8493322016-01-20 13:36:31 +010087 if (!cond_var_.SleepCS(&crit_sect_, timeout_ms)) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +000088 return false;
89 }
90 }
91 being_passed_ = false;
Tommie8493322016-01-20 13:36:31 +010092 cond_var_.Wake();
hta@webrtc.org72e3a892012-06-19 13:49:48 +000093 return true;
94 }
95
96 void SignalBatonAvailable() {
97 assert(!being_passed_);
98 being_passed_ = true;
Tommie8493322016-01-20 13:36:31 +010099 cond_var_.Wake();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000100 }
101
102 // Timeout extension: Wait for a limited time for someone else to
103 // take it, and take it if it's not taken.
104 // Returns true if resource is taken by someone else, false
105 // if it is taken back by the caller.
106 // This function must be called with both |giver_sect_| and
107 // |crit_sect_| held.
108 bool TakeBatonIfStillFree(int timeout_ms) {
109 bool not_timeout = true;
110 while (being_passed_ && not_timeout) {
Tommie8493322016-01-20 13:36:31 +0100111 not_timeout = cond_var_.SleepCS(&crit_sect_, timeout_ms);
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000112 // If we're woken up while variable is still held, we may have
113 // gotten a wakeup destined for a grabber thread.
114 // This situation is not treated specially here.
115 }
Tommie8493322016-01-20 13:36:31 +0100116 if (!being_passed_)
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000117 return true;
Tommie8493322016-01-20 13:36:31 +0100118 assert(!not_timeout);
119 being_passed_ = false;
120 return false;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000121 }
122
123 // Lock that ensures that there is only one thread in the active
124 // part of Pass() at a time.
125 // |giver_sect_| must always be acquired before |cond_var_|.
Tommie8493322016-01-20 13:36:31 +0100126 CriticalSectionWrapper giver_sect_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000127 // Lock that protects |being_passed_|.
Tommie8493322016-01-20 13:36:31 +0100128 CRITICAL_SECTION crit_sect_;
129 ConditionVariableEventWin cond_var_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000130 bool being_passed_;
131 // Statistics information: Number of successfull passes.
132 int pass_count_;
133};
134
135// Function that waits on a Baton, and passes it right back.
136// We expect these calls never to time out.
137bool WaitingRunFunction(void* obj) {
andrew@webrtc.org50419b02012-11-14 19:07:54 +0000138 Baton* the_baton = static_cast<Baton*> (obj);
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000139 EXPECT_TRUE(the_baton->Grab(kLongWaitMs));
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000140 EXPECT_TRUE(the_baton->Pass(kLongWaitMs));
141 return true;
142}
143
144class CondVarTest : public ::testing::Test {
145 public:
Peter Boström8c38e8b2015-11-26 17:45:47 +0100146 CondVarTest() : thread_(&WaitingRunFunction, &baton_, "CondVarTest") {}
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000147
148 virtual void SetUp() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100149 thread_.Start();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000150 }
151
152 virtual void TearDown() {
153 // We have to wake the thread in order to make it obey the stop order.
154 // But we don't know if the thread has completed the run function, so
155 // we don't know if it will exit before or after the Pass.
156 // Thus, we need to pin it down inside its Run function (between Grab
157 // and Pass).
158 ASSERT_TRUE(baton_.Pass(kShortWaitMs));
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000159 ASSERT_TRUE(baton_.Grab(kShortWaitMs));
Peter Boström8c38e8b2015-11-26 17:45:47 +0100160 thread_.Stop();
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000161 }
162
163 protected:
164 Baton baton_;
165
166 private:
Peter Boström8c38e8b2015-11-26 17:45:47 +0100167 rtc::PlatformThread thread_;
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000168};
169
170// The SetUp and TearDown functions use condition variables.
171// This test verifies those pieces in isolation.
tommi@webrtc.org5d32f432015-02-05 06:25:35 +0000172// Disabled due to flakiness. See bug 4262 for details.
173TEST_F(CondVarTest, DISABLED_InitFunctionsWork) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000174 // All relevant asserts are in the SetUp and TearDown functions.
175}
176
177// This test verifies that one can use the baton multiple times.
bjornv@webrtc.orgdc096f22015-02-04 09:14:14 +0000178TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) {
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000179 const int kNumberOfRounds = 2;
180 for (int i = 0; i < kNumberOfRounds; ++i) {
181 ASSERT_TRUE(baton_.Pass(kShortWaitMs));
182 ASSERT_TRUE(baton_.Grab(kShortWaitMs));
183 }
phoglund@webrtc.orga36d75a2012-11-14 09:55:04 +0000184 EXPECT_EQ(2 * kNumberOfRounds, baton_.PassCount());
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000185}
186
hta3866c4f2015-10-20 09:31:01 -0700187TEST(CondVarWaitTest, WaitingWaits) {
Tommie8493322016-01-20 13:36:31 +0100188 CRITICAL_SECTION crit_sect;
189 InitializeCriticalSection(&crit_sect);
190 ConditionVariableEventWin cond_var;
191 EnterCriticalSection(&crit_sect);
Niels Möllerd28db7f2016-05-10 16:31:47 +0200192 int64_t start_ms = rtc::TimeMillis();
Tommie8493322016-01-20 13:36:31 +0100193 EXPECT_FALSE(cond_var.SleepCS(&crit_sect, kVeryShortWaitMs));
Niels Möllerd28db7f2016-05-10 16:31:47 +0200194 int64_t end_ms = rtc::TimeMillis();
hta3866c4f2015-10-20 09:31:01 -0700195 EXPECT_LE(start_ms + kVeryShortWaitMs, end_ms)
196 << "actual elapsed:" << end_ms - start_ms;
Tommie8493322016-01-20 13:36:31 +0100197 LeaveCriticalSection(&crit_sect);
198 DeleteCriticalSection(&crit_sect);
hta3866c4f2015-10-20 09:31:01 -0700199}
200
hta@webrtc.org72e3a892012-06-19 13:49:48 +0000201} // anonymous namespace
202
203} // namespace webrtc
tommicd255cc2016-01-19 13:13:14 -0800204
205#endif // defined(WEBRTC_WIN)