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