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