Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Motordriver mbed-rtos PololuLedStrip
mbed/mbed/platform/SingletonPtr.h@3:a3ed7ff99772, 2019-12-06 (annotated)
- Committer:
- arogliero3
- Date:
- Fri Dec 06 00:58:02 2019 -0500
- Revision:
- 3:a3ed7ff99772
- Parent:
- 0:e0dbd261724a
update img6
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| arogliero3 | 0:e0dbd261724a | 1 | |
| arogliero3 | 0:e0dbd261724a | 2 | /** \addtogroup platform */ |
| arogliero3 | 0:e0dbd261724a | 3 | /** @{*/ |
| arogliero3 | 0:e0dbd261724a | 4 | /* mbed Microcontroller Library |
| arogliero3 | 0:e0dbd261724a | 5 | * Copyright (c) 2006-2013 ARM Limited |
| arogliero3 | 0:e0dbd261724a | 6 | * |
| arogliero3 | 0:e0dbd261724a | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| arogliero3 | 0:e0dbd261724a | 8 | * you may not use this file except in compliance with the License. |
| arogliero3 | 0:e0dbd261724a | 9 | * You may obtain a copy of the License at |
| arogliero3 | 0:e0dbd261724a | 10 | * |
| arogliero3 | 0:e0dbd261724a | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| arogliero3 | 0:e0dbd261724a | 12 | * |
| arogliero3 | 0:e0dbd261724a | 13 | * Unless required by applicable law or agreed to in writing, software |
| arogliero3 | 0:e0dbd261724a | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| arogliero3 | 0:e0dbd261724a | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| arogliero3 | 0:e0dbd261724a | 16 | * See the License for the specific language governing permissions and |
| arogliero3 | 0:e0dbd261724a | 17 | * limitations under the License. |
| arogliero3 | 0:e0dbd261724a | 18 | */ |
| arogliero3 | 0:e0dbd261724a | 19 | #ifndef SINGLETONPTR_H |
| arogliero3 | 0:e0dbd261724a | 20 | #define SINGLETONPTR_H |
| arogliero3 | 0:e0dbd261724a | 21 | |
| arogliero3 | 0:e0dbd261724a | 22 | #include <stdint.h> |
| arogliero3 | 0:e0dbd261724a | 23 | #include <new> |
| arogliero3 | 0:e0dbd261724a | 24 | #include "platform/mbed_assert.h" |
| arogliero3 | 0:e0dbd261724a | 25 | #ifdef MBED_CONF_RTOS_PRESENT |
| arogliero3 | 0:e0dbd261724a | 26 | #include "cmsis_os.h" |
| arogliero3 | 0:e0dbd261724a | 27 | #endif |
| arogliero3 | 0:e0dbd261724a | 28 | |
| arogliero3 | 0:e0dbd261724a | 29 | #ifdef MBED_CONF_RTOS_PRESENT |
| arogliero3 | 0:e0dbd261724a | 30 | extern osMutexId singleton_mutex_id; |
| arogliero3 | 0:e0dbd261724a | 31 | #endif |
| arogliero3 | 0:e0dbd261724a | 32 | |
| arogliero3 | 0:e0dbd261724a | 33 | /** Lock the singleton mutex |
| arogliero3 | 0:e0dbd261724a | 34 | * |
| arogliero3 | 0:e0dbd261724a | 35 | * This function is typically used to provide |
| arogliero3 | 0:e0dbd261724a | 36 | * exclusive access when initializing a |
| arogliero3 | 0:e0dbd261724a | 37 | * global object. |
| arogliero3 | 0:e0dbd261724a | 38 | */ |
| arogliero3 | 0:e0dbd261724a | 39 | inline static void singleton_lock(void) |
| arogliero3 | 0:e0dbd261724a | 40 | { |
| arogliero3 | 0:e0dbd261724a | 41 | #ifdef MBED_CONF_RTOS_PRESENT |
| arogliero3 | 0:e0dbd261724a | 42 | osMutexWait(singleton_mutex_id, osWaitForever); |
| arogliero3 | 0:e0dbd261724a | 43 | #endif |
| arogliero3 | 0:e0dbd261724a | 44 | } |
| arogliero3 | 0:e0dbd261724a | 45 | |
| arogliero3 | 0:e0dbd261724a | 46 | /** Unlock the singleton mutex |
| arogliero3 | 0:e0dbd261724a | 47 | * |
| arogliero3 | 0:e0dbd261724a | 48 | * This function is typically used to provide |
| arogliero3 | 0:e0dbd261724a | 49 | * exclusive access when initializing a |
| arogliero3 | 0:e0dbd261724a | 50 | * global object. |
| arogliero3 | 0:e0dbd261724a | 51 | */ |
| arogliero3 | 0:e0dbd261724a | 52 | inline static void singleton_unlock(void) |
| arogliero3 | 0:e0dbd261724a | 53 | { |
| arogliero3 | 0:e0dbd261724a | 54 | #ifdef MBED_CONF_RTOS_PRESENT |
| arogliero3 | 0:e0dbd261724a | 55 | osMutexRelease (singleton_mutex_id); |
| arogliero3 | 0:e0dbd261724a | 56 | #endif |
| arogliero3 | 0:e0dbd261724a | 57 | } |
| arogliero3 | 0:e0dbd261724a | 58 | |
| arogliero3 | 0:e0dbd261724a | 59 | /** Utility class for creating an using a singleton |
| arogliero3 | 0:e0dbd261724a | 60 | * |
| arogliero3 | 0:e0dbd261724a | 61 | * @Note Synchronization level: Thread safe |
| arogliero3 | 0:e0dbd261724a | 62 | * |
| arogliero3 | 0:e0dbd261724a | 63 | * @Note: This class must only be used in a static context - |
| arogliero3 | 0:e0dbd261724a | 64 | * this class must never be allocated or created on the |
| arogliero3 | 0:e0dbd261724a | 65 | * stack. |
| arogliero3 | 0:e0dbd261724a | 66 | * |
| arogliero3 | 0:e0dbd261724a | 67 | * @Note: This class is lazily initialized on first use. |
| arogliero3 | 0:e0dbd261724a | 68 | * This class is a POD type so if it is not used it will |
| arogliero3 | 0:e0dbd261724a | 69 | * be garbage collected. |
| arogliero3 | 0:e0dbd261724a | 70 | */ |
| arogliero3 | 0:e0dbd261724a | 71 | template <class T> |
| arogliero3 | 0:e0dbd261724a | 72 | struct SingletonPtr { |
| arogliero3 | 0:e0dbd261724a | 73 | |
| arogliero3 | 0:e0dbd261724a | 74 | /** Get a pointer to the underlying singleton |
| arogliero3 | 0:e0dbd261724a | 75 | * |
| arogliero3 | 0:e0dbd261724a | 76 | * @returns |
| arogliero3 | 0:e0dbd261724a | 77 | * A pointer to the singleton |
| arogliero3 | 0:e0dbd261724a | 78 | */ |
| arogliero3 | 0:e0dbd261724a | 79 | T* get() { |
| arogliero3 | 0:e0dbd261724a | 80 | if (NULL == _ptr) { |
| arogliero3 | 0:e0dbd261724a | 81 | singleton_lock(); |
| arogliero3 | 0:e0dbd261724a | 82 | if (NULL == _ptr) { |
| arogliero3 | 0:e0dbd261724a | 83 | _ptr = new (_data) T(); |
| arogliero3 | 0:e0dbd261724a | 84 | } |
| arogliero3 | 0:e0dbd261724a | 85 | singleton_unlock(); |
| arogliero3 | 0:e0dbd261724a | 86 | } |
| arogliero3 | 0:e0dbd261724a | 87 | // _ptr was not zero initialized or was |
| arogliero3 | 0:e0dbd261724a | 88 | // corrupted if this assert is hit |
| arogliero3 | 0:e0dbd261724a | 89 | MBED_ASSERT(_ptr == (T *)&_data); |
| arogliero3 | 0:e0dbd261724a | 90 | return _ptr; |
| arogliero3 | 0:e0dbd261724a | 91 | } |
| arogliero3 | 0:e0dbd261724a | 92 | |
| arogliero3 | 0:e0dbd261724a | 93 | /** Get a pointer to the underlying singleton |
| arogliero3 | 0:e0dbd261724a | 94 | * |
| arogliero3 | 0:e0dbd261724a | 95 | * @returns |
| arogliero3 | 0:e0dbd261724a | 96 | * A pointer to the singleton |
| arogliero3 | 0:e0dbd261724a | 97 | */ |
| arogliero3 | 0:e0dbd261724a | 98 | T* operator->() { |
| arogliero3 | 0:e0dbd261724a | 99 | return get(); |
| arogliero3 | 0:e0dbd261724a | 100 | } |
| arogliero3 | 0:e0dbd261724a | 101 | |
| arogliero3 | 0:e0dbd261724a | 102 | // This is zero initialized when in global scope |
| arogliero3 | 0:e0dbd261724a | 103 | T *_ptr; |
| arogliero3 | 0:e0dbd261724a | 104 | // Force data to be 4 byte aligned |
| arogliero3 | 0:e0dbd261724a | 105 | uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
| arogliero3 | 0:e0dbd261724a | 106 | }; |
| arogliero3 | 0:e0dbd261724a | 107 | |
| arogliero3 | 0:e0dbd261724a | 108 | #endif |
| arogliero3 | 0:e0dbd261724a | 109 | |
| arogliero3 | 0:e0dbd261724a | 110 | /** @}*/ |