blob: de1dbe812201e1aebb72f3cc5557f1d9afbf0d23 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2011 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/base/common.h"
12#include "webrtc/base/gunit.h"
13#include "webrtc/base/messagehandler.h"
14#include "webrtc/base/messagequeue.h"
15#include "webrtc/base/scoped_ptr.h"
16#include "webrtc/base/sharedexclusivelock.h"
17#include "webrtc/base/thread.h"
18#include "webrtc/base/timeutils.h"
19
20namespace rtc {
21
22static const uint32 kMsgRead = 0;
23static const uint32 kMsgWrite = 0;
24static const int kNoWaitThresholdInMs = 10;
25static const int kWaitThresholdInMs = 80;
26static const int kProcessTimeInMs = 100;
27static const int kProcessTimeoutInMs = 5000;
28
29class SharedExclusiveTask : public MessageHandler {
30 public:
31 SharedExclusiveTask(SharedExclusiveLock* shared_exclusive_lock,
32 int* value,
33 bool* done)
34 : shared_exclusive_lock_(shared_exclusive_lock),
35 waiting_time_in_ms_(0),
36 value_(value),
37 done_(done) {
38 worker_thread_.reset(new Thread());
39 worker_thread_->Start();
40 }
41
42 int waiting_time_in_ms() const { return waiting_time_in_ms_; }
43
44 protected:
45 scoped_ptr<Thread> worker_thread_;
46 SharedExclusiveLock* shared_exclusive_lock_;
47 int waiting_time_in_ms_;
48 int* value_;
49 bool* done_;
50};
51
52class ReadTask : public SharedExclusiveTask {
53 public:
54 ReadTask(SharedExclusiveLock* shared_exclusive_lock, int* value, bool* done)
55 : SharedExclusiveTask(shared_exclusive_lock, value, done) {
56 }
57
58 void PostRead(int* value) {
59 worker_thread_->Post(this, kMsgRead, new TypedMessageData<int*>(value));
60 }
61
62 private:
63 virtual void OnMessage(Message* message) {
64 ASSERT(rtc::Thread::Current() == worker_thread_.get());
65 ASSERT(message != NULL);
66 ASSERT(message->message_id == kMsgRead);
67
68 TypedMessageData<int*>* message_data =
69 static_cast<TypedMessageData<int*>*>(message->pdata);
70
71 uint32 start_time = Time();
72 {
73 SharedScope ss(shared_exclusive_lock_);
74 waiting_time_in_ms_ = TimeDiff(Time(), start_time);
75
76 Thread::SleepMs(kProcessTimeInMs);
77 *message_data->data() = *value_;
78 *done_ = true;
79 }
80 delete message->pdata;
81 message->pdata = NULL;
82 }
83};
84
85class WriteTask : public SharedExclusiveTask {
86 public:
87 WriteTask(SharedExclusiveLock* shared_exclusive_lock, int* value, bool* done)
88 : SharedExclusiveTask(shared_exclusive_lock, value, done) {
89 }
90
91 void PostWrite(int value) {
92 worker_thread_->Post(this, kMsgWrite, new TypedMessageData<int>(value));
93 }
94
95 private:
96 virtual void OnMessage(Message* message) {
97 ASSERT(rtc::Thread::Current() == worker_thread_.get());
98 ASSERT(message != NULL);
99 ASSERT(message->message_id == kMsgWrite);
100
101 TypedMessageData<int>* message_data =
102 static_cast<TypedMessageData<int>*>(message->pdata);
103
104 uint32 start_time = Time();
105 {
106 ExclusiveScope es(shared_exclusive_lock_);
107 waiting_time_in_ms_ = TimeDiff(Time(), start_time);
108
109 Thread::SleepMs(kProcessTimeInMs);
110 *value_ = message_data->data();
111 *done_ = true;
112 }
113 delete message->pdata;
114 message->pdata = NULL;
115 }
116};
117
118// Unit test for SharedExclusiveLock.
119class SharedExclusiveLockTest
120 : public testing::Test {
121 public:
122 SharedExclusiveLockTest() : value_(0) {
123 }
124
125 virtual void SetUp() {
126 shared_exclusive_lock_.reset(new SharedExclusiveLock());
127 }
128
129 protected:
130 scoped_ptr<SharedExclusiveLock> shared_exclusive_lock_;
131 int value_;
132};
133
134TEST_F(SharedExclusiveLockTest, TestSharedShared) {
135 int value0, value1;
136 bool done0, done1;
137 ReadTask reader0(shared_exclusive_lock_.get(), &value_, &done0);
138 ReadTask reader1(shared_exclusive_lock_.get(), &value_, &done1);
139
140 // Test shared locks can be shared without waiting.
141 {
142 SharedScope ss(shared_exclusive_lock_.get());
143 value_ = 1;
144 done0 = false;
145 done1 = false;
146 reader0.PostRead(&value0);
147 reader1.PostRead(&value1);
148 Thread::SleepMs(kProcessTimeInMs);
149 }
150
151 EXPECT_TRUE_WAIT(done0, kProcessTimeoutInMs);
152 EXPECT_EQ(1, value0);
153 EXPECT_LE(reader0.waiting_time_in_ms(), kNoWaitThresholdInMs);
154 EXPECT_TRUE_WAIT(done1, kProcessTimeoutInMs);
155 EXPECT_EQ(1, value1);
156 EXPECT_LE(reader1.waiting_time_in_ms(), kNoWaitThresholdInMs);
157}
158
159TEST_F(SharedExclusiveLockTest, TestSharedExclusive) {
160 bool done;
161 WriteTask writer(shared_exclusive_lock_.get(), &value_, &done);
162
163 // Test exclusive lock needs to wait for shared lock.
164 {
165 SharedScope ss(shared_exclusive_lock_.get());
166 value_ = 1;
167 done = false;
168 writer.PostWrite(2);
169 Thread::SleepMs(kProcessTimeInMs);
170 EXPECT_EQ(1, value_);
171 }
172
173 EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs);
174 EXPECT_EQ(2, value_);
175 EXPECT_GE(writer.waiting_time_in_ms(), kWaitThresholdInMs);
176}
177
178TEST_F(SharedExclusiveLockTest, TestExclusiveShared) {
179 int value;
180 bool done;
181 ReadTask reader(shared_exclusive_lock_.get(), &value_, &done);
182
183 // Test shared lock needs to wait for exclusive lock.
184 {
185 ExclusiveScope es(shared_exclusive_lock_.get());
186 value_ = 1;
187 done = false;
188 reader.PostRead(&value);
189 Thread::SleepMs(kProcessTimeInMs);
190 value_ = 2;
191 }
192
193 EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs);
194 EXPECT_EQ(2, value);
195 EXPECT_GE(reader.waiting_time_in_ms(), kWaitThresholdInMs);
196}
197
198TEST_F(SharedExclusiveLockTest, TestExclusiveExclusive) {
199 bool done;
200 WriteTask writer(shared_exclusive_lock_.get(), &value_, &done);
201
202 // Test exclusive lock needs to wait for exclusive lock.
203 {
204 ExclusiveScope es(shared_exclusive_lock_.get());
205 value_ = 1;
206 done = false;
207 writer.PostWrite(2);
208 Thread::SleepMs(kProcessTimeInMs);
209 EXPECT_EQ(1, value_);
210 }
211
212 EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs);
213 EXPECT_EQ(2, value_);
214 EXPECT_GE(writer.waiting_time_in_ms(), kWaitThresholdInMs);
215}
216
217} // namespace rtc