Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: webrtc/system_wrappers/source/critical_section_unittest.cc

Issue 2790533002: Remove ALL usage of CriticalSectionWrapper. (Closed)
Patch Set: remove winXP rw_lock include Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
11 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
12
13 #include "webrtc/base/platform_thread.h"
14 #include "webrtc/system_wrappers/include/sleep.h"
15 #include "webrtc/test/gtest.h"
16
17 namespace webrtc {
18
19 namespace {
20
21 // Cause a process switch. Needed to avoid depending on
22 // busy-wait in tests.
23 static void SwitchProcess() {
24 // Note - sched_yield has been tried as process switch. This does
25 // not cause a process switch enough of the time for reliability.
26 SleepMs(1);
27 }
28
29 class ProtectedCount {
30 public:
31 explicit ProtectedCount(CriticalSectionWrapper* crit_sect)
32 : crit_sect_(crit_sect),
33 count_(0) {
34 }
35
36 void Increment() {
37 CriticalSectionScoped cs(crit_sect_);
38 ++count_;
39 }
40
41 int Count() const {
42 CriticalSectionScoped cs(crit_sect_);
43 return count_;
44 }
45
46 private:
47 CriticalSectionWrapper* crit_sect_;
48 int count_;
49 };
50
51 class CritSectTest : public ::testing::Test {
52 public:
53 CritSectTest() {}
54
55 // Waits a number of cycles for the count to reach a given value.
56 // Returns true if the target is reached or passed.
57 bool WaitForCount(int target, ProtectedCount* count) {
58 int loop_counter = 0;
59 // On Posix, this SwitchProcess() needs to be in a loop to make the
60 // test both fast and non-flaky.
61 // With 1 us wait as the switch, up to 7 rounds have been observed.
62 while (count->Count() < target && loop_counter < 100 * target) {
63 ++loop_counter;
64 SwitchProcess();
65 }
66 return (count->Count() >= target);
67 }
68 };
69
70 void LockUnlockThenStopRunFunction(void* obj) {
71 ProtectedCount* the_count = static_cast<ProtectedCount*>(obj);
72 the_count->Increment();
73 }
74
75 TEST_F(CritSectTest, ThreadWakesOnce) NO_THREAD_SAFETY_ANALYSIS {
76 CriticalSectionWrapper* crit_sect =
77 CriticalSectionWrapper::CreateCriticalSection();
78 ProtectedCount count(crit_sect);
79 rtc::PlatformThread thread(
80 &LockUnlockThenStopRunFunction, &count, "ThreadWakesOnce");
81 crit_sect->Enter();
82 thread.Start();
83 SwitchProcess();
84 // The critical section is of reentrant mode, so this should not release
85 // the lock, even though count.Count() locks and unlocks the critical section
86 // again.
87 // Thus, the thread should not be able to increment the count
88 ASSERT_EQ(0, count.Count());
89 crit_sect->Leave(); // This frees the thread to act.
90 EXPECT_TRUE(WaitForCount(1, &count));
91 thread.Stop();
92 delete crit_sect;
93 }
94
95 bool LockUnlockRunFunction(void* obj) {
96 ProtectedCount* the_count = static_cast<ProtectedCount*>(obj);
97 the_count->Increment();
98 SwitchProcess();
99 return true;
100 }
101
102 TEST_F(CritSectTest, ThreadWakesTwice) NO_THREAD_SAFETY_ANALYSIS {
103 CriticalSectionWrapper* crit_sect =
104 CriticalSectionWrapper::CreateCriticalSection();
105 ProtectedCount count(crit_sect);
106 rtc::PlatformThread thread(
107 &LockUnlockRunFunction, &count, "ThreadWakesTwice");
108 crit_sect->Enter(); // Make sure counter stays 0 until we wait for it.
109 thread.Start();
110 crit_sect->Leave();
111
112 // The thread is capable of grabbing the lock multiple times,
113 // incrementing counter once each time.
114 // It's possible for the count to be incremented by more than 2.
115 EXPECT_TRUE(WaitForCount(2, &count));
116 EXPECT_LE(2, count.Count());
117
118 // The thread does not increment while lock is held.
119 crit_sect->Enter();
120 int count_before = count.Count();
121 for (int i = 0; i < 10; i++) {
122 SwitchProcess();
123 }
124 EXPECT_EQ(count_before, count.Count());
125 crit_sect->Leave();
126
127 SwitchProcess();
128 EXPECT_TRUE(WaitForCount(count_before + 1, &count));
129 thread.Stop();
130 delete crit_sect;
131 }
132
133 } // anonymous namespace
134
135 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/system_wrappers/source/condition_variable_unittest.cc ('k') | webrtc/system_wrappers/source/rw_lock.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698