The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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