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