-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathsingleton.h
164 lines (147 loc) · 5.85 KB
/
singleton.h
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
// Copyright 2003 Google Inc.
//
// The Singleton<Type> class manages a single instance of Type which will be
// created on first use and (usually) never destroyed.
//
// MyClass* ptr = Singleton<MyClass>::get()
// ptr->DoSomething();
//
// Singleton<> has no non-static members and is never actually instantiated.
//
// WARNING: Read go/singletons before using.
//
// This class is thread safe; the constructor will be run at most once, and
// no user will gain access to the object until the constructor is completed.
// The underlying Type must of course be thread-safe if you want to use it
// concurrently.
//
// If you want to ensure that your class can only exist as a singleton, make
// its constructors private, and make Singleton<> a friend:
//
// class MySingletonOnlyClass {
// public:
// void DoSomething() { ... }
// private:
// DISALLOW_COPY_AND_ASSIGN(MySingletonOnlyClass);
// MySingletonOnlyClass() { ... }
// friend class Singleton<MySingletonOnlyClass>;
// }
//
// If your singleton requires complex initialization, or does not have a
// suitable default constructor, you can provide a specialization of
// Singleton<Type>::CreateInstance() to perform the appropriate setup, e.g.:
//
// template <>
// Type* Singleton<VirtualType>::CreateInstance() { return new ConcreteImpl; }
//
// If you want to initialize something eagerly at startup, rather than lazily
// upon use, consider using REGISTER_MODULE_INITIALIZER (in base/googleinit.h).
//
// This class also allows users to pick a particular instance as the
// singleton with InjectInstance(). This enables unittesting and
// dependency injection. It must only be used at program startup.
//
// Caveats:
// (a) The instance is normally never destroyed. Destroying a Singleton is
// complex and error-prone; C++ books go on about this at great length,
// and I have seen no perfect general solution to the problem.
// We *do* offer UnsafeReset() which is not thread-safe at all.
//
// (b) Your class must have a default (no-argument) constructor, or you must
// provide a specialization for Singleton<Type>::CreateInstance().
//
// (c) Your class's constructor must never throw an exception.
//
// Singleton::get() is very fast - about 1ns on a 2.4GHz Core 2.
//
// The following only applies to changes made to this file as part of YugaByte development.
//
// Portions Copyright (c) YugaByte, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations
// under the License.
//
#pragma once
#include <stddef.h>
#include "yb/util/logging.h"
#include "yb/gutil/once.h"
namespace util {
namespace gtl {
template <typename SingletonType> class ScopedSingletonOverride;
template <typename SingletonType> class ScopedSingletonOverrideNoDelete;
} // namespace gtl
} // namespace util
template <typename Type>
class Singleton {
public:
// Return a pointer to the one true instance of the class.
static Type* get() {
GoogleOnceInit(&once_, &Singleton<Type>::Init);
return instance_;
}
// WARNING!!! This function is not thread-safe and may leak memory.
static void UnsafeReset() {
delete instance_;
instance_ = NULL;
once_.state = GOOGLE_ONCE_INTERNAL_INIT; // This is the bad part!
}
// This function is used to replace the instance used by
// Singleton<Type>::get(). It can be used for breaking dependencies. For
// unittesting, you probably want to use ScopedSingletonOverride instead.
//
// This function must be called before Singleton<Type>::get() is
// called and before any threads are created. If these assumptions
// are violated, anything could happen, but we try to crash in debug
// mode and do nothing in production.
static void InjectInstance(Type* instance) {
injected_instance_ = instance;
GoogleOnceInit(&once_, &Singleton<Type>::Inject);
injected_instance_ = NULL; // Helps detect leaks in the unittest.
if (instance_ != instance) {
LOG(DFATAL) << "(jyasskin) InjectInstance() must be called at most once"
<< " at the start of the program, before the Singleton has"
<< " been accessed and before any threads have been created."
<< " Ignoring the call in production.";
delete instance;
}
}
private:
friend class util::gtl::ScopedSingletonOverride<Type>;
friend class util::gtl::ScopedSingletonOverrideNoDelete<Type>;
// Create the instance.
static void Init() {
instance_ = CreateInstance();
}
// Create and return the instance. You can use Singleton for objects which
// require more complex setup by defining a specialization for your type.
static Type* CreateInstance() {
// use ::new to work around a gcc bug when operator new is overloaded
return ::new Type;
}
// Inject the instance.
static void Inject() {
instance_ = injected_instance_;
}
// Used by ScopedSingletonOverride. Definitely not threadsafe. No one
// should be calling this other than ScopedSingletonOverride (which has
// friend access to do this and makes sure it calls get() first).
static void OverrideSingleton(Type* override_instance) {
instance_ = override_instance;
}
static GoogleOnceType once_;
static Type* instance_;
static Type* injected_instance_;
};
template <typename Type>
GoogleOnceType Singleton<Type>::once_ = GOOGLE_ONCE_INIT;
template <typename Type>
Type* Singleton<Type>::instance_ = NULL;
template <typename Type>
Type* Singleton<Type>::injected_instance_ = NULL;