mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
rtos/Semaphore.h@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2006-2019 ARM Limited |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kenjiArai | 0:5b88d5760320 | 5 | * of this software and associated documentation files (the "Software"), to deal |
kenjiArai | 0:5b88d5760320 | 6 | * in the Software without restriction, including without limitation the rights |
kenjiArai | 0:5b88d5760320 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenjiArai | 0:5b88d5760320 | 8 | * copies of the Software, and to permit persons to whom the Software is |
kenjiArai | 0:5b88d5760320 | 9 | * furnished to do so, subject to the following conditions: |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * The above copyright notice and this permission notice shall be included in |
kenjiArai | 0:5b88d5760320 | 12 | * all copies or substantial portions of the Software. |
kenjiArai | 0:5b88d5760320 | 13 | * |
kenjiArai | 0:5b88d5760320 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenjiArai | 0:5b88d5760320 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenjiArai | 0:5b88d5760320 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenjiArai | 0:5b88d5760320 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenjiArai | 0:5b88d5760320 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:5b88d5760320 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kenjiArai | 0:5b88d5760320 | 20 | * SOFTWARE. |
kenjiArai | 0:5b88d5760320 | 21 | */ |
kenjiArai | 0:5b88d5760320 | 22 | #ifndef SEMAPHORE_H |
kenjiArai | 0:5b88d5760320 | 23 | #define SEMAPHORE_H |
kenjiArai | 0:5b88d5760320 | 24 | |
kenjiArai | 0:5b88d5760320 | 25 | #include <stdint.h> |
kenjiArai | 1:9db0e321a9f4 | 26 | #include "rtos/mbed_rtos_types.h" |
kenjiArai | 1:9db0e321a9f4 | 27 | #include "rtos/mbed_rtos1_types.h" |
kenjiArai | 1:9db0e321a9f4 | 28 | #include "rtos/mbed_rtos_storage.h" |
kenjiArai | 0:5b88d5760320 | 29 | #include "platform/mbed_toolchain.h" |
kenjiArai | 0:5b88d5760320 | 30 | #include "platform/NonCopyable.h" |
kenjiArai | 0:5b88d5760320 | 31 | |
kenjiArai | 0:5b88d5760320 | 32 | namespace rtos { |
kenjiArai | 1:9db0e321a9f4 | 33 | /** \addtogroup rtos-public-api */ |
kenjiArai | 0:5b88d5760320 | 34 | /** @{*/ |
kenjiArai | 1:9db0e321a9f4 | 35 | |
kenjiArai | 0:5b88d5760320 | 36 | /** |
kenjiArai | 0:5b88d5760320 | 37 | * \defgroup rtos_Semaphore Semaphore class |
kenjiArai | 0:5b88d5760320 | 38 | * @{ |
kenjiArai | 0:5b88d5760320 | 39 | */ |
kenjiArai | 0:5b88d5760320 | 40 | |
kenjiArai | 0:5b88d5760320 | 41 | /** The Semaphore class is used to manage and protect access to a set of shared resources. |
kenjiArai | 0:5b88d5760320 | 42 | * |
kenjiArai | 0:5b88d5760320 | 43 | * @note |
kenjiArai | 0:5b88d5760320 | 44 | * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS |
kenjiArai | 0:5b88d5760320 | 45 | * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
kenjiArai | 0:5b88d5760320 | 46 | */ |
kenjiArai | 0:5b88d5760320 | 47 | class Semaphore : private mbed::NonCopyable<Semaphore> { |
kenjiArai | 0:5b88d5760320 | 48 | public: |
kenjiArai | 0:5b88d5760320 | 49 | /** Create and Initialize a Semaphore object used for managing resources. |
kenjiArai | 0:5b88d5760320 | 50 | @param count number of available resources; maximum index value is (count-1). (default: 0). |
kenjiArai | 0:5b88d5760320 | 51 | |
kenjiArai | 0:5b88d5760320 | 52 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 53 | */ |
kenjiArai | 0:5b88d5760320 | 54 | Semaphore(int32_t count = 0); |
kenjiArai | 0:5b88d5760320 | 55 | |
kenjiArai | 0:5b88d5760320 | 56 | /** Create and Initialize a Semaphore object used for managing resources. |
kenjiArai | 0:5b88d5760320 | 57 | @param count number of available resources |
kenjiArai | 0:5b88d5760320 | 58 | @param max_count maximum number of available resources |
kenjiArai | 0:5b88d5760320 | 59 | |
kenjiArai | 0:5b88d5760320 | 60 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 61 | */ |
kenjiArai | 0:5b88d5760320 | 62 | Semaphore(int32_t count, uint16_t max_count); |
kenjiArai | 0:5b88d5760320 | 63 | |
kenjiArai | 0:5b88d5760320 | 64 | /** Wait until a Semaphore resource becomes available. |
kenjiArai | 0:5b88d5760320 | 65 | |
kenjiArai | 0:5b88d5760320 | 66 | @deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions. |
kenjiArai | 0:5b88d5760320 | 67 | |
kenjiArai | 0:5b88d5760320 | 68 | @param millisec timeout value. (default: osWaitForever). |
kenjiArai | 0:5b88d5760320 | 69 | @return number of available tokens, before taking one; or -1 in case of incorrect parameters |
kenjiArai | 0:5b88d5760320 | 70 | |
kenjiArai | 0:5b88d5760320 | 71 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
kenjiArai | 0:5b88d5760320 | 72 | */ |
kenjiArai | 0:5b88d5760320 | 73 | MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions") |
kenjiArai | 0:5b88d5760320 | 74 | int32_t wait(uint32_t millisec = osWaitForever); |
kenjiArai | 0:5b88d5760320 | 75 | |
kenjiArai | 0:5b88d5760320 | 76 | /** Wait until a Semaphore resource becomes available. |
kenjiArai | 0:5b88d5760320 | 77 | |
kenjiArai | 0:5b88d5760320 | 78 | @deprecated Do not use this function. This function has been replaced with try_acquire_until(). |
kenjiArai | 0:5b88d5760320 | 79 | |
kenjiArai | 0:5b88d5760320 | 80 | @param millisec absolute timeout time, referenced to Kernel::get_ms_count() |
kenjiArai | 0:5b88d5760320 | 81 | @return number of available tokens, before taking one; or -1 in case of incorrect parameters |
kenjiArai | 0:5b88d5760320 | 82 | @note the underlying RTOS may have a limit to the maximum wait time |
kenjiArai | 0:5b88d5760320 | 83 | due to internal 32-bit computations, but this is guaranteed to work if the |
kenjiArai | 0:5b88d5760320 | 84 | wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kenjiArai | 0:5b88d5760320 | 85 | the acquire attempt will time out earlier than specified. |
kenjiArai | 0:5b88d5760320 | 86 | |
kenjiArai | 0:5b88d5760320 | 87 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 88 | */ |
kenjiArai | 0:5b88d5760320 | 89 | MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()") |
kenjiArai | 0:5b88d5760320 | 90 | int32_t wait_until(uint64_t millisec); |
kenjiArai | 0:5b88d5760320 | 91 | |
kenjiArai | 0:5b88d5760320 | 92 | /** Wait until a Semaphore resource becomes available. |
kenjiArai | 0:5b88d5760320 | 93 | |
kenjiArai | 0:5b88d5760320 | 94 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 95 | */ |
kenjiArai | 0:5b88d5760320 | 96 | void acquire(); |
kenjiArai | 0:5b88d5760320 | 97 | |
kenjiArai | 0:5b88d5760320 | 98 | /** Try to acquire a Semaphore resource, and return immediately |
kenjiArai | 0:5b88d5760320 | 99 | @return true if a resource was acquired, false otherwise. |
kenjiArai | 0:5b88d5760320 | 100 | @note equivalent to try_acquire_for(0) |
kenjiArai | 0:5b88d5760320 | 101 | |
kenjiArai | 0:5b88d5760320 | 102 | @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 103 | */ |
kenjiArai | 0:5b88d5760320 | 104 | bool try_acquire(); |
kenjiArai | 0:5b88d5760320 | 105 | |
kenjiArai | 0:5b88d5760320 | 106 | /** Wait until a Semaphore resource becomes available. |
kenjiArai | 0:5b88d5760320 | 107 | @param millisec timeout value. |
kenjiArai | 0:5b88d5760320 | 108 | @return true if a resource was acquired, false otherwise. |
kenjiArai | 0:5b88d5760320 | 109 | |
kenjiArai | 0:5b88d5760320 | 110 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
kenjiArai | 0:5b88d5760320 | 111 | */ |
kenjiArai | 0:5b88d5760320 | 112 | bool try_acquire_for(uint32_t millisec); |
kenjiArai | 0:5b88d5760320 | 113 | |
kenjiArai | 0:5b88d5760320 | 114 | /** Wait until a Semaphore resource becomes available. |
kenjiArai | 0:5b88d5760320 | 115 | @param millisec absolute timeout time, referenced to Kernel::get_ms_count() |
kenjiArai | 0:5b88d5760320 | 116 | @return true if a resource was acquired, false otherwise. |
kenjiArai | 0:5b88d5760320 | 117 | @note the underlying RTOS may have a limit to the maximum wait time |
kenjiArai | 0:5b88d5760320 | 118 | due to internal 32-bit computations, but this is guaranteed to work if the |
kenjiArai | 0:5b88d5760320 | 119 | wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kenjiArai | 0:5b88d5760320 | 120 | the acquire attempt will time out earlier than specified. |
kenjiArai | 0:5b88d5760320 | 121 | |
kenjiArai | 0:5b88d5760320 | 122 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 123 | */ |
kenjiArai | 0:5b88d5760320 | 124 | bool try_acquire_until(uint64_t millisec); |
kenjiArai | 0:5b88d5760320 | 125 | |
kenjiArai | 0:5b88d5760320 | 126 | /** Release a Semaphore resource that was obtain with Semaphore::acquire. |
kenjiArai | 0:5b88d5760320 | 127 | @return status code that indicates the execution status of the function: |
kenjiArai | 0:5b88d5760320 | 128 | @a osOK the token has been correctly released. |
kenjiArai | 0:5b88d5760320 | 129 | @a osErrorResource the maximum token count has been reached. |
kenjiArai | 0:5b88d5760320 | 130 | @a osErrorParameter internal error. |
kenjiArai | 0:5b88d5760320 | 131 | |
kenjiArai | 0:5b88d5760320 | 132 | @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 133 | */ |
kenjiArai | 0:5b88d5760320 | 134 | osStatus release(void); |
kenjiArai | 0:5b88d5760320 | 135 | |
kenjiArai | 0:5b88d5760320 | 136 | /** Semaphore destructor |
kenjiArai | 0:5b88d5760320 | 137 | * |
kenjiArai | 0:5b88d5760320 | 138 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 139 | */ |
kenjiArai | 0:5b88d5760320 | 140 | ~Semaphore(); |
kenjiArai | 0:5b88d5760320 | 141 | |
kenjiArai | 0:5b88d5760320 | 142 | private: |
kenjiArai | 0:5b88d5760320 | 143 | void constructor(int32_t count, uint16_t max_count); |
kenjiArai | 0:5b88d5760320 | 144 | |
kenjiArai | 1:9db0e321a9f4 | 145 | #if MBED_CONF_RTOS_PRESENT |
kenjiArai | 0:5b88d5760320 | 146 | int32_t _wait(uint32_t millisec); |
kenjiArai | 0:5b88d5760320 | 147 | |
kenjiArai | 0:5b88d5760320 | 148 | osSemaphoreId_t _id; |
kenjiArai | 0:5b88d5760320 | 149 | mbed_rtos_storage_semaphore_t _obj_mem; |
kenjiArai | 1:9db0e321a9f4 | 150 | #else |
kenjiArai | 1:9db0e321a9f4 | 151 | static bool semaphore_available(void *); |
kenjiArai | 1:9db0e321a9f4 | 152 | int32_t _count; |
kenjiArai | 1:9db0e321a9f4 | 153 | uint16_t _max_count; |
kenjiArai | 1:9db0e321a9f4 | 154 | #endif |
kenjiArai | 0:5b88d5760320 | 155 | }; |
kenjiArai | 0:5b88d5760320 | 156 | /** @}*/ |
kenjiArai | 0:5b88d5760320 | 157 | /** @}*/ |
kenjiArai | 0:5b88d5760320 | 158 | } |
kenjiArai | 0:5b88d5760320 | 159 | #endif |