this hurts

Dependencies:   FFT

Committer:
shyamgatech
Date:
Thu Dec 03 18:15:35 2020 +0000
Revision:
7:0d62545e6d73
Parent:
0:d6c9b09b4042
addded gui mbed code;

Who changed what in which revision?

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