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/observer_list.h" | 5 #include "base/observer_list.h" |
6 #include "base/observer_list_threadsafe.h" | 6 #include "base/observer_list_threadsafe.h" |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 64 |
65 template <typename ObserverListType> | 65 template <typename ObserverListType> |
66 class AddInObserve : public Foo { | 66 class AddInObserve : public Foo { |
67 public: | 67 public: |
68 explicit AddInObserve(ObserverListType* observer_list) | 68 explicit AddInObserve(ObserverListType* observer_list) |
69 : added(false), | 69 : added(false), |
70 observer_list(observer_list), | 70 observer_list(observer_list), |
71 adder(1) { | 71 adder(1) { |
72 } | 72 } |
73 | 73 |
74 virtual void Observe(int x) override { | 74 void Observe(int x) override { |
75 if (!added) { | 75 if (!added) { |
76 added = true; | 76 added = true; |
77 observer_list->AddObserver(&adder); | 77 observer_list->AddObserver(&adder); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 bool added; | 81 bool added; |
82 ObserverListType* observer_list; | 82 ObserverListType* observer_list; |
83 Adder adder; | 83 Adder adder; |
84 }; | 84 }; |
85 | 85 |
86 | 86 |
87 static const int kThreadRunTime = 2000; // ms to run the multi-threaded test. | 87 static const int kThreadRunTime = 2000; // ms to run the multi-threaded test. |
88 | 88 |
89 // A thread for use in the ThreadSafeObserver test | 89 // A thread for use in the ThreadSafeObserver test |
90 // which will add and remove itself from the notification | 90 // which will add and remove itself from the notification |
91 // list repeatedly. | 91 // list repeatedly. |
92 class AddRemoveThread : public PlatformThread::Delegate, | 92 class AddRemoveThread : public PlatformThread::Delegate, |
93 public Foo { | 93 public Foo { |
94 public: | 94 public: |
95 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify) | 95 AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify) |
96 : list_(list), | 96 : list_(list), |
97 loop_(NULL), | 97 loop_(nullptr), |
98 in_list_(false), | 98 in_list_(false), |
99 start_(Time::Now()), | 99 start_(Time::Now()), |
100 count_observes_(0), | 100 count_observes_(0), |
101 count_addtask_(0), | 101 count_addtask_(0), |
102 do_notifies_(notify), | 102 do_notifies_(notify), |
103 weak_factory_(this) { | 103 weak_factory_(this) { |
104 } | 104 } |
105 | 105 |
106 ~AddRemoveThread() override {} | 106 ~AddRemoveThread() override {} |
107 | 107 |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 ListDestructor a(observer_list); | 534 ListDestructor a(observer_list); |
535 observer_list->AddObserver(&a); | 535 observer_list->AddObserver(&a); |
536 | 536 |
537 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); | 537 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
538 // If this test fails, there'll be Valgrind errors when this function goes out | 538 // If this test fails, there'll be Valgrind errors when this function goes out |
539 // of scope. | 539 // of scope. |
540 } | 540 } |
541 | 541 |
542 } // namespace | 542 } // namespace |
543 } // namespace base | 543 } // namespace base |
OLD | NEW |