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
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef BASE_SINGLETON_H__
#define BASE_SINGLETON_H__
#include <stdlib.h>
#include <utility>
#include "base/lock.h"
#include "base/singleton_internal.h"
#ifdef WIN32
#include "base/fix_wp64.h"
#else // WIN32
#include <pthread.h>
#endif // WIN32
// Default traits for Singleton<Type>. Calls operator new and operator delete on
// the object. Registers automatic deletion at library unload or process exit.
// Overload if you need arguments or another memory allocation function.
template<typename Type>
struct DefaultSingletonTraits {
// Allocates the object.
static Type* New() {
// The parenthesis is very important here; it forces POD type
// initialization.
return new Type();
}
// Destroys the object.
static void Delete(Type* x) {
delete x;
}
// Set to true to automatically register deletion of the object on library
// unload or process exit.
static const bool kRegisterAtExit = true;
// Note: Only apply on Windows. Has *no effect* on other platform.
// When set to true, it signals that Trait::New() *must* not be called
// multiple times at construction. Anything that must be done to not enter
// this situation should be done at all cost. This simply involves creating a
// temporary lock.
static const bool kMustCallNewExactlyOnce = false;
};
// The Singleton<Type, Traits, DifferentiatingType> class manages a single
// instance of Type which will be created on first use and will be destroyed at
// library unload (or on normal process exit). The Trait::Delete function will
// not be called on abnormal process exit.
//
// DifferentiatingType is used as a key to differentiate two different
// singletons having the same memory allocation functions but serving a
// different purpose. This is mainly used for Locks serving different purposes.
//
// Example usages: (none are preferred, they all result in the same code)
// 1. FooClass* ptr = Singleton<FooClass>::get();
// ptr->Bar();
// 2. Singleton<FooClass>()->Bar();
// 3. Singleton<FooClass>::get()->Bar();
//
// Singleton<> has no non-static members and doesn't need to actually be
// instantiated. It does no harm to instantiate it and use it as a class member
// or at global level since it is acting as a POD type.
//
// This class is itself thread-safe. The underlying Type must of course be
// thread-safe if you want to use it concurrently. Two parameters may be tuned
// depending on the user's requirements.
//
// Glossary:
// MCNEO = kMustCallNewExactlyOnce
// RAE = kRegisterAtExit
//
// On every platform, if Traits::RAE is true, the singleton will be destroyed at
// library unload or process exit. if Traits::RAE is false, the singleton will
// not be freed at library unload or process exit, thus the singleton will be
// leaked if it is ever accessed. Traits::RAE shouldn't be false unless
// absolutely necessary. Remember that the heap where the object is allocated
// may be destroyed by the CRT anyway.
//
// On Windows, now the fun begins. Traits::New() may be called more than once
// concurrently, but no user will gain access to the object until the winning
// Traits::New() call is completed.
//
// On Windows, if Traits::MCNEO and Traits::RAE are both false,
// Traits::Delete() can still be called. The reason is that a race condition can
// occur during the object creation which will cause Traits::Delete() to be
// called even if Traits::RAE is false, so Traits::Delete() should still be
// implemented or objects may be leaked when there is a race condition in
// creating the singleton. Even though this case is very rare, it may happen in
// practice. To work around this situation, before creating a multithreaded
// environment, be sure to call Singleton<>::get() to force the creation of the
// instance.
//
// On Windows, If Traits::MCNEO is true, a temporary lock per singleton will be
// created to ensure that Trait::New() is only called once.
//
// If you want to ensure that your class can only exist as a singleton, make
// its constructors private, and make DefaultSingletonTraits<> a friend:
//
// #include "base/singleton.h"
// class FooClass {
// public:
// void Bar() { ... }
// private:
// FooClass() { ... }
// friend DefaultSingletonTraits<FooClass>;
//
// DISALLOW_EVIL_CONSTRUCTORS(FooClass);
// };
//
// Caveats:
// (a) Every call to get(), operator->() and operator*() incurs some overhead
// (16ns on my P4/2.8GHz) to check whether the object has already been
// initialized. You may wish to cache the result of get(); it will not
// change.
//
// (b) Your factory function must never throw an exception. This class is not
// exception-safe.
//
// (c) On Windows at least, if Traits::kMustCallNewExactlyOnce is false,
// Traits::New() may be called two times in two different threads at the
// same time so it must not have side effects. Set
// Traits::kMustCallNewExactlyOnce to true to alleviate this issue, at
// the cost of a slight increase of memory use and creation time.
//
template <typename Type,
typename Traits = DefaultSingletonTraits<Type>,
typename DifferentiatingType = Type>
class Singleton
: public SingletonStorage<
Type,
std::pair<Traits, DifferentiatingType>,
UseVolatileSingleton<Traits::kMustCallNewExactlyOnce>::value> {
public:
// This class is safe to be constructed and copy-constructed since it has no
// member.
// Return a pointer to the one true instance of the class.
static Type* get() {
Type* value = instance_;
// Acute readers may think: why not just discard "value" and use
// "instance_" directly? Astute readers will remark that instance_ can be a
// volatile pointer on Windows and hence the compiler would be forced to
// generate two memory reads instead of just one. Since this is the hotspot,
// this is inefficient.
if (value)
return value;
#ifdef WIN32
// Statically determine which function to call.
LockedConstruct<Traits::kMustCallNewExactlyOnce>();
#else // WIN32
// Posix platforms already have the functionality embedded.
pthread_once(&control_, SafeConstruct);
#endif // WIN32
return instance_;
}
// Shortcuts.
Type& operator*() {
return *get();
}
Type* operator->() {
return get();
}
private:
#ifdef WIN32
// Use bool template differentiation to make sure to not build the other part
// of the code. We don't want to instantiate Singleton<Lock, ...> uselessly.
template<bool kUseLock>
static void LockedConstruct() {
// Define a differentiating type for the Lock.
typedef std::pair<Type, std::pair<Traits, DifferentiatingType> >
LockDifferentiatingType;
// Object-type lock. Note that the lock singleton is different per singleton
// type.
AutoLock lock(*Singleton<Lock,
DefaultSingletonTraits<Lock>,
LockDifferentiatingType>());
// Now that we have the lock, look if the instance is created, if not yet,
// create it.
if (!instance_)
SafeConstruct();
}
template<>
static void LockedConstruct<false>() {
// Implemented using atomic compare-and-swap. The new object is
// constructed and used as the new value in the operation; if the
// compare fails, the new object will be deleted. Future implementations
// for Windows might use InitOnceExecuteOnce (Vista-only), similar in
// spirit to pthread_once.
// On Windows, multiple concurrent Traits::New() calls are tolerated.
Type* value = Traits::New();
if (InterlockedCompareExchangePointer(
reinterpret_cast<void* volatile*>(&instance_), value, NULL)) {
// Race condition, discard the temporary value.
Traits::Delete(value);
} else {
// Got it, register destruction at unload. atexit() is called on library
// unload. It is assumed that atexit() is itself thread safe. It is also
// assumed that registered functions by atexit are called in a thread
// safe manner. At least on Windows, they are called with the loader
// lock held. On Windows, the CRT use a structure similar to
// std::map<dll_handle,std::vector<registered_functions>> so the right
// functions are called on library unload, independent of having a DLL
// CRT or a static CRT or even both.
if (Traits::kRegisterAtExit)
atexit(&OnExit);
}
}
#endif // WIN32
// SafeConstruct is guaranteed to be executed only once.
static void SafeConstruct() {
instance_ = Traits::New();
// Porting note: this code depends on some properties of atexit which are
// not guaranteed by the standard:
// - atexit must be thread-safe: its internal manipulation of the list of
// registered functions must be tolerant of multiple threads attempting
// to register exit routines simultaneously.
// - exit routines must run when the executable module that contains them
// is unloaded. For routines in by dynamically-loaded modules, this
// may be sooner than process termination.
// - atexit should support an arbitrary number of registered exit
// routines, or at least should support more routines than will
// actually be registered (the standard only requires 32).
// The atexit implementations in contemporary versions of Mac OS X, glibc,
// and the Windows C runtime provide these capabilities. To port to other
// systems with less-advanced (even though still standard-conforming)
// atexit implmentations, consider alternatives such as __cxa_atexit or
// custom termination sections.
if (Traits::kRegisterAtExit)
atexit(OnExit);
}
// Adapter function for use with atexit().
static void OnExit() {
if (!instance_)
return;
Traits::Delete(instance_);
instance_ = NULL;
}
#ifndef WIN32
static pthread_once_t control_;
#endif // !WIN32
};
#ifndef WIN32
template <typename Type, typename Traits, typename DifferentiatingType>
pthread_once_t Singleton<Type, Traits, DifferentiatingType>::control_ =
PTHREAD_ONCE_INIT;
#endif // !WIN32
#endif // BASE_SINGLETON_H__
|