001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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