mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Thu Nov 08 11:46:34 2018 +0000
Revision:
188:bcfe06ba3d64
Parent:
187:0387e8f68319
Child:
189:f392fc9709a3
mbed-dev library. Release version 164

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