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