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