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