| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 #include "base/atomic_sequence_num.h" | 6 #include "base/atomic_sequence_num.h" |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/simple_thread.h" | 8 #include "base/simple_thread.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 class ShadowingAtExitManager : public base::AtExitManager { | |
| 14 public: | |
| 15 ShadowingAtExitManager() : AtExitManager(true) { } | |
| 16 }; | |
| 17 | |
| 18 base::AtomicSequenceNumber constructed_seq_(base::LINKER_INITIALIZED); | 13 base::AtomicSequenceNumber constructed_seq_(base::LINKER_INITIALIZED); |
| 19 base::AtomicSequenceNumber destructed_seq_(base::LINKER_INITIALIZED); | 14 base::AtomicSequenceNumber destructed_seq_(base::LINKER_INITIALIZED); |
| 20 | 15 |
| 21 class ConstructAndDestructLogger { | 16 class ConstructAndDestructLogger { |
| 22 public: | 17 public: |
| 23 ConstructAndDestructLogger() { | 18 ConstructAndDestructLogger() { |
| 24 constructed_seq_.GetNext(); | 19 constructed_seq_.GetNext(); |
| 25 } | 20 } |
| 26 ~ConstructAndDestructLogger() { | 21 ~ConstructAndDestructLogger() { |
| 27 destructed_seq_.GetNext(); | 22 destructed_seq_.GetNext(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 56 base::LazyInstance<SlowConstructor>* lazy_; | 51 base::LazyInstance<SlowConstructor>* lazy_; |
| 57 }; | 52 }; |
| 58 | 53 |
| 59 } // namespace | 54 } // namespace |
| 60 | 55 |
| 61 static base::LazyInstance<ConstructAndDestructLogger> lazy_logger( | 56 static base::LazyInstance<ConstructAndDestructLogger> lazy_logger( |
| 62 base::LINKER_INITIALIZED); | 57 base::LINKER_INITIALIZED); |
| 63 | 58 |
| 64 TEST(LazyInstanceTest, Basic) { | 59 TEST(LazyInstanceTest, Basic) { |
| 65 { | 60 { |
| 66 ShadowingAtExitManager shadow; | 61 base::ShadowingAtExitManager shadow; |
| 67 | 62 |
| 68 EXPECT_EQ(0, constructed_seq_.GetNext()); | 63 EXPECT_EQ(0, constructed_seq_.GetNext()); |
| 69 EXPECT_EQ(0, destructed_seq_.GetNext()); | 64 EXPECT_EQ(0, destructed_seq_.GetNext()); |
| 70 | 65 |
| 71 lazy_logger.Get(); | 66 lazy_logger.Get(); |
| 72 EXPECT_EQ(2, constructed_seq_.GetNext()); | 67 EXPECT_EQ(2, constructed_seq_.GetNext()); |
| 73 EXPECT_EQ(1, destructed_seq_.GetNext()); | 68 EXPECT_EQ(1, destructed_seq_.GetNext()); |
| 74 | 69 |
| 75 lazy_logger.Pointer(); | 70 lazy_logger.Pointer(); |
| 76 EXPECT_EQ(3, constructed_seq_.GetNext()); | 71 EXPECT_EQ(3, constructed_seq_.GetNext()); |
| 77 EXPECT_EQ(2, destructed_seq_.GetNext()); | 72 EXPECT_EQ(2, destructed_seq_.GetNext()); |
| 78 } | 73 } |
| 79 EXPECT_EQ(4, constructed_seq_.GetNext()); | 74 EXPECT_EQ(4, constructed_seq_.GetNext()); |
| 80 EXPECT_EQ(4, destructed_seq_.GetNext()); | 75 EXPECT_EQ(4, destructed_seq_.GetNext()); |
| 81 } | 76 } |
| 82 | 77 |
| 83 static base::LazyInstance<SlowConstructor> lazy_slow(base::LINKER_INITIALIZED); | 78 static base::LazyInstance<SlowConstructor> lazy_slow(base::LINKER_INITIALIZED); |
| 84 | 79 |
| 85 TEST(LazyInstanceTest, ConstructorThreadSafety) { | 80 TEST(LazyInstanceTest, ConstructorThreadSafety) { |
| 86 { | 81 { |
| 87 ShadowingAtExitManager shadow; | 82 base::ShadowingAtExitManager shadow; |
| 88 | 83 |
| 89 SlowDelegate delegate(&lazy_slow); | 84 SlowDelegate delegate(&lazy_slow); |
| 90 EXPECT_EQ(0, SlowConstructor::constructed); | 85 EXPECT_EQ(0, SlowConstructor::constructed); |
| 91 | 86 |
| 92 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); | 87 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); |
| 93 pool.AddWork(&delegate, 20); | 88 pool.AddWork(&delegate, 20); |
| 94 EXPECT_EQ(0, SlowConstructor::constructed); | 89 EXPECT_EQ(0, SlowConstructor::constructed); |
| 95 | 90 |
| 96 pool.Start(); | 91 pool.Start(); |
| 97 pool.JoinAll(); | 92 pool.JoinAll(); |
| 98 EXPECT_EQ(1, SlowConstructor::constructed); | 93 EXPECT_EQ(1, SlowConstructor::constructed); |
| 99 } | 94 } |
| 100 } | 95 } |
| OLD | NEW |