mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SingletonPtr.h Source File

SingletonPtr.h

00001 
00002 /** \addtogroup platform */
00003 /** @{*/
00004 /**
00005  * \defgroup platform_SingletonPtr SingletonPtr class
00006  * @{
00007  */
00008 /* mbed Microcontroller Library
00009  * Copyright (c) 2006-2013 ARM Limited
00010  * SPDX-License-Identifier: Apache-2.0
00011  *
00012  * Licensed under the Apache License, Version 2.0 (the "License");
00013  * you may not use this file except in compliance with the License.
00014  * You may obtain a copy of the License at
00015  *
00016  *     http://www.apache.org/licenses/LICENSE-2.0
00017  *
00018  * Unless required by applicable law or agreed to in writing, software
00019  * distributed under the License is distributed on an "AS IS" BASIS,
00020  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  * See the License for the specific language governing permissions and
00022  * limitations under the License.
00023  */
00024 #ifndef SINGLETONPTR_H
00025 #define SINGLETONPTR_H
00026 
00027 #include <stdlib.h>
00028 #include <stdint.h>
00029 #include <new>
00030 #include "platform/mbed_assert.h"
00031 #ifdef MBED_CONF_RTOS_PRESENT
00032 #include "cmsis_os2.h"
00033 #endif
00034 
00035 #ifdef MBED_CONF_RTOS_PRESENT
00036 extern osMutexId_t singleton_mutex_id;
00037 #endif
00038 
00039 /** Lock the singleton mutex
00040  *
00041  * This function is typically used to provide
00042  * exclusive access when initializing a
00043  * global object.
00044  */
00045 inline static void singleton_lock(void)
00046 {
00047 #ifdef MBED_CONF_RTOS_PRESENT
00048     if (!singleton_mutex_id) {
00049         // RTOS has not booted yet so no mutex is needed
00050         return;
00051     }
00052     osMutexAcquire(singleton_mutex_id, osWaitForever);
00053 #endif
00054 }
00055 
00056 /** Unlock the singleton mutex
00057  *
00058  * This function is typically used to provide
00059  * exclusive access when initializing a
00060  * global object.
00061  */
00062 inline static void singleton_unlock(void)
00063 {
00064 #ifdef MBED_CONF_RTOS_PRESENT
00065     if (!singleton_mutex_id) {
00066         // RTOS has not booted yet so no mutex is needed
00067         return;
00068     }
00069     osMutexRelease(singleton_mutex_id);
00070 #endif
00071 }
00072 
00073 /** Utility class for creating an using a singleton
00074  *
00075  * @note Synchronization level: Thread safe
00076  *
00077  * @note: This class must only be used in a static context -
00078  * this class must never be allocated or created on the
00079  * stack.
00080  *
00081  * @note: This class is lazily initialized on first use.
00082  * This class is a POD type so if it is not used it will
00083  * be garbage collected.
00084  */
00085 template <class T>
00086 struct SingletonPtr {
00087 
00088     /** Get a pointer to the underlying singleton
00089      *
00090      * @returns
00091      *   A pointer to the singleton
00092      */
00093     T *get() const
00094     {
00095         if (NULL == _ptr) {
00096             singleton_lock();
00097             if (NULL == _ptr) {
00098                 _ptr = new (_data) T();
00099             }
00100             singleton_unlock();
00101         }
00102         // _ptr was not zero initialized or was
00103         // corrupted if this assert is hit
00104         MBED_ASSERT(_ptr == (T *)&_data);
00105         return _ptr;
00106     }
00107 
00108     /** Get a pointer to the underlying singleton
00109      *
00110      * @returns
00111      *   A pointer to the singleton
00112      */
00113     T *operator->() const
00114     {
00115         return get();
00116     }
00117 
00118     /** Get a reference to the underlying singleton
00119      *
00120      * @returns
00121      *   A reference to the singleton
00122      */
00123     T &operator*() const
00124     {
00125         return *get();
00126     }
00127 
00128     // This is zero initialized when in global scope
00129     mutable T *_ptr;
00130 #if __cplusplus >= 201103L
00131     // Align data appropriately
00132     alignas(T) mutable char _data[sizeof(T)];
00133 #else
00134     // Force data to be 8 byte aligned
00135     mutable uint64_t _data[(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
00136 #endif
00137 };
00138 
00139 #endif
00140 /**@}*/
00141 
00142 /**@}*/