Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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