takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/rtos/ConditionVariable.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2017-2017 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kadonotakashi | 0:8fdf9a60065b | 5 | * of this software and associated documentation files (the "Software"), to deal |
kadonotakashi | 0:8fdf9a60065b | 6 | * in the Software without restriction, including without limitation the rights |
kadonotakashi | 0:8fdf9a60065b | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kadonotakashi | 0:8fdf9a60065b | 8 | * copies of the Software, and to permit persons to whom the Software is |
kadonotakashi | 0:8fdf9a60065b | 9 | * furnished to do so, subject to the following conditions: |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * The above copyright notice and this permission notice shall be included in |
kadonotakashi | 0:8fdf9a60065b | 12 | * all copies or substantial portions of the Software. |
kadonotakashi | 0:8fdf9a60065b | 13 | * |
kadonotakashi | 0:8fdf9a60065b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kadonotakashi | 0:8fdf9a60065b | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kadonotakashi | 0:8fdf9a60065b | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kadonotakashi | 0:8fdf9a60065b | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kadonotakashi | 0:8fdf9a60065b | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kadonotakashi | 0:8fdf9a60065b | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kadonotakashi | 0:8fdf9a60065b | 20 | * SOFTWARE. |
kadonotakashi | 0:8fdf9a60065b | 21 | */ |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "rtos/ConditionVariable.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "rtos/Kernel.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "rtos/Thread.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | #include "mbed_error.h" |
kadonotakashi | 0:8fdf9a60065b | 27 | #include "mbed_assert.h" |
kadonotakashi | 0:8fdf9a60065b | 28 | |
kadonotakashi | 0:8fdf9a60065b | 29 | namespace rtos { |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | ConditionVariable::Waiter::Waiter(): sem(0), prev(NULL), next(NULL), in_list(false) |
kadonotakashi | 0:8fdf9a60065b | 32 | { |
kadonotakashi | 0:8fdf9a60065b | 33 | // No initialization to do |
kadonotakashi | 0:8fdf9a60065b | 34 | } |
kadonotakashi | 0:8fdf9a60065b | 35 | |
kadonotakashi | 0:8fdf9a60065b | 36 | ConditionVariable::ConditionVariable(Mutex &mutex): _mutex(mutex), _wait_list(NULL) |
kadonotakashi | 0:8fdf9a60065b | 37 | { |
kadonotakashi | 0:8fdf9a60065b | 38 | // No initialization to do |
kadonotakashi | 0:8fdf9a60065b | 39 | } |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | void ConditionVariable::wait() |
kadonotakashi | 0:8fdf9a60065b | 42 | { |
kadonotakashi | 0:8fdf9a60065b | 43 | wait_for(osWaitForever); |
kadonotakashi | 0:8fdf9a60065b | 44 | } |
kadonotakashi | 0:8fdf9a60065b | 45 | |
kadonotakashi | 0:8fdf9a60065b | 46 | bool ConditionVariable::wait_for(uint32_t millisec) |
kadonotakashi | 0:8fdf9a60065b | 47 | { |
kadonotakashi | 0:8fdf9a60065b | 48 | Waiter current_thread; |
kadonotakashi | 0:8fdf9a60065b | 49 | MBED_ASSERT(_mutex.get_owner() == Thread::gettid()); |
kadonotakashi | 0:8fdf9a60065b | 50 | MBED_ASSERT(_mutex._count == 1); |
kadonotakashi | 0:8fdf9a60065b | 51 | _add_wait_list(&_wait_list, ¤t_thread); |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | _mutex.unlock(); |
kadonotakashi | 0:8fdf9a60065b | 54 | |
kadonotakashi | 0:8fdf9a60065b | 55 | int32_t sem_count = current_thread.sem.wait(millisec); |
kadonotakashi | 0:8fdf9a60065b | 56 | bool timeout = (sem_count > 0) ? false : true; |
kadonotakashi | 0:8fdf9a60065b | 57 | |
kadonotakashi | 0:8fdf9a60065b | 58 | _mutex.lock(); |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | if (current_thread.in_list) { |
kadonotakashi | 0:8fdf9a60065b | 61 | _remove_wait_list(&_wait_list, ¤t_thread); |
kadonotakashi | 0:8fdf9a60065b | 62 | } |
kadonotakashi | 0:8fdf9a60065b | 63 | |
kadonotakashi | 0:8fdf9a60065b | 64 | return timeout; |
kadonotakashi | 0:8fdf9a60065b | 65 | } |
kadonotakashi | 0:8fdf9a60065b | 66 | |
kadonotakashi | 0:8fdf9a60065b | 67 | bool ConditionVariable::wait_until(uint64_t millisec) |
kadonotakashi | 0:8fdf9a60065b | 68 | { |
kadonotakashi | 0:8fdf9a60065b | 69 | uint64_t now = Kernel::get_ms_count(); |
kadonotakashi | 0:8fdf9a60065b | 70 | |
kadonotakashi | 0:8fdf9a60065b | 71 | if (now >= millisec) { |
kadonotakashi | 0:8fdf9a60065b | 72 | // Time has already passed - standard behaviour is to |
kadonotakashi | 0:8fdf9a60065b | 73 | // treat as a "try". |
kadonotakashi | 0:8fdf9a60065b | 74 | return wait_for(0); |
kadonotakashi | 0:8fdf9a60065b | 75 | } else if (millisec - now >= osWaitForever) { |
kadonotakashi | 0:8fdf9a60065b | 76 | // Exceeds maximum delay of underlying wait_for - |
kadonotakashi | 0:8fdf9a60065b | 77 | // spuriously wake after 49 days, indicating no timeout. |
kadonotakashi | 0:8fdf9a60065b | 78 | wait_for(osWaitForever - 1); |
kadonotakashi | 0:8fdf9a60065b | 79 | return false; |
kadonotakashi | 0:8fdf9a60065b | 80 | } else { |
kadonotakashi | 0:8fdf9a60065b | 81 | return wait_for(millisec - now); |
kadonotakashi | 0:8fdf9a60065b | 82 | } |
kadonotakashi | 0:8fdf9a60065b | 83 | } |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | void ConditionVariable::notify_one() |
kadonotakashi | 0:8fdf9a60065b | 86 | { |
kadonotakashi | 0:8fdf9a60065b | 87 | MBED_ASSERT(_mutex.get_owner() == Thread::gettid()); |
kadonotakashi | 0:8fdf9a60065b | 88 | if (_wait_list != NULL) { |
kadonotakashi | 0:8fdf9a60065b | 89 | _wait_list->sem.release(); |
kadonotakashi | 0:8fdf9a60065b | 90 | _remove_wait_list(&_wait_list, _wait_list); |
kadonotakashi | 0:8fdf9a60065b | 91 | } |
kadonotakashi | 0:8fdf9a60065b | 92 | } |
kadonotakashi | 0:8fdf9a60065b | 93 | |
kadonotakashi | 0:8fdf9a60065b | 94 | void ConditionVariable::notify_all() |
kadonotakashi | 0:8fdf9a60065b | 95 | { |
kadonotakashi | 0:8fdf9a60065b | 96 | MBED_ASSERT(_mutex.get_owner() == Thread::gettid()); |
kadonotakashi | 0:8fdf9a60065b | 97 | while (_wait_list != NULL) { |
kadonotakashi | 0:8fdf9a60065b | 98 | _wait_list->sem.release(); |
kadonotakashi | 0:8fdf9a60065b | 99 | _remove_wait_list(&_wait_list, _wait_list); |
kadonotakashi | 0:8fdf9a60065b | 100 | } |
kadonotakashi | 0:8fdf9a60065b | 101 | } |
kadonotakashi | 0:8fdf9a60065b | 102 | |
kadonotakashi | 0:8fdf9a60065b | 103 | void ConditionVariable::_add_wait_list(Waiter **wait_list, Waiter *waiter) |
kadonotakashi | 0:8fdf9a60065b | 104 | { |
kadonotakashi | 0:8fdf9a60065b | 105 | if (NULL == *wait_list) { |
kadonotakashi | 0:8fdf9a60065b | 106 | // Nothing in the list so add it directly. |
kadonotakashi | 0:8fdf9a60065b | 107 | // Update prev and next pointer to reference self |
kadonotakashi | 0:8fdf9a60065b | 108 | *wait_list = waiter; |
kadonotakashi | 0:8fdf9a60065b | 109 | waiter->next = waiter; |
kadonotakashi | 0:8fdf9a60065b | 110 | waiter->prev = waiter; |
kadonotakashi | 0:8fdf9a60065b | 111 | } else { |
kadonotakashi | 0:8fdf9a60065b | 112 | // Add after the last element |
kadonotakashi | 0:8fdf9a60065b | 113 | Waiter *first = *wait_list; |
kadonotakashi | 0:8fdf9a60065b | 114 | Waiter *last = (*wait_list)->prev; |
kadonotakashi | 0:8fdf9a60065b | 115 | |
kadonotakashi | 0:8fdf9a60065b | 116 | // Update new entry |
kadonotakashi | 0:8fdf9a60065b | 117 | waiter->next = first; |
kadonotakashi | 0:8fdf9a60065b | 118 | waiter->prev = last; |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | // Insert into the list |
kadonotakashi | 0:8fdf9a60065b | 121 | first->prev = waiter; |
kadonotakashi | 0:8fdf9a60065b | 122 | last->next = waiter; |
kadonotakashi | 0:8fdf9a60065b | 123 | } |
kadonotakashi | 0:8fdf9a60065b | 124 | waiter->in_list = true; |
kadonotakashi | 0:8fdf9a60065b | 125 | } |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | void ConditionVariable::_remove_wait_list(Waiter **wait_list, Waiter *waiter) |
kadonotakashi | 0:8fdf9a60065b | 128 | { |
kadonotakashi | 0:8fdf9a60065b | 129 | Waiter *prev = waiter->prev; |
kadonotakashi | 0:8fdf9a60065b | 130 | Waiter *next = waiter->next; |
kadonotakashi | 0:8fdf9a60065b | 131 | |
kadonotakashi | 0:8fdf9a60065b | 132 | // Remove from list |
kadonotakashi | 0:8fdf9a60065b | 133 | prev->next = waiter->next; |
kadonotakashi | 0:8fdf9a60065b | 134 | next->prev = waiter->prev; |
kadonotakashi | 0:8fdf9a60065b | 135 | *wait_list = waiter->next; |
kadonotakashi | 0:8fdf9a60065b | 136 | |
kadonotakashi | 0:8fdf9a60065b | 137 | if (*wait_list == waiter) { |
kadonotakashi | 0:8fdf9a60065b | 138 | // This was the last element in the list |
kadonotakashi | 0:8fdf9a60065b | 139 | *wait_list = NULL; |
kadonotakashi | 0:8fdf9a60065b | 140 | } |
kadonotakashi | 0:8fdf9a60065b | 141 | |
kadonotakashi | 0:8fdf9a60065b | 142 | // Invalidate pointers |
kadonotakashi | 0:8fdf9a60065b | 143 | waiter->next = NULL; |
kadonotakashi | 0:8fdf9a60065b | 144 | waiter->prev = NULL; |
kadonotakashi | 0:8fdf9a60065b | 145 | waiter->in_list = false; |
kadonotakashi | 0:8fdf9a60065b | 146 | } |
kadonotakashi | 0:8fdf9a60065b | 147 | |
kadonotakashi | 0:8fdf9a60065b | 148 | ConditionVariable::~ConditionVariable() |
kadonotakashi | 0:8fdf9a60065b | 149 | { |
kadonotakashi | 0:8fdf9a60065b | 150 | MBED_ASSERT(NULL == _wait_list); |
kadonotakashi | 0:8fdf9a60065b | 151 | } |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | } |