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