temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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