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 virtual void Run() override { |
21 *ptr_ = val_; | 21 *ptr_ = val_; |
22 } | 22 } |
23 | 23 |
24 private: | 24 private: |
25 int* ptr_; | 25 int* ptr_; |
26 int val_; | 26 int val_; |
27 }; | 27 }; |
28 | 28 |
29 class WaitEventRunner : public DelegateSimpleThread::Delegate { | 29 class WaitEventRunner : public DelegateSimpleThread::Delegate { |
30 public: | 30 public: |
31 explicit WaitEventRunner(WaitableEvent* event) : event_(event) { } | 31 explicit WaitEventRunner(WaitableEvent* event) : event_(event) { } |
32 virtual ~WaitEventRunner() { } | 32 virtual ~WaitEventRunner() { } |
33 | 33 |
34 virtual void Run() OVERRIDE { | 34 virtual void Run() override { |
35 EXPECT_FALSE(event_->IsSignaled()); | 35 EXPECT_FALSE(event_->IsSignaled()); |
36 event_->Signal(); | 36 event_->Signal(); |
37 EXPECT_TRUE(event_->IsSignaled()); | 37 EXPECT_TRUE(event_->IsSignaled()); |
38 } | 38 } |
39 private: | 39 private: |
40 WaitableEvent* event_; | 40 WaitableEvent* event_; |
41 }; | 41 }; |
42 | 42 |
43 class SeqRunner : public DelegateSimpleThread::Delegate { | 43 class SeqRunner : public DelegateSimpleThread::Delegate { |
44 public: | 44 public: |
45 explicit SeqRunner(AtomicSequenceNumber* seq) : seq_(seq) { } | 45 explicit SeqRunner(AtomicSequenceNumber* seq) : seq_(seq) { } |
46 virtual void Run() OVERRIDE { | 46 virtual void Run() override { |
47 seq_->GetNext(); | 47 seq_->GetNext(); |
48 } | 48 } |
49 | 49 |
50 private: | 50 private: |
51 AtomicSequenceNumber* seq_; | 51 AtomicSequenceNumber* seq_; |
52 }; | 52 }; |
53 | 53 |
54 // We count up on a sequence number, firing on the event when we've hit our | 54 // 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 | 55 // 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. | 56 // have all threads outstanding until we hit our expected thread pool size. |
57 class VerifyPoolRunner : public DelegateSimpleThread::Delegate { | 57 class VerifyPoolRunner : public DelegateSimpleThread::Delegate { |
58 public: | 58 public: |
59 VerifyPoolRunner(AtomicSequenceNumber* seq, | 59 VerifyPoolRunner(AtomicSequenceNumber* seq, |
60 int total, WaitableEvent* event) | 60 int total, WaitableEvent* event) |
61 : seq_(seq), total_(total), event_(event) { } | 61 : seq_(seq), total_(total), event_(event) { } |
62 | 62 |
63 virtual void Run() OVERRIDE { | 63 virtual void Run() override { |
64 if (seq_->GetNext() == total_) { | 64 if (seq_->GetNext() == total_) { |
65 event_->Signal(); | 65 event_->Signal(); |
66 } else { | 66 } else { |
67 event_->Wait(); | 67 event_->Wait(); |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 AtomicSequenceNumber* seq_; | 72 AtomicSequenceNumber* seq_; |
73 int total_; | 73 int total_; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 VerifyPoolRunner verifier(&seq2, 9, &event); | 161 VerifyPoolRunner verifier(&seq2, 9, &event); |
162 pool.Start(); | 162 pool.Start(); |
163 | 163 |
164 pool.AddWork(&verifier, 10); | 164 pool.AddWork(&verifier, 10); |
165 | 165 |
166 pool.JoinAll(); | 166 pool.JoinAll(); |
167 EXPECT_EQ(seq2.GetNext(), 10); | 167 EXPECT_EQ(seq2.GetNext(), 10); |
168 } | 168 } |
169 | 169 |
170 } // namespace base | 170 } // namespace base |
OLD | NEW |