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