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