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