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