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.
SingletonPtr.h@125:2e9cc70d1897, 2016-08-31 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Aug 31 18:09:46 2016 +0100
- Revision:
- 125:2e9cc70d1897
- Parent:
- 123:b0220dba8be7
Release 125 of the mbed library
Changes:
New target - KL27Z_IAR
New target - MAX32620HSP_ARM_STD
New target - MAX32620HSP_GCC_ARM
New target - MAX32620HSP_IAR
New target - NCS36510_ARM_STD
New target - NCS36510_GCC_ARM
New target - NCS36510_IAR
Added support for NSAPI_REUSEADDR to the lwip interface.
STM32F3 family : Add and enable asynchronous serial, plus tests.
STM32L4 family : Add and enable asynchronous serial, plus tests.
Fixing issue where GCC fails to report compile errors when non-verbose.
Add ethernet and IPV4 support for: NUCLEO_F207ZG, NUCLEO_F429ZI, NUCLEO_F767ZI, DISCO_F746NG.
RZ_A1H - Enable SPI1 on pins P6_4 to P6_7.
KL27Z : SPI driver bug fixes and Improvements, ARM linker file update.
STM32F4, STM32F7 families : Add entropy functions, documentation, code improvements, fix build issues.
HEXIWEAR: Update I2C pin mapping, Add support to create KDS projects.
LWIP - fix recv blocking send on accepted sockets.
SingletonPtr bugfixes.
Beetle: Implement sleep API.
uVisor: Update to v0.20.1-alpha, minor documentation update.
STM32F3 : fix RTOS IAR test, RTOS GCC_ARM test.
nrf5x : Introduce uart hardware flow control configuration.
K64F/K22F: Implement HAL lp_timer API.
Ticker: Move ticker initialisation to object creation time.
STM32F4 : remove printf from pwmout
NXP : Fix multiple definition errors in GCC_CR build, fix linker errors.
Add TOOLCHAIN_GCC_CR support.
STM32L1 family : Add and enable asynchronous serial, plus tests.
mbed-client : Fix Bootstrap and Connector functionality.
NUC472 : Fix Ethernet wrong INT status in RX_Action.
RTX_CM_lib.h : fix compiler warning.
NUCLEO : Use GCC small build for 64K flash STM32.
STM32F2 family : Add and enable asynchronous serial, plus tests.
uvisor : Move page heap after uVisor private data, update page allocator.
K64F: Revert to hardcoded stack pointer in RTX .
dns-query : Internal API change , documentation, Added support for multiple results and ipv6.
Add support for implementation-provided DNS servers.
Adopted netconn_gethostbyname in the lwip interface.
Restructured nsapi_dns.h to have clear separation between C/C++ .
Tool fixes.
Tests : New ones added and some updates to existing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 123:b0220dba8be7 | 1 | /* mbed Microcontroller Library |
Kojto | 123:b0220dba8be7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 123:b0220dba8be7 | 3 | * |
Kojto | 123:b0220dba8be7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 123:b0220dba8be7 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 123:b0220dba8be7 | 6 | * You may obtain a copy of the License at |
Kojto | 123:b0220dba8be7 | 7 | * |
Kojto | 123:b0220dba8be7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 123:b0220dba8be7 | 9 | * |
Kojto | 123:b0220dba8be7 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 123:b0220dba8be7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 123:b0220dba8be7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 123:b0220dba8be7 | 13 | * See the License for the specific language governing permissions and |
Kojto | 123:b0220dba8be7 | 14 | * limitations under the License. |
Kojto | 123:b0220dba8be7 | 15 | */ |
Kojto | 123:b0220dba8be7 | 16 | #ifndef SINGLETONPTR_H |
Kojto | 123:b0220dba8be7 | 17 | #define SINGLETONPTR_H |
Kojto | 123:b0220dba8be7 | 18 | |
Kojto | 123:b0220dba8be7 | 19 | #include <stdint.h> |
Kojto | 123:b0220dba8be7 | 20 | #include <new> |
Kojto | 123:b0220dba8be7 | 21 | #include "mbed_assert.h" |
Kojto | 123:b0220dba8be7 | 22 | #ifdef MBED_CONF_RTOS_PRESENT |
Kojto | 123:b0220dba8be7 | 23 | #include "cmsis_os.h" |
Kojto | 123:b0220dba8be7 | 24 | #endif |
Kojto | 123:b0220dba8be7 | 25 | |
Kojto | 123:b0220dba8be7 | 26 | #ifdef MBED_CONF_RTOS_PRESENT |
Kojto | 123:b0220dba8be7 | 27 | extern osMutexId singleton_mutex_id; |
Kojto | 123:b0220dba8be7 | 28 | #endif |
Kojto | 123:b0220dba8be7 | 29 | |
Kojto | 123:b0220dba8be7 | 30 | /** Lock the singleton mutex |
Kojto | 123:b0220dba8be7 | 31 | * |
Kojto | 123:b0220dba8be7 | 32 | * This function is typically used to provide |
Kojto | 123:b0220dba8be7 | 33 | * exclusive access when initializing a |
Kojto | 123:b0220dba8be7 | 34 | * global object. |
Kojto | 123:b0220dba8be7 | 35 | */ |
Kojto | 123:b0220dba8be7 | 36 | inline static void singleton_lock(void) |
Kojto | 123:b0220dba8be7 | 37 | { |
Kojto | 123:b0220dba8be7 | 38 | #ifdef MBED_CONF_RTOS_PRESENT |
Kojto | 123:b0220dba8be7 | 39 | osMutexWait(singleton_mutex_id, osWaitForever); |
Kojto | 123:b0220dba8be7 | 40 | #endif |
Kojto | 123:b0220dba8be7 | 41 | } |
Kojto | 123:b0220dba8be7 | 42 | |
Kojto | 123:b0220dba8be7 | 43 | /** Unlock the singleton mutex |
Kojto | 123:b0220dba8be7 | 44 | * |
Kojto | 123:b0220dba8be7 | 45 | * This function is typically used to provide |
Kojto | 123:b0220dba8be7 | 46 | * exclusive access when initializing a |
Kojto | 123:b0220dba8be7 | 47 | * global object. |
Kojto | 123:b0220dba8be7 | 48 | */ |
Kojto | 123:b0220dba8be7 | 49 | inline static void singleton_unlock(void) |
Kojto | 123:b0220dba8be7 | 50 | { |
Kojto | 123:b0220dba8be7 | 51 | #ifdef MBED_CONF_RTOS_PRESENT |
Kojto | 123:b0220dba8be7 | 52 | osMutexRelease (singleton_mutex_id); |
Kojto | 123:b0220dba8be7 | 53 | #endif |
Kojto | 123:b0220dba8be7 | 54 | } |
Kojto | 123:b0220dba8be7 | 55 | |
Kojto | 123:b0220dba8be7 | 56 | /** Utility class for creating an using a singleton |
Kojto | 123:b0220dba8be7 | 57 | * |
Kojto | 123:b0220dba8be7 | 58 | * @Note Synchronization level: Thread safe |
Kojto | 123:b0220dba8be7 | 59 | * |
Kojto | 123:b0220dba8be7 | 60 | * @Note: This class must only be used in a static context - |
Kojto | 123:b0220dba8be7 | 61 | * this class must never be allocated or created on the |
Kojto | 123:b0220dba8be7 | 62 | * stack. |
Kojto | 123:b0220dba8be7 | 63 | * |
Kojto | 123:b0220dba8be7 | 64 | * @Note: This class is lazily initialized on first use. |
Kojto | 123:b0220dba8be7 | 65 | * This class is a POD type so if it is not used it will |
Kojto | 123:b0220dba8be7 | 66 | * be garbage collected. |
Kojto | 123:b0220dba8be7 | 67 | */ |
Kojto | 123:b0220dba8be7 | 68 | template <class T> |
Kojto | 123:b0220dba8be7 | 69 | struct SingletonPtr { |
Kojto | 123:b0220dba8be7 | 70 | |
Kojto | 123:b0220dba8be7 | 71 | /** Get a pointer to the underlying singleton |
Kojto | 123:b0220dba8be7 | 72 | * |
Kojto | 123:b0220dba8be7 | 73 | * @returns |
Kojto | 123:b0220dba8be7 | 74 | * A pointer to the singleton |
Kojto | 123:b0220dba8be7 | 75 | */ |
Kojto | 123:b0220dba8be7 | 76 | T* get() { |
Kojto | 123:b0220dba8be7 | 77 | if (NULL == _ptr) { |
Kojto | 123:b0220dba8be7 | 78 | singleton_lock(); |
AnnaBridge | 125:2e9cc70d1897 | 79 | if (NULL == _ptr) { |
AnnaBridge | 125:2e9cc70d1897 | 80 | _ptr = new (_data) T(); |
AnnaBridge | 125:2e9cc70d1897 | 81 | } |
Kojto | 123:b0220dba8be7 | 82 | singleton_unlock(); |
Kojto | 123:b0220dba8be7 | 83 | } |
Kojto | 123:b0220dba8be7 | 84 | // _ptr was not zero initialized or was |
Kojto | 123:b0220dba8be7 | 85 | // corrupted if this assert is hit |
Kojto | 123:b0220dba8be7 | 86 | MBED_ASSERT(_ptr == (T *)&_data); |
Kojto | 123:b0220dba8be7 | 87 | return _ptr; |
Kojto | 123:b0220dba8be7 | 88 | } |
Kojto | 123:b0220dba8be7 | 89 | |
Kojto | 123:b0220dba8be7 | 90 | /** Get a pointer to the underlying singleton |
Kojto | 123:b0220dba8be7 | 91 | * |
Kojto | 123:b0220dba8be7 | 92 | * @returns |
Kojto | 123:b0220dba8be7 | 93 | * A pointer to the singleton |
Kojto | 123:b0220dba8be7 | 94 | */ |
Kojto | 123:b0220dba8be7 | 95 | T* operator->() { |
Kojto | 123:b0220dba8be7 | 96 | return get(); |
Kojto | 123:b0220dba8be7 | 97 | } |
Kojto | 123:b0220dba8be7 | 98 | |
Kojto | 123:b0220dba8be7 | 99 | // This is zero initialized when in global scope |
Kojto | 123:b0220dba8be7 | 100 | T *_ptr; |
Kojto | 123:b0220dba8be7 | 101 | // Force data to be 4 byte aligned |
Kojto | 123:b0220dba8be7 | 102 | uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
Kojto | 123:b0220dba8be7 | 103 | }; |
Kojto | 123:b0220dba8be7 | 104 | |
Kojto | 123:b0220dba8be7 | 105 | #endif |