| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <memory> |
| 12 |
| 11 #include "webrtc/base/common.h" | 13 #include "webrtc/base/common.h" |
| 12 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
| 13 #include "webrtc/base/messagehandler.h" | 15 #include "webrtc/base/messagehandler.h" |
| 14 #include "webrtc/base/messagequeue.h" | 16 #include "webrtc/base/messagequeue.h" |
| 15 #include "webrtc/base/scoped_ptr.h" | |
| 16 #include "webrtc/base/sharedexclusivelock.h" | 17 #include "webrtc/base/sharedexclusivelock.h" |
| 17 #include "webrtc/base/thread.h" | 18 #include "webrtc/base/thread.h" |
| 18 #include "webrtc/base/timeutils.h" | 19 #include "webrtc/base/timeutils.h" |
| 19 | 20 |
| 20 namespace rtc { | 21 namespace rtc { |
| 21 | 22 |
| 22 static const uint32_t kMsgRead = 0; | 23 static const uint32_t kMsgRead = 0; |
| 23 static const uint32_t kMsgWrite = 0; | 24 static const uint32_t kMsgWrite = 0; |
| 24 static const int kNoWaitThresholdInMs = 10; | 25 static const int kNoWaitThresholdInMs = 10; |
| 25 static const int kWaitThresholdInMs = 80; | 26 static const int kWaitThresholdInMs = 80; |
| 26 static const int kProcessTimeInMs = 100; | 27 static const int kProcessTimeInMs = 100; |
| 27 static const int kProcessTimeoutInMs = 5000; | 28 static const int kProcessTimeoutInMs = 5000; |
| 28 | 29 |
| 29 class SharedExclusiveTask : public MessageHandler { | 30 class SharedExclusiveTask : public MessageHandler { |
| 30 public: | 31 public: |
| 31 SharedExclusiveTask(SharedExclusiveLock* shared_exclusive_lock, | 32 SharedExclusiveTask(SharedExclusiveLock* shared_exclusive_lock, |
| 32 int* value, | 33 int* value, |
| 33 bool* done) | 34 bool* done) |
| 34 : shared_exclusive_lock_(shared_exclusive_lock), | 35 : shared_exclusive_lock_(shared_exclusive_lock), |
| 35 waiting_time_in_ms_(0), | 36 waiting_time_in_ms_(0), |
| 36 value_(value), | 37 value_(value), |
| 37 done_(done) { | 38 done_(done) { |
| 38 worker_thread_.reset(new Thread()); | 39 worker_thread_.reset(new Thread()); |
| 39 worker_thread_->Start(); | 40 worker_thread_->Start(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 int waiting_time_in_ms() const { return waiting_time_in_ms_; } | 43 int waiting_time_in_ms() const { return waiting_time_in_ms_; } |
| 43 | 44 |
| 44 protected: | 45 protected: |
| 45 scoped_ptr<Thread> worker_thread_; | 46 std::unique_ptr<Thread> worker_thread_; |
| 46 SharedExclusiveLock* shared_exclusive_lock_; | 47 SharedExclusiveLock* shared_exclusive_lock_; |
| 47 int waiting_time_in_ms_; | 48 int waiting_time_in_ms_; |
| 48 int* value_; | 49 int* value_; |
| 49 bool* done_; | 50 bool* done_; |
| 50 }; | 51 }; |
| 51 | 52 |
| 52 class ReadTask : public SharedExclusiveTask { | 53 class ReadTask : public SharedExclusiveTask { |
| 53 public: | 54 public: |
| 54 ReadTask(SharedExclusiveLock* shared_exclusive_lock, int* value, bool* done) | 55 ReadTask(SharedExclusiveLock* shared_exclusive_lock, int* value, bool* done) |
| 55 : SharedExclusiveTask(shared_exclusive_lock, value, done) { | 56 : SharedExclusiveTask(shared_exclusive_lock, value, done) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 : public testing::Test { | 121 : public testing::Test { |
| 121 public: | 122 public: |
| 122 SharedExclusiveLockTest() : value_(0) { | 123 SharedExclusiveLockTest() : value_(0) { |
| 123 } | 124 } |
| 124 | 125 |
| 125 virtual void SetUp() { | 126 virtual void SetUp() { |
| 126 shared_exclusive_lock_.reset(new SharedExclusiveLock()); | 127 shared_exclusive_lock_.reset(new SharedExclusiveLock()); |
| 127 } | 128 } |
| 128 | 129 |
| 129 protected: | 130 protected: |
| 130 scoped_ptr<SharedExclusiveLock> shared_exclusive_lock_; | 131 std::unique_ptr<SharedExclusiveLock> shared_exclusive_lock_; |
| 131 int value_; | 132 int value_; |
| 132 }; | 133 }; |
| 133 | 134 |
| 134 // Flaky: https://code.google.com/p/webrtc/issues/detail?id=3318 | 135 // Flaky: https://code.google.com/p/webrtc/issues/detail?id=3318 |
| 135 TEST_F(SharedExclusiveLockTest, TestSharedShared) { | 136 TEST_F(SharedExclusiveLockTest, TestSharedShared) { |
| 136 int value0, value1; | 137 int value0, value1; |
| 137 bool done0, done1; | 138 bool done0, done1; |
| 138 ReadTask reader0(shared_exclusive_lock_.get(), &value_, &done0); | 139 ReadTask reader0(shared_exclusive_lock_.get(), &value_, &done0); |
| 139 ReadTask reader1(shared_exclusive_lock_.get(), &value_, &done1); | 140 ReadTask reader1(shared_exclusive_lock_.get(), &value_, &done1); |
| 140 | 141 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 Thread::SleepMs(kProcessTimeInMs); | 210 Thread::SleepMs(kProcessTimeInMs); |
| 210 EXPECT_EQ(1, value_); | 211 EXPECT_EQ(1, value_); |
| 211 } | 212 } |
| 212 | 213 |
| 213 EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs); | 214 EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs); |
| 214 EXPECT_EQ(2, value_); | 215 EXPECT_EQ(2, value_); |
| 215 EXPECT_GE(writer.waiting_time_in_ms(), kWaitThresholdInMs); | 216 EXPECT_GE(writer.waiting_time_in_ms(), kWaitThresholdInMs); |
| 216 } | 217 } |
| 217 | 218 |
| 218 } // namespace rtc | 219 } // namespace rtc |
| OLD | NEW |