fischman@chromium.org | 998561e | 2012-01-24 07:56:41 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 7 | #include "base/at_exit.h" |
| 8 | #include "base/atomic_sequence_num.h" |
| 9 | #include "base/lazy_instance.h" |
brettw@chromium.org | 34f964f | 2010-12-31 03:08:36 +0900 | [diff] [blame] | 10 | #include "base/threading/simple_thread.h" |
brettw | dd9392c | 2017-06-14 06:58:40 +0900 | [diff] [blame] | 11 | #include "build/build_config.h" |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
pliard@chromium.org | ac1f41d | 2012-03-13 21:07:19 +0900 | [diff] [blame] | 16 | base::StaticAtomicSequenceNumber constructed_seq_; |
| 17 | base::StaticAtomicSequenceNumber destructed_seq_; |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 18 | |
| 19 | class ConstructAndDestructLogger { |
| 20 | public: |
| 21 | ConstructAndDestructLogger() { |
| 22 | constructed_seq_.GetNext(); |
| 23 | } |
| 24 | ~ConstructAndDestructLogger() { |
| 25 | destructed_seq_.GetNext(); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | class SlowConstructor { |
| 30 | public: |
| 31 | SlowConstructor() : some_int_(0) { |
brettw@chromium.org | 6139182 | 2011-01-01 05:02:16 +0900 | [diff] [blame] | 32 | // Sleep for 1 second to try to cause a race. |
tedvessenes@gmail.com | aaa6303 | 2012-01-01 07:53:51 +0900 | [diff] [blame] | 33 | base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 34 | ++constructed; |
| 35 | some_int_ = 12; |
| 36 | } |
| 37 | int some_int() const { return some_int_; } |
| 38 | |
| 39 | static int constructed; |
| 40 | private: |
| 41 | int some_int_; |
| 42 | }; |
| 43 | |
| 44 | int SlowConstructor::constructed = 0; |
| 45 | |
| 46 | class SlowDelegate : public base::DelegateSimpleThread::Delegate { |
| 47 | public: |
scottmg | 7a1ef76 | 2017-03-08 17:48:46 +0900 | [diff] [blame] | 48 | explicit SlowDelegate( |
| 49 | base::LazyInstance<SlowConstructor>::DestructorAtExit* lazy) |
erg@google.com | bf6ce9f | 2010-01-27 08:08:02 +0900 | [diff] [blame] | 50 | : lazy_(lazy) {} |
| 51 | |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 52 | void Run() override { |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 53 | EXPECT_EQ(12, lazy_->Get().some_int()); |
| 54 | EXPECT_EQ(12, lazy_->Pointer()->some_int()); |
| 55 | } |
| 56 | |
| 57 | private: |
scottmg | 7a1ef76 | 2017-03-08 17:48:46 +0900 | [diff] [blame] | 58 | base::LazyInstance<SlowConstructor>::DestructorAtExit* lazy_; |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace |
| 62 | |
scottmg | 7a1ef76 | 2017-03-08 17:48:46 +0900 | [diff] [blame] | 63 | static base::LazyInstance<ConstructAndDestructLogger>::DestructorAtExit |
| 64 | lazy_logger = LAZY_INSTANCE_INITIALIZER; |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 65 | |
| 66 | TEST(LazyInstanceTest, Basic) { |
| 67 | { |
phajdan.jr@chromium.org | e7f133a | 2009-11-19 18:11:39 +0900 | [diff] [blame] | 68 | base::ShadowingAtExitManager shadow; |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 69 | |
| 70 | EXPECT_EQ(0, constructed_seq_.GetNext()); |
| 71 | EXPECT_EQ(0, destructed_seq_.GetNext()); |
| 72 | |
| 73 | lazy_logger.Get(); |
| 74 | EXPECT_EQ(2, constructed_seq_.GetNext()); |
| 75 | EXPECT_EQ(1, destructed_seq_.GetNext()); |
| 76 | |
| 77 | lazy_logger.Pointer(); |
| 78 | EXPECT_EQ(3, constructed_seq_.GetNext()); |
| 79 | EXPECT_EQ(2, destructed_seq_.GetNext()); |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 80 | } |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 81 | EXPECT_EQ(4, constructed_seq_.GetNext()); |
| 82 | EXPECT_EQ(4, destructed_seq_.GetNext()); |
| 83 | } |
| 84 | |
scottmg | 7a1ef76 | 2017-03-08 17:48:46 +0900 | [diff] [blame] | 85 | static base::LazyInstance<SlowConstructor>::DestructorAtExit lazy_slow = |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 86 | LAZY_INSTANCE_INITIALIZER; |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 87 | |
| 88 | TEST(LazyInstanceTest, ConstructorThreadSafety) { |
| 89 | { |
phajdan.jr@chromium.org | e7f133a | 2009-11-19 18:11:39 +0900 | [diff] [blame] | 90 | base::ShadowingAtExitManager shadow; |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 91 | |
| 92 | SlowDelegate delegate(&lazy_slow); |
| 93 | EXPECT_EQ(0, SlowConstructor::constructed); |
| 94 | |
| 95 | base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); |
| 96 | pool.AddWork(&delegate, 20); |
| 97 | EXPECT_EQ(0, SlowConstructor::constructed); |
| 98 | |
| 99 | pool.Start(); |
| 100 | pool.JoinAll(); |
| 101 | EXPECT_EQ(1, SlowConstructor::constructed); |
| 102 | } |
| 103 | } |
evan@chromium.org | a04f70c | 2010-10-22 05:41:47 +0900 | [diff] [blame] | 104 | |
| 105 | namespace { |
| 106 | |
| 107 | // DeleteLogger is an object which sets a flag when it's destroyed. |
| 108 | // It accepts a bool* and sets the bool to true when the dtor runs. |
| 109 | class DeleteLogger { |
| 110 | public: |
| 111 | DeleteLogger() : deleted_(NULL) {} |
| 112 | ~DeleteLogger() { *deleted_ = true; } |
| 113 | |
| 114 | void SetDeletedPtr(bool* deleted) { |
| 115 | deleted_ = deleted; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | bool* deleted_; |
| 120 | }; |
| 121 | |
| 122 | } // anonymous namespace |
| 123 | |
| 124 | TEST(LazyInstanceTest, LeakyLazyInstance) { |
| 125 | // Check that using a plain LazyInstance causes the dtor to run |
| 126 | // when the AtExitManager finishes. |
| 127 | bool deleted1 = false; |
| 128 | { |
| 129 | base::ShadowingAtExitManager shadow; |
scottmg | 7a1ef76 | 2017-03-08 17:48:46 +0900 | [diff] [blame] | 130 | static base::LazyInstance<DeleteLogger>::DestructorAtExit test = |
| 131 | LAZY_INSTANCE_INITIALIZER; |
evan@chromium.org | a04f70c | 2010-10-22 05:41:47 +0900 | [diff] [blame] | 132 | test.Get().SetDeletedPtr(&deleted1); |
| 133 | } |
| 134 | EXPECT_TRUE(deleted1); |
| 135 | |
| 136 | // Check that using a *leaky* LazyInstance makes the dtor not run |
| 137 | // when the AtExitManager finishes. |
| 138 | bool deleted2 = false; |
| 139 | { |
| 140 | base::ShadowingAtExitManager shadow; |
fischman@chromium.org | 998561e | 2012-01-24 07:56:41 +0900 | [diff] [blame] | 141 | static base::LazyInstance<DeleteLogger>::Leaky |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 142 | test = LAZY_INSTANCE_INITIALIZER; |
evan@chromium.org | a04f70c | 2010-10-22 05:41:47 +0900 | [diff] [blame] | 143 | test.Get().SetDeletedPtr(&deleted2); |
| 144 | } |
| 145 | EXPECT_FALSE(deleted2); |
| 146 | } |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 147 | |
| 148 | namespace { |
| 149 | |
| 150 | template <size_t alignment> |
| 151 | class AlignedData { |
| 152 | public: |
| 153 | AlignedData() {} |
| 154 | ~AlignedData() {} |
brettw | dd9392c | 2017-06-14 06:58:40 +0900 | [diff] [blame] | 155 | alignas(alignment) char data_[alignment]; |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 156 | }; |
| 157 | |
brettw | dd9392c | 2017-06-14 06:58:40 +0900 | [diff] [blame] | 158 | } // namespace |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 159 | |
| 160 | #define EXPECT_ALIGNED(ptr, align) \ |
| 161 | EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 162 | |
| 163 | TEST(LazyInstanceTest, Alignment) { |
| 164 | using base::LazyInstance; |
| 165 | |
| 166 | // Create some static instances with increasing sizes and alignment |
| 167 | // requirements. By ordering this way, the linker will need to do some work to |
| 168 | // ensure proper alignment of the static data. |
scottmg | 7a1ef76 | 2017-03-08 17:48:46 +0900 | [diff] [blame] | 169 | static LazyInstance<AlignedData<4>>::DestructorAtExit align4 = |
| 170 | LAZY_INSTANCE_INITIALIZER; |
| 171 | static LazyInstance<AlignedData<32>>::DestructorAtExit align32 = |
| 172 | LAZY_INSTANCE_INITIALIZER; |
| 173 | static LazyInstance<AlignedData<4096>>::DestructorAtExit align4096 = |
| 174 | LAZY_INSTANCE_INITIALIZER; |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 175 | |
| 176 | EXPECT_ALIGNED(align4.Pointer(), 4); |
| 177 | EXPECT_ALIGNED(align32.Pointer(), 32); |
| 178 | EXPECT_ALIGNED(align4096.Pointer(), 4096); |
| 179 | } |