init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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