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