RTC auf true
rtos/ThisThread.h@2:7aab896b1a3b, 2019-03-13 (annotated)
- Committer:
- kevman
- Date:
- Wed Mar 13 11:03:24 2019 +0000
- Revision:
- 2:7aab896b1a3b
- Parent:
- 0:38ceb79fef03
2019-03-13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* mbed Microcontroller Library |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2006-2018 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kevman | 0:38ceb79fef03 | 5 | * of this software and associated documentation files (the "Software"), to deal |
kevman | 0:38ceb79fef03 | 6 | * in the Software without restriction, including without limitation the rights |
kevman | 0:38ceb79fef03 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kevman | 0:38ceb79fef03 | 8 | * copies of the Software, and to permit persons to whom the Software is |
kevman | 0:38ceb79fef03 | 9 | * furnished to do so, subject to the following conditions: |
kevman | 0:38ceb79fef03 | 10 | * |
kevman | 0:38ceb79fef03 | 11 | * The above copyright notice and this permission notice shall be included in |
kevman | 0:38ceb79fef03 | 12 | * all copies or substantial portions of the Software. |
kevman | 0:38ceb79fef03 | 13 | * |
kevman | 0:38ceb79fef03 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kevman | 0:38ceb79fef03 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kevman | 0:38ceb79fef03 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kevman | 0:38ceb79fef03 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kevman | 0:38ceb79fef03 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kevman | 0:38ceb79fef03 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kevman | 0:38ceb79fef03 | 20 | * SOFTWARE. |
kevman | 0:38ceb79fef03 | 21 | */ |
kevman | 0:38ceb79fef03 | 22 | #ifndef THIS_THREAD_H |
kevman | 0:38ceb79fef03 | 23 | #define THIS_THREAD_H |
kevman | 0:38ceb79fef03 | 24 | |
kevman | 0:38ceb79fef03 | 25 | #include <stdint.h> |
kevman | 0:38ceb79fef03 | 26 | #include "cmsis_os2.h" |
kevman | 0:38ceb79fef03 | 27 | #include "mbed_rtos1_types.h" |
kevman | 0:38ceb79fef03 | 28 | #include "mbed_rtos_storage.h" |
kevman | 0:38ceb79fef03 | 29 | #include "platform/Callback.h" |
kevman | 0:38ceb79fef03 | 30 | #include "platform/mbed_toolchain.h" |
kevman | 0:38ceb79fef03 | 31 | #include "platform/NonCopyable.h" |
kevman | 0:38ceb79fef03 | 32 | #include "rtos/Semaphore.h" |
kevman | 0:38ceb79fef03 | 33 | #include "rtos/Mutex.h" |
kevman | 0:38ceb79fef03 | 34 | |
kevman | 0:38ceb79fef03 | 35 | namespace rtos { |
kevman | 0:38ceb79fef03 | 36 | /** \addtogroup rtos */ |
kevman | 0:38ceb79fef03 | 37 | /** @{*/ |
kevman | 0:38ceb79fef03 | 38 | /** |
kevman | 0:38ceb79fef03 | 39 | * \defgroup rtos_ThisThread ThisThread namespace |
kevman | 0:38ceb79fef03 | 40 | * @{ |
kevman | 0:38ceb79fef03 | 41 | */ |
kevman | 0:38ceb79fef03 | 42 | |
kevman | 0:38ceb79fef03 | 43 | /** The ThisThread namespace allows controlling the current thread. |
kevman | 0:38ceb79fef03 | 44 | * |
kevman | 0:38ceb79fef03 | 45 | * Example: |
kevman | 0:38ceb79fef03 | 46 | * @code |
kevman | 0:38ceb79fef03 | 47 | * #include "mbed.h" |
kevman | 0:38ceb79fef03 | 48 | * #include "rtos.h" |
kevman | 0:38ceb79fef03 | 49 | * |
kevman | 0:38ceb79fef03 | 50 | * Thread thread; |
kevman | 0:38ceb79fef03 | 51 | * DigitalOut led1(LED1); |
kevman | 0:38ceb79fef03 | 52 | * |
kevman | 0:38ceb79fef03 | 53 | * #define STOP_FLAG 1 |
kevman | 0:38ceb79fef03 | 54 | * |
kevman | 0:38ceb79fef03 | 55 | * // Blink function toggles the led in a long running loop |
kevman | 0:38ceb79fef03 | 56 | * void blink(DigitalOut *led) { |
kevman | 0:38ceb79fef03 | 57 | * while (!ThisThread::flags_wait_any_for(STOP_FLAG, 1000)) { |
kevman | 0:38ceb79fef03 | 58 | * *led = !*led; |
kevman | 0:38ceb79fef03 | 59 | * } |
kevman | 0:38ceb79fef03 | 60 | * } |
kevman | 0:38ceb79fef03 | 61 | * |
kevman | 0:38ceb79fef03 | 62 | * // Spawns a thread to run blink for 5 seconds |
kevman | 0:38ceb79fef03 | 63 | * int main() { |
kevman | 0:38ceb79fef03 | 64 | * thread.start(callback(blink, &led1)); |
kevman | 0:38ceb79fef03 | 65 | * ThisThread::sleep_for(5000); |
kevman | 0:38ceb79fef03 | 66 | * thread.signal_set(STOP_FLAG); |
kevman | 0:38ceb79fef03 | 67 | * thread.join(); |
kevman | 0:38ceb79fef03 | 68 | * } |
kevman | 0:38ceb79fef03 | 69 | * @endcode |
kevman | 0:38ceb79fef03 | 70 | * |
kevman | 0:38ceb79fef03 | 71 | */ |
kevman | 0:38ceb79fef03 | 72 | namespace ThisThread { |
kevman | 0:38ceb79fef03 | 73 | /** Clears the specified Thread Flags of the currently running thread. |
kevman | 0:38ceb79fef03 | 74 | @param flags specifies the flags of the thread that should be cleared. |
kevman | 0:38ceb79fef03 | 75 | @return thread flags before clearing. |
kevman | 0:38ceb79fef03 | 76 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 77 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 78 | */ |
kevman | 0:38ceb79fef03 | 79 | uint32_t flags_clear(uint32_t flags); |
kevman | 0:38ceb79fef03 | 80 | |
kevman | 0:38ceb79fef03 | 81 | /** Returns the Thread Flags currently set for the currently running thread. |
kevman | 0:38ceb79fef03 | 82 | @return current thread flags or 0 if not in a valid thread. |
kevman | 0:38ceb79fef03 | 83 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 84 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 85 | */ |
kevman | 0:38ceb79fef03 | 86 | uint32_t flags_get(); |
kevman | 0:38ceb79fef03 | 87 | |
kevman | 0:38ceb79fef03 | 88 | /** Wait for all of the specified Thread Flags to become signaled for the current thread. |
kevman | 0:38ceb79fef03 | 89 | @param flags specifies the flags to wait for |
kevman | 0:38ceb79fef03 | 90 | @param clear whether to clear the specified flags after waiting for them. (default: true) |
kevman | 0:38ceb79fef03 | 91 | @return actual thread flags before clearing, which will satisfy the wait |
kevman | 0:38ceb79fef03 | 92 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 93 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 94 | */ |
kevman | 0:38ceb79fef03 | 95 | uint32_t flags_wait_all(uint32_t flags, bool clear = true); |
kevman | 0:38ceb79fef03 | 96 | |
kevman | 0:38ceb79fef03 | 97 | /** Wait for any of the specified Thread Flags to become signaled for the current thread. |
kevman | 0:38ceb79fef03 | 98 | @param flags specifies the flags to wait for |
kevman | 0:38ceb79fef03 | 99 | @param clear whether to clear the specified flags after waiting for them. (default: true) |
kevman | 0:38ceb79fef03 | 100 | @return actual thread flags before clearing, which will satisfy the wait |
kevman | 0:38ceb79fef03 | 101 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 102 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 103 | */ |
kevman | 0:38ceb79fef03 | 104 | uint32_t flags_wait_any(uint32_t flags, bool clear = true); |
kevman | 0:38ceb79fef03 | 105 | |
kevman | 0:38ceb79fef03 | 106 | /** Wait for all of the specified Thread Flags to become signaled for the current thread. |
kevman | 0:38ceb79fef03 | 107 | @param flags specifies the flags to wait for |
kevman | 0:38ceb79fef03 | 108 | @param millisec timeout value or 0 in case of no time-out. |
kevman | 0:38ceb79fef03 | 109 | @param clear whether to clear the specified flags after waiting for them. (default: true) |
kevman | 0:38ceb79fef03 | 110 | @return actual thread flags before clearing, which may not satisfy the wait |
kevman | 0:38ceb79fef03 | 111 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 112 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 113 | */ |
kevman | 0:38ceb79fef03 | 114 | uint32_t flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear = true); |
kevman | 0:38ceb79fef03 | 115 | |
kevman | 0:38ceb79fef03 | 116 | /** Wait for all of the specified Thread Flags to become signaled for the current thread. |
kevman | 0:38ceb79fef03 | 117 | @param flags specifies the flags to wait for |
kevman | 0:38ceb79fef03 | 118 | @param millisec absolute timeout time, referenced to Kernel::get_ms_count() |
kevman | 0:38ceb79fef03 | 119 | @param clear whether to clear the specified flags after waiting for them. (default: true) |
kevman | 0:38ceb79fef03 | 120 | @return actual thread flags before clearing, which may not satisfy the wait |
kevman | 0:38ceb79fef03 | 121 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 122 | @note the underlying RTOS may have a limit to the maximum wait time |
kevman | 0:38ceb79fef03 | 123 | due to internal 32-bit computations, but this is guaranteed to work if the |
kevman | 0:38ceb79fef03 | 124 | wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kevman | 0:38ceb79fef03 | 125 | the wait will time out earlier than specified. |
kevman | 0:38ceb79fef03 | 126 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 127 | */ |
kevman | 0:38ceb79fef03 | 128 | uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear = true); |
kevman | 0:38ceb79fef03 | 129 | |
kevman | 0:38ceb79fef03 | 130 | /** Wait for any of the specified Thread Flags to become signaled for the current thread. |
kevman | 0:38ceb79fef03 | 131 | @param flags specifies the flags to wait for |
kevman | 0:38ceb79fef03 | 132 | @param millisec timeout value or 0 in case of no time-out. |
kevman | 0:38ceb79fef03 | 133 | @param clear whether to clear the specified flags after waiting for them. (default: true) |
kevman | 0:38ceb79fef03 | 134 | @return actual thread flags before clearing, which may not satisfy the wait |
kevman | 0:38ceb79fef03 | 135 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 136 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 137 | */ |
kevman | 0:38ceb79fef03 | 138 | uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear = true); |
kevman | 0:38ceb79fef03 | 139 | |
kevman | 0:38ceb79fef03 | 140 | /** Wait for any of the specified Thread Flags to become signaled for the current thread. |
kevman | 0:38ceb79fef03 | 141 | @param flags specifies the flags to wait for |
kevman | 0:38ceb79fef03 | 142 | @param millisec absolute timeout time, referenced to Kernel::get_ms_count() |
kevman | 0:38ceb79fef03 | 143 | @param clear whether to clear the specified flags after waiting for them. (default: true) |
kevman | 0:38ceb79fef03 | 144 | @return actual thread flags before clearing, which may not satisfy the wait |
kevman | 0:38ceb79fef03 | 145 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 146 | @note the underlying RTOS may have a limit to the maximum wait time |
kevman | 0:38ceb79fef03 | 147 | due to internal 32-bit computations, but this is guaranteed to work if the |
kevman | 0:38ceb79fef03 | 148 | wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kevman | 0:38ceb79fef03 | 149 | the wait will time out earlier than specified. |
kevman | 0:38ceb79fef03 | 150 | @see Thread::flags_set |
kevman | 0:38ceb79fef03 | 151 | */ |
kevman | 0:38ceb79fef03 | 152 | uint32_t flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear = true); |
kevman | 0:38ceb79fef03 | 153 | |
kevman | 0:38ceb79fef03 | 154 | /** Sleep for a specified time period in millisec: |
kevman | 0:38ceb79fef03 | 155 | @param millisec time delay value |
kevman | 0:38ceb79fef03 | 156 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 157 | */ |
kevman | 0:38ceb79fef03 | 158 | void sleep_for(uint32_t millisec); |
kevman | 0:38ceb79fef03 | 159 | |
kevman | 0:38ceb79fef03 | 160 | /** Sleep until a specified time in millisec |
kevman | 0:38ceb79fef03 | 161 | The specified time is according to Kernel::get_ms_count(). |
kevman | 0:38ceb79fef03 | 162 | @param millisec absolute time in millisec |
kevman | 0:38ceb79fef03 | 163 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 164 | @note if millisec is equal to or lower than the current tick count, this |
kevman | 0:38ceb79fef03 | 165 | returns immediately. |
kevman | 0:38ceb79fef03 | 166 | */ |
kevman | 0:38ceb79fef03 | 167 | void sleep_until(uint64_t millisec); |
kevman | 0:38ceb79fef03 | 168 | |
kevman | 0:38ceb79fef03 | 169 | /** Pass control to next equal-priority thread that is in state READY. |
kevman | 0:38ceb79fef03 | 170 | (Higher-priority READY threads would prevent us from running; this |
kevman | 0:38ceb79fef03 | 171 | will not enable lower-priority threads to run, as we remain READY). |
kevman | 0:38ceb79fef03 | 172 | @note You cannot call this function from ISR context. |
kevman | 0:38ceb79fef03 | 173 | */ |
kevman | 0:38ceb79fef03 | 174 | void yield(); |
kevman | 0:38ceb79fef03 | 175 | |
kevman | 0:38ceb79fef03 | 176 | /** Get the thread id of the current running thread. |
kevman | 0:38ceb79fef03 | 177 | @return thread ID for reference by other functions or NULL in case of error or in ISR context. |
kevman | 0:38ceb79fef03 | 178 | @note You may call this function from ISR context. |
kevman | 0:38ceb79fef03 | 179 | */ |
kevman | 0:38ceb79fef03 | 180 | osThreadId_t get_id(); |
kevman | 0:38ceb79fef03 | 181 | |
kevman | 0:38ceb79fef03 | 182 | }; |
kevman | 0:38ceb79fef03 | 183 | /** @}*/ |
kevman | 0:38ceb79fef03 | 184 | /** @}*/ |
kevman | 0:38ceb79fef03 | 185 | } |
kevman | 0:38ceb79fef03 | 186 | #endif |
kevman | 0:38ceb79fef03 | 187 | |
kevman | 0:38ceb79fef03 | 188 |