Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ConditionVariable.cpp Source File

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/Kernel.h"
00024 #include "rtos/Thread.h"
00025 
00026 #include "mbed_error.h"
00027 #include "mbed_assert.h"
00028 
00029 namespace rtos {
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, &current_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, &current_thread);
00062     }
00063 
00064     return timeout;
00065 }
00066 
00067 bool ConditionVariable::wait_until(uint64_t millisec)
00068 {
00069     uint64_t now = Kernel::get_ms_count();
00070 
00071     if (now >= millisec) {
00072         // Time has already passed - standard behaviour is to
00073         // treat as a "try".
00074         return wait_for(0);
00075     } else if (millisec - now >= osWaitForever) {
00076         // Exceeds maximum delay of underlying wait_for -
00077         // spuriously wake after 49 days, indicating no timeout.
00078         wait_for(osWaitForever - 1);
00079         return false;
00080     } else {
00081         return wait_for(millisec - now);
00082     }
00083 }
00084 
00085 void ConditionVariable::notify_one()
00086 {
00087     MBED_ASSERT(_mutex.get_owner() == Thread::gettid());
00088     if (_wait_list != NULL) {
00089         _wait_list->sem.release();
00090         _remove_wait_list(&_wait_list, _wait_list);
00091     }
00092 }
00093 
00094 void ConditionVariable::notify_all()
00095 {
00096     MBED_ASSERT(_mutex.get_owner() == Thread::gettid());
00097     while (_wait_list != NULL) {
00098         _wait_list->sem.release();
00099         _remove_wait_list(&_wait_list, _wait_list);
00100     }
00101 }
00102 
00103 void ConditionVariable::_add_wait_list(Waiter **wait_list, Waiter *waiter)
00104 {
00105     if (NULL == *wait_list) {
00106         // Nothing in the list so add it directly.
00107         // Update prev and next pointer to reference self
00108         *wait_list = waiter;
00109         waiter->next = waiter;
00110         waiter->prev = waiter;
00111     } else {
00112         // Add after the last element
00113         Waiter *first = *wait_list;
00114         Waiter *last = (*wait_list)->prev;
00115 
00116         // Update new entry
00117         waiter->next = first;
00118         waiter->prev = last;
00119 
00120         // Insert into the list
00121         first->prev = waiter;
00122         last->next = waiter;
00123     }
00124     waiter->in_list = true;
00125 }
00126 
00127 void ConditionVariable::_remove_wait_list(Waiter **wait_list, Waiter *waiter)
00128 {
00129     Waiter *prev = waiter->prev;
00130     Waiter *next = waiter->next;
00131 
00132     // Remove from list
00133     prev->next = waiter->next;
00134     next->prev = waiter->prev;
00135     *wait_list = waiter->next;
00136 
00137     if (*wait_list == waiter) {
00138         // This was the last element in the list
00139         *wait_list = NULL;
00140     }
00141 
00142     // Invalidate pointers
00143     waiter->next = NULL;
00144     waiter->prev = NULL;
00145     waiter->in_list = false;
00146 }
00147 
00148 ConditionVariable::~ConditionVariable()
00149 {
00150     MBED_ASSERT(NULL == _wait_list);
00151 }
00152 
00153 }