2016-10-04 20:02:44 +00:00
|
|
|
|
|
|
|
/** \addtogroup platform */
|
|
|
|
/** @{*/
|
2017-10-24 15:05:45 +00:00
|
|
|
/**
|
|
|
|
* \defgroup platform_SingletonPtr SingletonPtr class
|
|
|
|
* @{
|
|
|
|
*/
|
2016-06-27 23:55:07 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
2018-11-09 11:31:20 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-06-27 23:55:07 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef SINGLETONPTR_H
|
|
|
|
#define SINGLETONPTR_H
|
|
|
|
|
2018-11-26 15:53:19 +00:00
|
|
|
#include <stdlib.h>
|
2016-06-27 23:55:07 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <new>
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/mbed_assert.h"
|
2019-01-29 09:14:02 +00:00
|
|
|
#include "platform/mbed_critical.h"
|
2016-06-27 23:55:07 +00:00
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
2017-05-15 14:55:45 +00:00
|
|
|
#include "cmsis_os2.h"
|
2016-06-27 23:55:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
2017-05-15 14:55:45 +00:00
|
|
|
extern osMutexId_t singleton_mutex_id;
|
2016-06-27 23:55:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Lock the singleton mutex
|
|
|
|
*
|
|
|
|
* This function is typically used to provide
|
|
|
|
* exclusive access when initializing a
|
|
|
|
* global object.
|
|
|
|
*/
|
|
|
|
inline static void singleton_lock(void)
|
|
|
|
{
|
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
2018-08-13 15:34:08 +00:00
|
|
|
if (!singleton_mutex_id) {
|
|
|
|
// RTOS has not booted yet so no mutex is needed
|
|
|
|
return;
|
|
|
|
}
|
2017-05-15 14:55:45 +00:00
|
|
|
osMutexAcquire(singleton_mutex_id, osWaitForever);
|
2016-06-27 23:55:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Unlock the singleton mutex
|
|
|
|
*
|
|
|
|
* This function is typically used to provide
|
|
|
|
* exclusive access when initializing a
|
|
|
|
* global object.
|
|
|
|
*/
|
|
|
|
inline static void singleton_unlock(void)
|
|
|
|
{
|
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
2018-08-13 15:34:08 +00:00
|
|
|
if (!singleton_mutex_id) {
|
|
|
|
// RTOS has not booted yet so no mutex is needed
|
|
|
|
return;
|
|
|
|
}
|
2018-06-27 14:09:15 +00:00
|
|
|
osMutexRelease(singleton_mutex_id);
|
2016-06-27 23:55:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Utility class for creating an using a singleton
|
|
|
|
*
|
2017-04-25 19:37:08 +00:00
|
|
|
* @note Synchronization level: Thread safe
|
2016-06-27 23:55:07 +00:00
|
|
|
*
|
2017-04-25 19:37:08 +00:00
|
|
|
* @note: This class must only be used in a static context -
|
2016-06-27 23:55:07 +00:00
|
|
|
* this class must never be allocated or created on the
|
|
|
|
* stack.
|
|
|
|
*
|
2017-04-25 19:37:08 +00:00
|
|
|
* @note: This class is lazily initialized on first use.
|
2016-06-27 23:55:07 +00:00
|
|
|
* This class is a POD type so if it is not used it will
|
|
|
|
* be garbage collected.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
struct SingletonPtr {
|
|
|
|
|
|
|
|
/** Get a pointer to the underlying singleton
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* A pointer to the singleton
|
|
|
|
*/
|
2018-09-10 08:00:08 +00:00
|
|
|
T *get() const
|
2018-06-27 14:09:15 +00:00
|
|
|
{
|
2019-01-29 09:14:02 +00:00
|
|
|
T *p = static_cast<T *>(core_util_atomic_load_ptr(&_ptr));
|
|
|
|
if (p == NULL) {
|
2016-06-27 23:55:07 +00:00
|
|
|
singleton_lock();
|
2019-01-29 09:14:02 +00:00
|
|
|
p = static_cast<T *>(_ptr);
|
|
|
|
if (p == NULL) {
|
|
|
|
p = new (_data) T();
|
|
|
|
core_util_atomic_store_ptr(&_ptr, p);
|
2016-08-19 23:40:06 +00:00
|
|
|
}
|
2016-06-27 23:55:07 +00:00
|
|
|
singleton_unlock();
|
|
|
|
}
|
|
|
|
// _ptr was not zero initialized or was
|
|
|
|
// corrupted if this assert is hit
|
2019-01-29 09:14:02 +00:00
|
|
|
MBED_ASSERT(p == reinterpret_cast<T *>(&_data));
|
|
|
|
return p;
|
2016-06-27 23:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a pointer to the underlying singleton
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* A pointer to the singleton
|
|
|
|
*/
|
2018-09-10 08:00:08 +00:00
|
|
|
T *operator->() const
|
2018-06-27 14:09:15 +00:00
|
|
|
{
|
2016-06-27 23:55:07 +00:00
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:36:55 +00:00
|
|
|
/** Get a reference to the underlying singleton
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* A reference to the singleton
|
|
|
|
*/
|
2018-09-10 08:00:08 +00:00
|
|
|
T &operator*() const
|
2018-08-29 13:36:55 +00:00
|
|
|
{
|
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:55:07 +00:00
|
|
|
// This is zero initialized when in global scope
|
2019-01-29 09:14:02 +00:00
|
|
|
mutable void *_ptr;
|
2018-09-10 08:10:41 +00:00
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
// Align data appropriately
|
|
|
|
alignas(T) mutable char _data[sizeof(T)];
|
|
|
|
#else
|
|
|
|
// Force data to be 8 byte aligned
|
|
|
|
mutable uint64_t _data[(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
|
|
|
|
#endif
|
2016-06-27 23:55:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2017-10-24 15:05:45 +00:00
|
|
|
/**@}*/
|
2016-10-04 20:02:44 +00:00
|
|
|
|
2017-10-24 15:05:45 +00:00
|
|
|
/**@}*/
|