| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const uint64_t kNanosecondsPerSecond = static_cast<uint64_t>(1E9); | 25 const uint64_t kNanosecondsPerSecond = static_cast<uint64_t>(1E9); |
| 26 | 26 |
| 27 class WorkDelegate : public WorkerThread::Delegate { | 27 class WorkDelegate : public WorkerThread::Delegate { |
| 28 public: | 28 public: |
| 29 WorkDelegate() {} | 29 WorkDelegate() {} |
| 30 ~WorkDelegate() {} | 30 ~WorkDelegate() {} |
| 31 | 31 |
| 32 void DoWork(const WorkerThread* thread) override { | 32 void DoWork(const WorkerThread* thread) override { |
| 33 if (++work_count_ == waiting_for_count_) | 33 if (work_count_ < waiting_for_count_) { |
| 34 semaphore_.Signal(); | 34 if (++work_count_ == waiting_for_count_) { |
| 35 semaphore_.Signal(); |
| 36 } |
| 37 } |
| 35 } | 38 } |
| 36 | 39 |
| 37 void SetDesiredWorkCount(int times) { | 40 void SetDesiredWorkCount(int times) { |
| 38 waiting_for_count_ = times; | 41 waiting_for_count_ = times; |
| 39 } | 42 } |
| 40 | 43 |
| 41 //! \brief Suspends the calling thread until the DoWork() has been called | 44 //! \brief Suspends the calling thread until the DoWork() has been called |
| 42 //! the number of times specified by SetDesiredWorkCount(). | 45 //! the number of times specified by SetDesiredWorkCount(). |
| 43 void WaitForWorkCount() { | 46 void WaitForWorkCount() { |
| 44 semaphore_.Wait(); | 47 semaphore_.Wait(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 int work_count() const { return work_count_; } | 50 int work_count() const { return work_count_; } |
| 48 | 51 |
| 49 private: | 52 private: |
| 50 Semaphore semaphore_{0}; | 53 Semaphore semaphore_{0}; |
| 51 int work_count_ = 0; | 54 int work_count_ = 0; |
| 52 int waiting_for_count_ = -1; | 55 int waiting_for_count_ = -1; |
| 53 | 56 |
| 54 DISALLOW_COPY_AND_ASSIGN(WorkDelegate); | 57 DISALLOW_COPY_AND_ASSIGN(WorkDelegate); |
| 55 }; | 58 }; |
| 56 | 59 |
| 57 TEST(WorkerThread, DoWork) { | 60 TEST(WorkerThread, DoWork) { |
| 58 WorkDelegate delegate; | 61 WorkDelegate delegate; |
| 59 WorkerThread thread(0.05, &delegate); | 62 WorkerThread thread(0.05, &delegate); |
| 60 | 63 |
| 61 uint64_t start = ClockMonotonicNanoseconds(); | 64 uint64_t start = ClockMonotonicNanoseconds(); |
| 65 |
| 62 delegate.SetDesiredWorkCount(2); | 66 delegate.SetDesiredWorkCount(2); |
| 63 thread.Start(0); | 67 thread.Start(0); |
| 64 EXPECT_TRUE(thread.is_running()); | 68 EXPECT_TRUE(thread.is_running()); |
| 65 | 69 |
| 66 delegate.WaitForWorkCount(); | 70 delegate.WaitForWorkCount(); |
| 67 thread.Stop(); | 71 thread.Stop(); |
| 68 EXPECT_FALSE(thread.is_running()); | 72 EXPECT_FALSE(thread.is_running()); |
| 69 | 73 |
| 70 EXPECT_GE(1 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); | 74 EXPECT_GE(1 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); |
| 71 } | 75 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 96 thread.Start(0); | 100 thread.Start(0); |
| 97 delegate.WaitForWorkCount(); | 101 delegate.WaitForWorkCount(); |
| 98 thread.Stop(); | 102 thread.Stop(); |
| 99 ASSERT_FALSE(thread.is_running()); | 103 ASSERT_FALSE(thread.is_running()); |
| 100 } | 104 } |
| 101 | 105 |
| 102 TEST(WorkerThread, DoWorkNow) { | 106 TEST(WorkerThread, DoWorkNow) { |
| 103 WorkDelegate delegate; | 107 WorkDelegate delegate; |
| 104 WorkerThread thread(100, &delegate); | 108 WorkerThread thread(100, &delegate); |
| 105 | 109 |
| 110 uint64_t start = ClockMonotonicNanoseconds(); |
| 111 |
| 106 delegate.SetDesiredWorkCount(1); | 112 delegate.SetDesiredWorkCount(1); |
| 107 thread.Start(0); | 113 thread.Start(0); |
| 108 EXPECT_TRUE(thread.is_running()); | 114 EXPECT_TRUE(thread.is_running()); |
| 109 | 115 |
| 110 uint64_t start = ClockMonotonicNanoseconds(); | |
| 111 | |
| 112 delegate.WaitForWorkCount(); | 116 delegate.WaitForWorkCount(); |
| 113 EXPECT_EQ(1, delegate.work_count()); | 117 EXPECT_EQ(1, delegate.work_count()); |
| 114 | 118 |
| 115 delegate.SetDesiredWorkCount(2); | 119 delegate.SetDesiredWorkCount(2); |
| 116 thread.DoWorkNow(); | 120 thread.DoWorkNow(); |
| 117 delegate.WaitForWorkCount(); | 121 delegate.WaitForWorkCount(); |
| 118 thread.Stop(); | 122 thread.Stop(); |
| 119 EXPECT_EQ(2, delegate.work_count()); | 123 EXPECT_EQ(2, delegate.work_count()); |
| 120 | 124 |
| 121 EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); | 125 EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 137 | 141 |
| 138 EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); | 142 EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); |
| 139 | 143 |
| 140 thread.Stop(); | 144 thread.Stop(); |
| 141 EXPECT_FALSE(thread.is_running()); | 145 EXPECT_FALSE(thread.is_running()); |
| 142 } | 146 } |
| 143 | 147 |
| 144 } // namespace | 148 } // namespace |
| 145 } // namespace test | 149 } // namespace test |
| 146 } // namespace crashpad | 150 } // namespace crashpad |
| OLD | NEW |