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