Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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