Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
ConditionVariable.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017-2017 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #include "rtos/ConditionVariable.h" 00023 #include "rtos/Thread.h" 00024 00025 #include "mbed_error.h" 00026 #include "mbed_assert.h" 00027 00028 namespace rtos { 00029 00030 00031 ConditionVariable::Waiter::Waiter(): sem(0), prev(NULL), next(NULL), in_list(false) 00032 { 00033 // No initialization to do 00034 } 00035 00036 ConditionVariable::ConditionVariable(Mutex &mutex): _mutex(mutex), _wait_list(NULL) 00037 { 00038 // No initialization to do 00039 } 00040 00041 void ConditionVariable::wait() 00042 { 00043 wait_for(osWaitForever); 00044 } 00045 00046 bool ConditionVariable::wait_for(uint32_t millisec) 00047 { 00048 Waiter current_thread; 00049 MBED_ASSERT(_mutex.get_owner() == Thread::gettid()); 00050 MBED_ASSERT(_mutex._count == 1); 00051 _add_wait_list(&_wait_list, ¤t_thread); 00052 00053 _mutex.unlock(); 00054 00055 int32_t sem_count = current_thread.sem.wait(millisec); 00056 bool timeout = (sem_count > 0) ? false : true; 00057 00058 _mutex.lock(); 00059 00060 if (current_thread.in_list) { 00061 _remove_wait_list(&_wait_list, ¤t_thread); 00062 } 00063 00064 return timeout; 00065 } 00066 00067 void ConditionVariable::notify_one() 00068 { 00069 MBED_ASSERT(_mutex.get_owner() == Thread::gettid()); 00070 if (_wait_list != NULL) { 00071 _wait_list->sem.release(); 00072 _remove_wait_list(&_wait_list, _wait_list); 00073 } 00074 } 00075 00076 void ConditionVariable::notify_all() 00077 { 00078 MBED_ASSERT(_mutex.get_owner() == Thread::gettid()); 00079 while (_wait_list != NULL) { 00080 _wait_list->sem.release(); 00081 _remove_wait_list(&_wait_list, _wait_list); 00082 } 00083 } 00084 00085 void ConditionVariable::_add_wait_list(Waiter **wait_list, Waiter *waiter) 00086 { 00087 if (NULL == *wait_list) { 00088 // Nothing in the list so add it directly. 00089 // Update prev and next pointer to reference self 00090 *wait_list = waiter; 00091 waiter->next = waiter; 00092 waiter->prev = waiter; 00093 } else { 00094 // Add after the last element 00095 Waiter *first = *wait_list; 00096 Waiter *last = (*wait_list)->prev; 00097 00098 // Update new entry 00099 waiter->next = first; 00100 waiter->prev = last; 00101 00102 // Insert into the list 00103 first->prev = waiter; 00104 last->next = waiter; 00105 } 00106 waiter->in_list = true; 00107 } 00108 00109 void ConditionVariable::_remove_wait_list(Waiter **wait_list, Waiter *waiter) 00110 { 00111 Waiter *prev = waiter->prev; 00112 Waiter *next = waiter->next; 00113 00114 // Remove from list 00115 prev->next = waiter->next; 00116 next->prev = waiter->prev; 00117 *wait_list = waiter->next; 00118 00119 if (*wait_list == waiter) { 00120 // This was the last element in the list 00121 *wait_list = NULL; 00122 } 00123 00124 // Invalidate pointers 00125 waiter->next = NULL; 00126 waiter->prev = NULL; 00127 waiter->in_list = false; 00128 } 00129 00130 ConditionVariable::~ConditionVariable() 00131 { 00132 MBED_ASSERT(NULL == _wait_list); 00133 } 00134 00135 }
Generated on Sun Jul 17 2022 08:25:21 by 1.7.2