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