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