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