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@123:b0220dba8be7, 2016-08-12 (annotated)
- Committer:
- Kojto
- Date:
- Fri Aug 12 13:04:35 2016 +0200
- Revision:
- 123:b0220dba8be7
- Child:
- 125:2e9cc70d1897
Release 123 of the mbed library
Changes:
- new targets: nucleo_f207zg, beetle, nrf51_dk, hexiwear,
nuvoton nuc472, vk rz a1h
- ST - fix timer interrupt handler, sleep api fix
- NXP - lpc15xx us ticker fix
- Nordic - analogin fixes, LF clock init addition, enable i2c async
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(); |
Kojto | 123:b0220dba8be7 | 79 | _ptr = new (_data) T; |
Kojto | 123:b0220dba8be7 | 80 | singleton_unlock(); |
Kojto | 123:b0220dba8be7 | 81 | } |
Kojto | 123:b0220dba8be7 | 82 | // _ptr was not zero initialized or was |
Kojto | 123:b0220dba8be7 | 83 | // corrupted if this assert is hit |
Kojto | 123:b0220dba8be7 | 84 | MBED_ASSERT(_ptr == (T *)&_data); |
Kojto | 123:b0220dba8be7 | 85 | return _ptr; |
Kojto | 123:b0220dba8be7 | 86 | } |
Kojto | 123:b0220dba8be7 | 87 | |
Kojto | 123:b0220dba8be7 | 88 | /** Get a pointer to the underlying singleton |
Kojto | 123:b0220dba8be7 | 89 | * |
Kojto | 123:b0220dba8be7 | 90 | * @returns |
Kojto | 123:b0220dba8be7 | 91 | * A pointer to the singleton |
Kojto | 123:b0220dba8be7 | 92 | */ |
Kojto | 123:b0220dba8be7 | 93 | T* operator->() { |
Kojto | 123:b0220dba8be7 | 94 | return get(); |
Kojto | 123:b0220dba8be7 | 95 | } |
Kojto | 123:b0220dba8be7 | 96 | |
Kojto | 123:b0220dba8be7 | 97 | // This is zero initialized when in global scope |
Kojto | 123:b0220dba8be7 | 98 | T *_ptr; |
Kojto | 123:b0220dba8be7 | 99 | // Force data to be 4 byte aligned |
Kojto | 123:b0220dba8be7 | 100 | uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
Kojto | 123:b0220dba8be7 | 101 | }; |
Kojto | 123:b0220dba8be7 | 102 | |
Kojto | 123:b0220dba8be7 | 103 | #endif |