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