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