mbed library sources, mbed-dev only for TYBLE16
Fork of mbed-dev by
Please refer flowing link.
/users/kenjiArai/notebook/tyble16-module-will-become-a-mbed-family--mbedliza/
platform/SingletonPtr.h@182:976b131d2800, 2018-02-25 (annotated)
- Committer:
- kenjiArai
- Date:
- Sun Feb 25 02:00:51 2018 +0000
- Revision:
- 182:976b131d2800
- Parent:
- 178:79309dc6340a
Only for TYBLE16
Who changed what in which revision?
User | Revision | Line number | New 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 | 167:e84263d55307 | 46 | osMutexAcquire(singleton_mutex_id, osWaitForever); |
<> | 149:156823d33999 | 47 | #endif |
<> | 149:156823d33999 | 48 | } |
<> | 149:156823d33999 | 49 | |
<> | 149:156823d33999 | 50 | /** Unlock the singleton mutex |
<> | 149:156823d33999 | 51 | * |
<> | 149:156823d33999 | 52 | * This function is typically used to provide |
<> | 149:156823d33999 | 53 | * exclusive access when initializing a |
<> | 149:156823d33999 | 54 | * global object. |
<> | 149:156823d33999 | 55 | */ |
<> | 149:156823d33999 | 56 | inline static void singleton_unlock(void) |
<> | 149:156823d33999 | 57 | { |
<> | 149:156823d33999 | 58 | #ifdef MBED_CONF_RTOS_PRESENT |
<> | 149:156823d33999 | 59 | osMutexRelease (singleton_mutex_id); |
<> | 149:156823d33999 | 60 | #endif |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | |
<> | 149:156823d33999 | 63 | /** Utility class for creating an using a singleton |
<> | 149:156823d33999 | 64 | * |
AnnaBridge | 167:e84263d55307 | 65 | * @note Synchronization level: Thread safe |
<> | 149:156823d33999 | 66 | * |
AnnaBridge | 167:e84263d55307 | 67 | * @note: This class must only be used in a static context - |
<> | 149:156823d33999 | 68 | * this class must never be allocated or created on the |
<> | 149:156823d33999 | 69 | * stack. |
<> | 149:156823d33999 | 70 | * |
AnnaBridge | 167:e84263d55307 | 71 | * @note: This class is lazily initialized on first use. |
<> | 149:156823d33999 | 72 | * This class is a POD type so if it is not used it will |
<> | 149:156823d33999 | 73 | * be garbage collected. |
<> | 149:156823d33999 | 74 | */ |
<> | 149:156823d33999 | 75 | template <class T> |
<> | 149:156823d33999 | 76 | struct SingletonPtr { |
<> | 149:156823d33999 | 77 | |
<> | 149:156823d33999 | 78 | /** Get a pointer to the underlying singleton |
<> | 149:156823d33999 | 79 | * |
<> | 149:156823d33999 | 80 | * @returns |
<> | 149:156823d33999 | 81 | * A pointer to the singleton |
<> | 149:156823d33999 | 82 | */ |
<> | 149:156823d33999 | 83 | T* get() { |
<> | 149:156823d33999 | 84 | if (NULL == _ptr) { |
<> | 149:156823d33999 | 85 | singleton_lock(); |
<> | 149:156823d33999 | 86 | if (NULL == _ptr) { |
<> | 149:156823d33999 | 87 | _ptr = new (_data) T(); |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | singleton_unlock(); |
<> | 149:156823d33999 | 90 | } |
<> | 149:156823d33999 | 91 | // _ptr was not zero initialized or was |
<> | 149:156823d33999 | 92 | // corrupted if this assert is hit |
<> | 149:156823d33999 | 93 | MBED_ASSERT(_ptr == (T *)&_data); |
<> | 149:156823d33999 | 94 | return _ptr; |
<> | 149:156823d33999 | 95 | } |
<> | 149:156823d33999 | 96 | |
<> | 149:156823d33999 | 97 | /** Get a pointer to the underlying singleton |
<> | 149:156823d33999 | 98 | * |
<> | 149:156823d33999 | 99 | * @returns |
<> | 149:156823d33999 | 100 | * A pointer to the singleton |
<> | 149:156823d33999 | 101 | */ |
<> | 149:156823d33999 | 102 | T* operator->() { |
<> | 149:156823d33999 | 103 | return get(); |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | // This is zero initialized when in global scope |
<> | 149:156823d33999 | 107 | T *_ptr; |
<> | 149:156823d33999 | 108 | // Force data to be 4 byte aligned |
<> | 149:156823d33999 | 109 | uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
<> | 149:156823d33999 | 110 | }; |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | #endif |
AnnaBridge | 178:79309dc6340a | 113 | /**@}*/ |
<> | 149:156823d33999 | 114 | |
AnnaBridge | 178:79309dc6340a | 115 | /**@}*/ |