Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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