OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/atomic_sequence_num.h" | 5 #include "base/atomic_sequence_num.h" |
6 #include "base/strings/string_number_conversions.h" | 6 #include "base/strings/string_number_conversions.h" |
7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "base/threading/simple_thread.h" | 8 #include "base/threading/simple_thread.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 class SetIntRunner : public DelegateSimpleThread::Delegate { | 15 class SetIntRunner : public DelegateSimpleThread::Delegate { |
16 public: | 16 public: |
17 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { } | 17 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { } |
18 virtual ~SetIntRunner() { } | 18 virtual ~SetIntRunner() { } |
19 | 19 |
20 virtual void Run() OVERRIDE { | 20 void Run() override { *ptr_ = val_; } |
21 *ptr_ = val_; | |
22 } | |
23 | 21 |
24 private: | 22 private: |
25 int* ptr_; | 23 int* ptr_; |
26 int val_; | 24 int val_; |
27 }; | 25 }; |
28 | 26 |
29 class WaitEventRunner : public DelegateSimpleThread::Delegate { | 27 class WaitEventRunner : public DelegateSimpleThread::Delegate { |
30 public: | 28 public: |
31 explicit WaitEventRunner(WaitableEvent* event) : event_(event) { } | 29 explicit WaitEventRunner(WaitableEvent* event) : event_(event) { } |
32 virtual ~WaitEventRunner() { } | 30 virtual ~WaitEventRunner() { } |
33 | 31 |
34 virtual void Run() OVERRIDE { | 32 void Run() override { |
35 EXPECT_FALSE(event_->IsSignaled()); | 33 EXPECT_FALSE(event_->IsSignaled()); |
36 event_->Signal(); | 34 event_->Signal(); |
37 EXPECT_TRUE(event_->IsSignaled()); | 35 EXPECT_TRUE(event_->IsSignaled()); |
38 } | 36 } |
39 private: | 37 private: |
40 WaitableEvent* event_; | 38 WaitableEvent* event_; |
41 }; | 39 }; |
42 | 40 |
43 class SeqRunner : public DelegateSimpleThread::Delegate { | 41 class SeqRunner : public DelegateSimpleThread::Delegate { |
44 public: | 42 public: |
45 explicit SeqRunner(AtomicSequenceNumber* seq) : seq_(seq) { } | 43 explicit SeqRunner(AtomicSequenceNumber* seq) : seq_(seq) { } |
46 virtual void Run() OVERRIDE { | 44 void Run() override { seq_->GetNext(); } |
47 seq_->GetNext(); | |
48 } | |
49 | 45 |
50 private: | 46 private: |
51 AtomicSequenceNumber* seq_; | 47 AtomicSequenceNumber* seq_; |
52 }; | 48 }; |
53 | 49 |
54 // We count up on a sequence number, firing on the event when we've hit our | 50 // We count up on a sequence number, firing on the event when we've hit our |
55 // expected amount, otherwise we wait on the event. This will ensure that we | 51 // expected amount, otherwise we wait on the event. This will ensure that we |
56 // have all threads outstanding until we hit our expected thread pool size. | 52 // have all threads outstanding until we hit our expected thread pool size. |
57 class VerifyPoolRunner : public DelegateSimpleThread::Delegate { | 53 class VerifyPoolRunner : public DelegateSimpleThread::Delegate { |
58 public: | 54 public: |
59 VerifyPoolRunner(AtomicSequenceNumber* seq, | 55 VerifyPoolRunner(AtomicSequenceNumber* seq, |
60 int total, WaitableEvent* event) | 56 int total, WaitableEvent* event) |
61 : seq_(seq), total_(total), event_(event) { } | 57 : seq_(seq), total_(total), event_(event) { } |
62 | 58 |
63 virtual void Run() OVERRIDE { | 59 void Run() override { |
64 if (seq_->GetNext() == total_) { | 60 if (seq_->GetNext() == total_) { |
65 event_->Signal(); | 61 event_->Signal(); |
66 } else { | 62 } else { |
67 event_->Wait(); | 63 event_->Wait(); |
68 } | 64 } |
69 } | 65 } |
70 | 66 |
71 private: | 67 private: |
72 AtomicSequenceNumber* seq_; | 68 AtomicSequenceNumber* seq_; |
73 int total_; | 69 int total_; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 VerifyPoolRunner verifier(&seq2, 9, &event); | 157 VerifyPoolRunner verifier(&seq2, 9, &event); |
162 pool.Start(); | 158 pool.Start(); |
163 | 159 |
164 pool.AddWork(&verifier, 10); | 160 pool.AddWork(&verifier, 10); |
165 | 161 |
166 pool.JoinAll(); | 162 pool.JoinAll(); |
167 EXPECT_EQ(seq2.GetNext(), 10); | 163 EXPECT_EQ(seq2.GetNext(), 10); |
168 } | 164 } |
169 | 165 |
170 } // namespace base | 166 } // namespace base |
OLD | NEW |