Forked.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Parent:
149:156823d33999
This updates the lib to the mbed lib v 145

Who changed what in which revision?

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