mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1
<> 149:156823d33999 2 /** \addtogroup platform */
<> 149:156823d33999 3 /** @{*/
AnnaBridge 178:79309dc6340a 4 /**
AnnaBridge 178:79309dc6340a 5 * \defgroup platform_SingletonPtr SingletonPtr class
AnnaBridge 178:79309dc6340a 6 * @{
AnnaBridge 178:79309dc6340a 7 */
<> 149:156823d33999 8 /* mbed Microcontroller Library
<> 149:156823d33999 9 * Copyright (c) 2006-2013 ARM Limited
AnnaBridge 189:f392fc9709a3 10 * SPDX-License-Identifier: Apache-2.0
<> 149:156823d33999 11 *
<> 149:156823d33999 12 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 13 * you may not use this file except in compliance with the License.
<> 149:156823d33999 14 * You may obtain a copy of the License at
<> 149:156823d33999 15 *
<> 149:156823d33999 16 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 17 *
<> 149:156823d33999 18 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 19 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 21 * See the License for the specific language governing permissions and
<> 149:156823d33999 22 * limitations under the License.
<> 149:156823d33999 23 */
<> 149:156823d33999 24 #ifndef SINGLETONPTR_H
<> 149:156823d33999 25 #define SINGLETONPTR_H
<> 149:156823d33999 26
AnnaBridge 189:f392fc9709a3 27 #include <stdlib.h>
<> 149:156823d33999 28 #include <stdint.h>
<> 149:156823d33999 29 #include <new>
<> 149:156823d33999 30 #include "platform/mbed_assert.h"
<> 149:156823d33999 31 #ifdef MBED_CONF_RTOS_PRESENT
AnnaBridge 167:e84263d55307 32 #include "cmsis_os2.h"
<> 149:156823d33999 33 #endif
<> 149:156823d33999 34
<> 149:156823d33999 35 #ifdef MBED_CONF_RTOS_PRESENT
AnnaBridge 167:e84263d55307 36 extern osMutexId_t singleton_mutex_id;
<> 149:156823d33999 37 #endif
<> 149:156823d33999 38
<> 149:156823d33999 39 /** Lock the singleton mutex
<> 149:156823d33999 40 *
<> 149:156823d33999 41 * This function is typically used to provide
<> 149:156823d33999 42 * exclusive access when initializing a
<> 149:156823d33999 43 * global object.
<> 149:156823d33999 44 */
<> 149:156823d33999 45 inline static void singleton_lock(void)
<> 149:156823d33999 46 {
<> 149:156823d33999 47 #ifdef MBED_CONF_RTOS_PRESENT
AnnaBridge 188:bcfe06ba3d64 48 if (!singleton_mutex_id) {
AnnaBridge 188:bcfe06ba3d64 49 // RTOS has not booted yet so no mutex is needed
AnnaBridge 188:bcfe06ba3d64 50 return;
AnnaBridge 188:bcfe06ba3d64 51 }
AnnaBridge 167:e84263d55307 52 osMutexAcquire(singleton_mutex_id, osWaitForever);
<> 149:156823d33999 53 #endif
<> 149:156823d33999 54 }
<> 149:156823d33999 55
<> 149:156823d33999 56 /** Unlock the singleton mutex
<> 149:156823d33999 57 *
<> 149:156823d33999 58 * This function is typically used to provide
<> 149:156823d33999 59 * exclusive access when initializing a
<> 149:156823d33999 60 * global object.
<> 149:156823d33999 61 */
<> 149:156823d33999 62 inline static void singleton_unlock(void)
<> 149:156823d33999 63 {
<> 149:156823d33999 64 #ifdef MBED_CONF_RTOS_PRESENT
AnnaBridge 188:bcfe06ba3d64 65 if (!singleton_mutex_id) {
AnnaBridge 188:bcfe06ba3d64 66 // RTOS has not booted yet so no mutex is needed
AnnaBridge 188:bcfe06ba3d64 67 return;
AnnaBridge 188:bcfe06ba3d64 68 }
AnnaBridge 187:0387e8f68319 69 osMutexRelease(singleton_mutex_id);
<> 149:156823d33999 70 #endif
<> 149:156823d33999 71 }
<> 149:156823d33999 72
<> 149:156823d33999 73 /** Utility class for creating an using a singleton
<> 149:156823d33999 74 *
AnnaBridge 167:e84263d55307 75 * @note Synchronization level: Thread safe
<> 149:156823d33999 76 *
AnnaBridge 167:e84263d55307 77 * @note: This class must only be used in a static context -
<> 149:156823d33999 78 * this class must never be allocated or created on the
<> 149:156823d33999 79 * stack.
<> 149:156823d33999 80 *
AnnaBridge 167:e84263d55307 81 * @note: This class is lazily initialized on first use.
<> 149:156823d33999 82 * This class is a POD type so if it is not used it will
<> 149:156823d33999 83 * be garbage collected.
<> 149:156823d33999 84 */
<> 149:156823d33999 85 template <class T>
<> 149:156823d33999 86 struct SingletonPtr {
<> 149:156823d33999 87
<> 149:156823d33999 88 /** Get a pointer to the underlying singleton
<> 149:156823d33999 89 *
<> 149:156823d33999 90 * @returns
<> 149:156823d33999 91 * A pointer to the singleton
<> 149:156823d33999 92 */
AnnaBridge 189:f392fc9709a3 93 T *get() const
AnnaBridge 187:0387e8f68319 94 {
<> 149:156823d33999 95 if (NULL == _ptr) {
<> 149:156823d33999 96 singleton_lock();
<> 149:156823d33999 97 if (NULL == _ptr) {
<> 149:156823d33999 98 _ptr = new (_data) T();
<> 149:156823d33999 99 }
<> 149:156823d33999 100 singleton_unlock();
<> 149:156823d33999 101 }
<> 149:156823d33999 102 // _ptr was not zero initialized or was
<> 149:156823d33999 103 // corrupted if this assert is hit
<> 149:156823d33999 104 MBED_ASSERT(_ptr == (T *)&_data);
<> 149:156823d33999 105 return _ptr;
<> 149:156823d33999 106 }
<> 149:156823d33999 107
<> 149:156823d33999 108 /** Get a pointer to the underlying singleton
<> 149:156823d33999 109 *
<> 149:156823d33999 110 * @returns
<> 149:156823d33999 111 * A pointer to the singleton
<> 149:156823d33999 112 */
AnnaBridge 189:f392fc9709a3 113 T *operator->() const
AnnaBridge 187:0387e8f68319 114 {
<> 149:156823d33999 115 return get();
<> 149:156823d33999 116 }
<> 149:156823d33999 117
AnnaBridge 189:f392fc9709a3 118 /** Get a reference to the underlying singleton
AnnaBridge 189:f392fc9709a3 119 *
AnnaBridge 189:f392fc9709a3 120 * @returns
AnnaBridge 189:f392fc9709a3 121 * A reference to the singleton
AnnaBridge 189:f392fc9709a3 122 */
AnnaBridge 189:f392fc9709a3 123 T &operator*() const
AnnaBridge 189:f392fc9709a3 124 {
AnnaBridge 189:f392fc9709a3 125 return *get();
AnnaBridge 189:f392fc9709a3 126 }
AnnaBridge 189:f392fc9709a3 127
<> 149:156823d33999 128 // This is zero initialized when in global scope
AnnaBridge 189:f392fc9709a3 129 mutable T *_ptr;
AnnaBridge 189:f392fc9709a3 130 #if __cplusplus >= 201103L
AnnaBridge 189:f392fc9709a3 131 // Align data appropriately
AnnaBridge 189:f392fc9709a3 132 alignas(T) mutable char _data[sizeof(T)];
AnnaBridge 189:f392fc9709a3 133 #else
AnnaBridge 189:f392fc9709a3 134 // Force data to be 8 byte aligned
AnnaBridge 189:f392fc9709a3 135 mutable uint64_t _data[(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
AnnaBridge 189:f392fc9709a3 136 #endif
<> 149:156823d33999 137 };
<> 149:156823d33999 138
<> 149:156823d33999 139 #endif
AnnaBridge 178:79309dc6340a 140 /**@}*/
<> 149:156823d33999 141
AnnaBridge 178:79309dc6340a 142 /**@}*/