Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

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