You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/common.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/messagehandler.h"
  30. #include "talk/base/messagequeue.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/sharedexclusivelock.h"
  33. #include "talk/base/thread.h"
  34. #include "talk/base/timeutils.h"
  35. namespace talk_base {
  36. static const uint32 kMsgRead = 0;
  37. static const uint32 kMsgWrite = 0;
  38. static const int kNoWaitThresholdInMs = 10;
  39. static const int kWaitThresholdInMs = 80;
  40. static const int kProcessTimeInMs = 100;
  41. static const int kProcessTimeoutInMs = 5000;
  42. class SharedExclusiveTask : public MessageHandler {
  43. public:
  44. SharedExclusiveTask(SharedExclusiveLock* shared_exclusive_lock,
  45. int* value,
  46. bool* done)
  47. : shared_exclusive_lock_(shared_exclusive_lock),
  48. waiting_time_in_ms_(0),
  49. value_(value),
  50. done_(done) {
  51. worker_thread_.reset(new Thread());
  52. worker_thread_->Start();
  53. }
  54. int waiting_time_in_ms() const { return waiting_time_in_ms_; }
  55. protected:
  56. scoped_ptr<Thread> worker_thread_;
  57. SharedExclusiveLock* shared_exclusive_lock_;
  58. int waiting_time_in_ms_;
  59. int* value_;
  60. bool* done_;
  61. };
  62. class ReadTask : public SharedExclusiveTask {
  63. public:
  64. ReadTask(SharedExclusiveLock* shared_exclusive_lock, int* value, bool* done)
  65. : SharedExclusiveTask(shared_exclusive_lock, value, done) {
  66. }
  67. void PostRead(int* value) {
  68. worker_thread_->Post(this, kMsgRead, new TypedMessageData<int*>(value));
  69. }
  70. private:
  71. virtual void OnMessage(Message* message) {
  72. ASSERT(talk_base::Thread::Current() == worker_thread_.get());
  73. ASSERT(message != NULL);
  74. ASSERT(message->message_id == kMsgRead);
  75. TypedMessageData<int*>* message_data =
  76. static_cast<TypedMessageData<int*>*>(message->pdata);
  77. uint32 start_time = Time();
  78. {
  79. SharedScope ss(shared_exclusive_lock_);
  80. waiting_time_in_ms_ = TimeDiff(Time(), start_time);
  81. Thread::SleepMs(kProcessTimeInMs);
  82. *message_data->data() = *value_;
  83. *done_ = true;
  84. }
  85. delete message->pdata;
  86. message->pdata = NULL;
  87. }
  88. };
  89. class WriteTask : public SharedExclusiveTask {
  90. public:
  91. WriteTask(SharedExclusiveLock* shared_exclusive_lock, int* value, bool* done)
  92. : SharedExclusiveTask(shared_exclusive_lock, value, done) {
  93. }
  94. void PostWrite(int value) {
  95. worker_thread_->Post(this, kMsgWrite, new TypedMessageData<int>(value));
  96. }
  97. private:
  98. virtual void OnMessage(Message* message) {
  99. ASSERT(talk_base::Thread::Current() == worker_thread_.get());
  100. ASSERT(message != NULL);
  101. ASSERT(message->message_id == kMsgWrite);
  102. TypedMessageData<int>* message_data =
  103. static_cast<TypedMessageData<int>*>(message->pdata);
  104. uint32 start_time = Time();
  105. {
  106. ExclusiveScope es(shared_exclusive_lock_);
  107. waiting_time_in_ms_ = TimeDiff(Time(), start_time);
  108. Thread::SleepMs(kProcessTimeInMs);
  109. *value_ = message_data->data();
  110. *done_ = true;
  111. }
  112. delete message->pdata;
  113. message->pdata = NULL;
  114. }
  115. };
  116. // Unit test for SharedExclusiveLock.
  117. class SharedExclusiveLockTest
  118. : public testing::Test {
  119. public:
  120. SharedExclusiveLockTest() : value_(0) {
  121. }
  122. virtual void SetUp() {
  123. shared_exclusive_lock_.reset(new SharedExclusiveLock());
  124. }
  125. protected:
  126. scoped_ptr<SharedExclusiveLock> shared_exclusive_lock_;
  127. int value_;
  128. };
  129. TEST_F(SharedExclusiveLockTest, TestSharedShared) {
  130. int value0, value1;
  131. bool done0, done1;
  132. ReadTask reader0(shared_exclusive_lock_.get(), &value_, &done0);
  133. ReadTask reader1(shared_exclusive_lock_.get(), &value_, &done1);
  134. // Test shared locks can be shared without waiting.
  135. {
  136. SharedScope ss(shared_exclusive_lock_.get());
  137. value_ = 1;
  138. done0 = false;
  139. done1 = false;
  140. reader0.PostRead(&value0);
  141. reader1.PostRead(&value1);
  142. Thread::SleepMs(kProcessTimeInMs);
  143. }
  144. EXPECT_TRUE_WAIT(done0, kProcessTimeoutInMs);
  145. EXPECT_EQ(1, value0);
  146. EXPECT_LE(reader0.waiting_time_in_ms(), kNoWaitThresholdInMs);
  147. EXPECT_TRUE_WAIT(done1, kProcessTimeoutInMs);
  148. EXPECT_EQ(1, value1);
  149. EXPECT_LE(reader1.waiting_time_in_ms(), kNoWaitThresholdInMs);
  150. }
  151. TEST_F(SharedExclusiveLockTest, TestSharedExclusive) {
  152. bool done;
  153. WriteTask writer(shared_exclusive_lock_.get(), &value_, &done);
  154. // Test exclusive lock needs to wait for shared lock.
  155. {
  156. SharedScope ss(shared_exclusive_lock_.get());
  157. value_ = 1;
  158. done = false;
  159. writer.PostWrite(2);
  160. Thread::SleepMs(kProcessTimeInMs);
  161. EXPECT_EQ(1, value_);
  162. }
  163. EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs);
  164. EXPECT_EQ(2, value_);
  165. EXPECT_GE(writer.waiting_time_in_ms(), kWaitThresholdInMs);
  166. }
  167. TEST_F(SharedExclusiveLockTest, TestExclusiveShared) {
  168. int value;
  169. bool done;
  170. ReadTask reader(shared_exclusive_lock_.get(), &value_, &done);
  171. // Test shared lock needs to wait for exclusive lock.
  172. {
  173. ExclusiveScope es(shared_exclusive_lock_.get());
  174. value_ = 1;
  175. done = false;
  176. reader.PostRead(&value);
  177. Thread::SleepMs(kProcessTimeInMs);
  178. value_ = 2;
  179. }
  180. EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs);
  181. EXPECT_EQ(2, value);
  182. EXPECT_GE(reader.waiting_time_in_ms(), kWaitThresholdInMs);
  183. }
  184. TEST_F(SharedExclusiveLockTest, TestExclusiveExclusive) {
  185. bool done;
  186. WriteTask writer(shared_exclusive_lock_.get(), &value_, &done);
  187. // Test exclusive lock needs to wait for exclusive lock.
  188. {
  189. ExclusiveScope es(shared_exclusive_lock_.get());
  190. value_ = 1;
  191. done = false;
  192. writer.PostWrite(2);
  193. Thread::SleepMs(kProcessTimeInMs);
  194. EXPECT_EQ(1, value_);
  195. }
  196. EXPECT_TRUE_WAIT(done, kProcessTimeoutInMs);
  197. EXPECT_EQ(2, value_);
  198. EXPECT_GE(writer.waiting_time_in_ms(), kWaitThresholdInMs);
  199. }
  200. } // namespace talk_base