blob: e25366e228f5948e39bea955204cada3eb4775f9 [file] [log] [blame]
[email protected]9fc44162012-01-23 22:56:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]30039e62008-09-08 14:11:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/at_exit.h"
6#include "base/atomic_sequence_num.h"
7#include "base/lazy_instance.h"
[email protected]cd924d62012-02-23 17:52:208#include "base/memory/aligned_memory.h"
[email protected]ac9ba8fe2010-12-30 18:08:369#include "base/threading/simple_thread.h"
[email protected]30039e62008-09-08 14:11:1310#include "testing/gtest/include/gtest/gtest.h"
11
12namespace {
13
[email protected]8a8443f2012-03-13 12:07:1914base::StaticAtomicSequenceNumber constructed_seq_;
15base::StaticAtomicSequenceNumber destructed_seq_;
[email protected]30039e62008-09-08 14:11:1316
17class ConstructAndDestructLogger {
18 public:
19 ConstructAndDestructLogger() {
20 constructed_seq_.GetNext();
21 }
22 ~ConstructAndDestructLogger() {
23 destructed_seq_.GetNext();
24 }
25};
26
27class SlowConstructor {
28 public:
29 SlowConstructor() : some_int_(0) {
[email protected]ce072a72010-12-31 20:02:1630 // Sleep for 1 second to try to cause a race.
[email protected]a1b75b942011-12-31 22:53:5131 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
[email protected]30039e62008-09-08 14:11:1332 ++constructed;
33 some_int_ = 12;
34 }
35 int some_int() const { return some_int_; }
36
37 static int constructed;
38 private:
39 int some_int_;
40};
41
42int SlowConstructor::constructed = 0;
43
44class SlowDelegate : public base::DelegateSimpleThread::Delegate {
45 public:
[email protected]2fdc86a2010-01-26 23:08:0246 explicit SlowDelegate(base::LazyInstance<SlowConstructor>* lazy)
47 : lazy_(lazy) {}
48
[email protected]44106182012-04-06 03:53:0249 virtual void Run() OVERRIDE {
[email protected]30039e62008-09-08 14:11:1350 EXPECT_EQ(12, lazy_->Get().some_int());
51 EXPECT_EQ(12, lazy_->Pointer()->some_int());
52 }
53
54 private:
55 base::LazyInstance<SlowConstructor>* lazy_;
56};
57
58} // namespace
59
[email protected]6de0fd1d2011-11-15 13:31:4960static base::LazyInstance<ConstructAndDestructLogger> lazy_logger =
61 LAZY_INSTANCE_INITIALIZER;
[email protected]30039e62008-09-08 14:11:1362
63TEST(LazyInstanceTest, Basic) {
64 {
[email protected]4ea927b2009-11-19 09:11:3965 base::ShadowingAtExitManager shadow;
[email protected]30039e62008-09-08 14:11:1366
67 EXPECT_EQ(0, constructed_seq_.GetNext());
68 EXPECT_EQ(0, destructed_seq_.GetNext());
69
70 lazy_logger.Get();
71 EXPECT_EQ(2, constructed_seq_.GetNext());
72 EXPECT_EQ(1, destructed_seq_.GetNext());
73
74 lazy_logger.Pointer();
75 EXPECT_EQ(3, constructed_seq_.GetNext());
76 EXPECT_EQ(2, destructed_seq_.GetNext());
[email protected]52a261f2009-03-03 15:01:1277 }
[email protected]30039e62008-09-08 14:11:1378 EXPECT_EQ(4, constructed_seq_.GetNext());
79 EXPECT_EQ(4, destructed_seq_.GetNext());
80}
81
[email protected]6de0fd1d2011-11-15 13:31:4982static base::LazyInstance<SlowConstructor> lazy_slow =
83 LAZY_INSTANCE_INITIALIZER;
[email protected]30039e62008-09-08 14:11:1384
85TEST(LazyInstanceTest, ConstructorThreadSafety) {
86 {
[email protected]4ea927b2009-11-19 09:11:3987 base::ShadowingAtExitManager shadow;
[email protected]30039e62008-09-08 14:11:1388
89 SlowDelegate delegate(&lazy_slow);
90 EXPECT_EQ(0, SlowConstructor::constructed);
91
92 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5);
93 pool.AddWork(&delegate, 20);
94 EXPECT_EQ(0, SlowConstructor::constructed);
95
96 pool.Start();
97 pool.JoinAll();
98 EXPECT_EQ(1, SlowConstructor::constructed);
99 }
100}
[email protected]dcc69332010-10-21 20:41:47101
102namespace {
103
104// DeleteLogger is an object which sets a flag when it's destroyed.
105// It accepts a bool* and sets the bool to true when the dtor runs.
106class DeleteLogger {
107 public:
108 DeleteLogger() : deleted_(NULL) {}
109 ~DeleteLogger() { *deleted_ = true; }
110
111 void SetDeletedPtr(bool* deleted) {
112 deleted_ = deleted;
113 }
114
115 private:
116 bool* deleted_;
117};
118
119} // anonymous namespace
120
121TEST(LazyInstanceTest, LeakyLazyInstance) {
122 // Check that using a plain LazyInstance causes the dtor to run
123 // when the AtExitManager finishes.
124 bool deleted1 = false;
125 {
126 base::ShadowingAtExitManager shadow;
[email protected]6de0fd1d2011-11-15 13:31:49127 static base::LazyInstance<DeleteLogger> test = LAZY_INSTANCE_INITIALIZER;
[email protected]dcc69332010-10-21 20:41:47128 test.Get().SetDeletedPtr(&deleted1);
129 }
130 EXPECT_TRUE(deleted1);
131
132 // Check that using a *leaky* LazyInstance makes the dtor not run
133 // when the AtExitManager finishes.
134 bool deleted2 = false;
135 {
136 base::ShadowingAtExitManager shadow;
[email protected]9fc44162012-01-23 22:56:41137 static base::LazyInstance<DeleteLogger>::Leaky
[email protected]6de0fd1d2011-11-15 13:31:49138 test = LAZY_INSTANCE_INITIALIZER;
[email protected]dcc69332010-10-21 20:41:47139 test.Get().SetDeletedPtr(&deleted2);
140 }
141 EXPECT_FALSE(deleted2);
142}
[email protected]cd924d62012-02-23 17:52:20143
144namespace {
145
146template <size_t alignment>
147class AlignedData {
148 public:
149 AlignedData() {}
150 ~AlignedData() {}
151 base::AlignedMemory<alignment, alignment> data_;
152};
153
154} // anonymous namespace
155
156#define EXPECT_ALIGNED(ptr, align) \
157 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
158
159TEST(LazyInstanceTest, Alignment) {
160 using base::LazyInstance;
161
162 // Create some static instances with increasing sizes and alignment
163 // requirements. By ordering this way, the linker will need to do some work to
164 // ensure proper alignment of the static data.
165 static LazyInstance<AlignedData<4> > align4 = LAZY_INSTANCE_INITIALIZER;
166 static LazyInstance<AlignedData<32> > align32 = LAZY_INSTANCE_INITIALIZER;
167 static LazyInstance<AlignedData<4096> > align4096 = LAZY_INSTANCE_INITIALIZER;
168
169 EXPECT_ALIGNED(align4.Pointer(), 4);
170 EXPECT_ALIGNED(align32.Pointer(), 32);
171 EXPECT_ALIGNED(align4096.Pointer(), 4096);
172}