Mbed OS Reference
Loading...
Searching...
No Matches
SingletonPtr.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef SINGLETONPTR_H
18#define SINGLETONPTR_H
19
20#include <stdlib.h>
21#include <stdint.h>
22#include <new>
23#include "platform/mbed_assert.h"
24#include "platform/mbed_atomic.h"
25
26
27/** \addtogroup platform-public-api */
28/** @{*/
29
30/**
31 * \defgroup platform_SingletonPtr SingletonPtr class
32 * @{
33 */
34
35/** Lock the singleton mutex
36 *
37 * This function is typically used to provide
38 * exclusive access when initializing a
39 * global object.
40 */
41void singleton_lock(void);
42
43/** Unlock the singleton mutex
44 *
45 * This function is typically used to provide
46 * exclusive access when initializing a
47 * global object.
48 */
50
51/** Utility class for creating and using a singleton
52 *
53 * @note Synchronization level: Thread safe
54 *
55 * @note: This class is lazily initialized on first use.
56 * This class has a constexpr default constructor so if it is
57 * not used as a non-local variable it will be garbage collected.
58 *
59 * @note: This class would normally be used in a static standalone
60 * context. It does not call the destructor of the wrapped object
61 * when it is destroyed, effectively ensuring linker exclusion of the
62 * destructor for static objects. If used in another context, such as
63 * a member of a normal class wanting "initialize on first-use"
64 * semantics on a member, care should be taken to call the destroy
65 * method manually if necessary.
66 *
67 * @note: If used as a sub-object of a class, that class's own
68 * constructor must be constexpr to achieve its exclusion by
69 * the linker when unused. That will require explicit
70 * initialization of its other members.
71 *
72 * @note: More detail on initialization: Formerly, SingletonPtr
73 * had no constructor, so was "zero-initialized" when non-local.
74 * So if enclosed in another class with no constructor, the whole
75 * thing would be zero-initialized, and linker-excludable.
76 * Having no constructor meant SingletonPtr was not constexpr,
77 * which limited applicability in other contexts. With its new
78 * constexpr constructor, it is now "constant-initialized" when
79 * non-local. This achieves the same effect as a standalone
80 * non-local object, but as a sub-object linker exclusion is
81 * now only achieved if the outer object is itself using a
82 * constexpr constructor to get constant-initialization.
83 * Otherwise, the outer object will be neither zero-initialized
84 * nor constant-initialized, so will be "dynamic-initialized",
85 * and likely to be left in by the linker.
86 */
87template <class T>
89
90 // Initializers are required to make default constructor constexpr
91 // This adds no overhead as a static object - the compiler and linker can
92 // figure out that we are effectively zero-init, and either place us in
93 // ".bss", or exclude us if unused.
94 constexpr SingletonPtr() noexcept : _ptr(), _data() { }
95
96 /** Get a pointer to the underlying singleton
97 *
98 * @returns
99 * A pointer to the singleton
100 */
101 T *get() const
102 {
103 T *p = core_util_atomic_load(&_ptr);
104 if (p == NULL) {
106 p = _ptr;
107 if (p == NULL) {
108 p = new (_data) T();
109 core_util_atomic_store(&_ptr, p);
110 }
112 }
113 // _ptr was not zero initialized or was
114 // corrupted if this assert is hit
115 MBED_ASSERT(p == reinterpret_cast<T *>(&_data));
116 return p;
117 }
118
119 /** Get a pointer to the underlying singleton
120 *
121 * @returns
122 * A pointer to the singleton
123 */
124 T *operator->() const
125 {
126 return get();
127 }
128
129 /** Get a reference to the underlying singleton
130 *
131 * @returns
132 * A reference to the singleton
133 */
134 T &operator*() const
135 {
136 return *get();
137 }
138
139 /** Get a pointer to the underlying singleton
140 *
141 * Gets a pointer without initialization - can be
142 * used as an optimization when it is known that
143 * initialization must have already occurred.
144 *
145 * @returns
146 * A pointer to the singleton, or NULL if not
147 * initialized.
148 */
149 T *get_no_init() const
150 {
151 return _ptr;
152 }
153
154 /** Destroy the underlying singleton
155 *
156 * The underlying singleton is never automatically destroyed;
157 * this is a potential optimization to avoid destructors
158 * being pulled into an embedded image on the exit path,
159 * which should never occur. The destructor can be
160 * manually invoked via this call.
161 *
162 * Unlike construction, this is not thread-safe. After this call,
163 * no further operations on the object are permitted.
164 *
165 * Is a no-op if the object has not been constructed.
166 */
167 void destroy()
168 {
169 if (_ptr) {
170 _ptr->~T();
171 }
172 }
173
174 mutable T *_ptr;
175#if __cplusplus >= 201103L
176 // Align data appropriately
177 alignas(T) mutable char _data[sizeof(T)];
178#else
179 // Force data to be 8 byte aligned
180 mutable uint64_t _data[(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
181#endif
182};
183
184#endif
185/**@}*/
186
187/**@}*/
#define MBED_ASSERT(expr)
MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.
Definition: mbed_assert.h:66
void singleton_unlock(void)
Unlock the singleton mutex.
void singleton_lock(void)
Lock the singleton mutex.
T * core_util_atomic_load(T *const volatile *valuePtr) noexcept
Utility class for creating and using a singleton.
Definition: SingletonPtr.h:88
T & operator*() const
Get a reference to the underlying singleton.
Definition: SingletonPtr.h:134
void destroy()
Destroy the underlying singleton.
Definition: SingletonPtr.h:167
T * get_no_init() const
Get a pointer to the underlying singleton.
Definition: SingletonPtr.h:149
T * operator->() const
Get a pointer to the underlying singleton.
Definition: SingletonPtr.h:124
T * get() const
Get a pointer to the underlying singleton.
Definition: SingletonPtr.h:101