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