1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
namespace base {
namespace {
template <class T>
class OffThreadObjectCreator {
public:
static T* NewObject() {
T* result;
{
Thread creator_thread("creator_thread");
creator_thread.Start();
creator_thread.message_loop()->PostTask(
FROM_HERE,
base::Bind(OffThreadObjectCreator::CreateObject, &result));
}
DCHECK(result); // We synchronized on thread destruction above.
return result;
}
private:
static void CreateObject(T** result) {
*result = new T;
}
};
struct Base { std::string member; };
struct Derived : Base {};
struct Producer : SupportsWeakPtr<Producer> {};
struct Consumer { WeakPtr<Producer> producer; };
// Helper class to create and destroy weak pointer copies
// and delete objects on a background thread.
class BackgroundThread : public Thread {
public:
BackgroundThread() : Thread("owner_thread") {}
virtual ~BackgroundThread() {
Stop();
}
void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) {
WaitableEvent completion(true, false);
message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundThread::DoCreateFromProducer, consumer, producer,
&completion));
completion.Wait();
}
void CreateConsumerFromConsumer(Consumer** consumer, const Consumer* other) {
WaitableEvent completion(true, false);
message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundThread::DoCreateFromConsumer, consumer, other,
&completion));
completion.Wait();
}
void DeleteProducer(Producer* object) {
WaitableEvent completion(true, false);
message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundThread::DoDeleteProducer, object, &completion));
completion.Wait();
}
void DeleteConsumer(Consumer* object) {
WaitableEvent completion(true, false);
message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundThread::DoDeleteConsumer, object, &completion));
completion.Wait();
}
Producer* DeRef(const Consumer* consumer) {
WaitableEvent completion(true, false);
Producer* result = NULL;
message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundThread::DoDeRef, consumer, &result, &completion));
completion.Wait();
return result;
}
protected:
static void DoCreateFromConsumer(Consumer** consumer,
const Consumer* other,
WaitableEvent* completion) {
*consumer = new Consumer;
**consumer = *other;
completion->Signal();
}
static void DoCreateFromProducer(Consumer** consumer,
Producer* producer,
WaitableEvent* completion) {
*consumer = new Consumer;
(*consumer)->producer = producer->AsWeakPtr();
completion->Signal();
}
static void DoDeRef(const Consumer* consumer,
Producer** result,
WaitableEvent* completion) {
*result = consumer->producer.get();
completion->Signal();
}
static void DoDeleteProducer(Producer* object, WaitableEvent* completion) {
delete object;
completion->Signal();
}
static void DoDeleteConsumer(Consumer* object, WaitableEvent* completion) {
delete object;
completion->Signal();
}
};
} // namespace
TEST(WeakPtrTest, Basic) {
int data;
WeakPtrFactory<int> factory(&data);
WeakPtr<int> ptr = factory.GetWeakPtr();
EXPECT_EQ(&data, ptr.get());
}
TEST(WeakPtrTest, Comparison) {
int data;
WeakPtrFactory<int> factory(&data);
WeakPtr<int> ptr = factory.GetWeakPtr();
WeakPtr<int> ptr2 = ptr;
EXPECT_TRUE(ptr == ptr2);
}
TEST(WeakPtrTest, OutOfScope) {
WeakPtr<int> ptr;
EXPECT_TRUE(ptr.get() == NULL);
{
int data;
WeakPtrFactory<int> factory(&data);
ptr = factory.GetWeakPtr();
}
EXPECT_TRUE(ptr.get() == NULL);
}
TEST(WeakPtrTest, Multiple) {
WeakPtr<int> a, b;
{
int data;
WeakPtrFactory<int> factory(&data);
a = factory.GetWeakPtr();
b = factory.GetWeakPtr();
EXPECT_EQ(&data, a.get());
EXPECT_EQ(&data, b.get());
}
EXPECT_TRUE(a.get() == NULL);
EXPECT_TRUE(b.get() == NULL);
}
TEST(WeakPtrTest, MultipleStaged) {
WeakPtr<int> a;
{
int data;
WeakPtrFactory<int> factory(&data);
a = factory.GetWeakPtr();
{
WeakPtr<int> b = factory.GetWeakPtr();
}
EXPECT_TRUE(a.get() != NULL);
}
EXPECT_TRUE(a.get() == NULL);
}
TEST(WeakPtrTest, UpCast) {
Derived data;
WeakPtrFactory<Derived> factory(&data);
WeakPtr<Base> ptr = factory.GetWeakPtr();
ptr = factory.GetWeakPtr();
EXPECT_EQ(ptr.get(), &data);
}
TEST(WeakPtrTest, SupportsWeakPtr) {
Producer f;
WeakPtr<Producer> ptr = f.AsWeakPtr();
EXPECT_EQ(&f, ptr.get());
}
TEST(WeakPtrTest, InvalidateWeakPtrs) {
int data;
WeakPtrFactory<int> factory(&data);
WeakPtr<int> ptr = factory.GetWeakPtr();
EXPECT_EQ(&data, ptr.get());
EXPECT_TRUE(factory.HasWeakPtrs());
factory.InvalidateWeakPtrs();
EXPECT_TRUE(ptr.get() == NULL);
EXPECT_FALSE(factory.HasWeakPtrs());
}
TEST(WeakPtrTest, HasWeakPtrs) {
int data;
WeakPtrFactory<int> factory(&data);
{
WeakPtr<int> ptr = factory.GetWeakPtr();
EXPECT_TRUE(factory.HasWeakPtrs());
}
EXPECT_FALSE(factory.HasWeakPtrs());
}
TEST(WeakPtrTest, SingleThreaded1) {
// Test that it is OK to create a class that supports weak references on one
// thread, but use it on another. This tests that we do not trip runtime
// checks that ensure that a weak reference is not used by multiple threads.
scoped_ptr<Producer> producer(OffThreadObjectCreator<Producer>::NewObject());
WeakPtr<Producer> weak_producer = producer->AsWeakPtr();
EXPECT_EQ(producer.get(), weak_producer.get());
}
TEST(WeakPtrTest, SingleThreaded2) {
// Test that it is OK to create a class that has a WeakPtr member on one
// thread, but use it on another. This tests that we do not trip runtime
// checks that ensure that a weak reference is not used by multiple threads.
scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject());
Producer producer;
consumer->producer = producer.AsWeakPtr();
EXPECT_EQ(&producer, consumer->producer.get());
}
TEST(WeakPtrTest, MoveOwnershipImplicit) {
// Move object ownership to other thread by releasing all weak pointers
// on the original thread first. Establishing weak pointers on a different
// thread after previous pointers have been destroyed implicitly reattaches
// the thread checks.
// - Thread A creates object and weak pointer
// - Thread A deletes the weak pointer
// - Thread B creates weak pointer
// - Thread B derefs weak pointer
// - Thread B deletes object
BackgroundThread thread;
thread.Start();
Producer* producer = new Producer();
{
WeakPtr<Producer> weak_ptr = producer->AsWeakPtr();
}
Consumer* consumer;
thread.CreateConsumerFromProducer(&consumer, producer);
EXPECT_EQ(thread.DeRef(consumer), producer);
thread.DeleteProducer(producer);
thread.DeleteConsumer(consumer);
}
TEST(WeakPtrTest, MoveOwnershipExplicit) {
// Test that we do not trip any checks if we establish weak references
// on one thread and delete the object on another thread after explicit
// detachment.
// - Thread A creates object
// - Thread B creates weak pointer
// - Thread B releases weak pointer
// - Detach owner from Thread B
// - Thread A destroys object
BackgroundThread thread;
thread.Start();
Producer producer;
Consumer* consumer;
thread.CreateConsumerFromProducer(&consumer, &producer);
EXPECT_EQ(thread.DeRef(consumer), &producer);
thread.DeleteConsumer(consumer);
producer.DetachFromThread();
}
TEST(WeakPtrTest, ThreadARefOutlivesThreadBRef) {
// Originating thread has a WeakPtr that outlives others.
// - Thread A creates WeakPtr<> and passes copy to Thread B
// - Destruct the pointer on Thread B
// - Destruct the pointer on Thread A
BackgroundThread thread;
thread.Start();
Producer producer;
Consumer consumer;
consumer.producer = producer.AsWeakPtr();
Consumer* consumer_copy;
thread.CreateConsumerFromConsumer(&consumer_copy, &consumer);
EXPECT_EQ(consumer_copy->producer, &producer);
thread.DeleteConsumer(consumer_copy);
}
TEST(WeakPtrTest, ThreadBRefOutlivesThreadARef) {
// Originating thread drops all references before another thread.
// - Thread A creates WeakPtr<> and passes copy to Thread B
// - Destruct the pointer on Thread A
// - Destruct the pointer on Thread B
BackgroundThread thread;
thread.Start();
Producer producer;
Consumer* consumer_copy;
{
Consumer consumer;
consumer.producer = producer.AsWeakPtr();
thread.CreateConsumerFromConsumer(&consumer_copy, &consumer);
}
EXPECT_EQ(consumer_copy->producer, &producer);
thread.DeleteConsumer(consumer_copy);
}
TEST(WeakPtrTest, OwnerThreadDeletesObject) {
// Originating thread invalidates WeakPtrs while its held by other thread.
// - Thread A creates WeakPtr<> and passes Copy to Thread B
// - WeakReferenceOwner gets destroyed on Thread A
// - WeakPtr gets destroyed on Thread B
BackgroundThread thread;
thread.Start();
Consumer* consumer_copy;
{
Producer producer;
Consumer consumer;
consumer.producer = producer.AsWeakPtr();
thread.CreateConsumerFromConsumer(&consumer_copy, &consumer);
}
EXPECT_TRUE(consumer_copy->producer == NULL);
thread.DeleteConsumer(consumer_copy);
}
TEST(WeakPtrTest, Dereference) {
Base data;
data.member = "123456";
WeakPtrFactory<Base> factory(&data);
WeakPtr<Base> ptr = factory.GetWeakPtr();
EXPECT_EQ(&data, ptr.get());
EXPECT_EQ(data.member, (*ptr).member);
EXPECT_EQ(data.member, ptr->member);
}
} // namespace base
|