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