2017-01-26 08:59:12 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2017-2017 ARM Limited
|
2020-02-20 08:45:04 +00:00
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2017-01-26 08:59:12 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "rtos/ConditionVariable.h"
|
2017-11-10 13:01:51 +00:00
|
|
|
#include "rtos/Kernel.h"
|
2018-10-09 11:41:36 +00:00
|
|
|
#include "rtos/ThisThread.h"
|
2017-01-26 08:59:12 +00:00
|
|
|
|
2019-07-11 15:23:39 +00:00
|
|
|
#include "platform/mbed_error.h"
|
|
|
|
#include "platform/mbed_assert.h"
|
2017-01-26 08:59:12 +00:00
|
|
|
|
2019-03-14 11:36:02 +00:00
|
|
|
#if MBED_CONF_RTOS_PRESENT
|
|
|
|
|
2017-01-26 08:59:12 +00:00
|
|
|
namespace rtos {
|
|
|
|
|
2019-07-04 12:37:47 +00:00
|
|
|
ConditionVariable::Waiter::Waiter(): sem(0), prev(nullptr), next(nullptr), in_list(false)
|
2017-01-26 08:59:12 +00:00
|
|
|
{
|
|
|
|
// No initialization to do
|
|
|
|
}
|
|
|
|
|
2019-07-04 12:37:47 +00:00
|
|
|
ConditionVariable::ConditionVariable(Mutex &mutex): _mutex(mutex), _wait_list(nullptr)
|
2017-01-26 08:59:12 +00:00
|
|
|
{
|
|
|
|
// No initialization to do
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConditionVariable::wait()
|
|
|
|
{
|
|
|
|
wait_for(osWaitForever);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConditionVariable::wait_for(uint32_t millisec)
|
|
|
|
{
|
|
|
|
Waiter current_thread;
|
2018-10-09 11:41:36 +00:00
|
|
|
MBED_ASSERT(_mutex.get_owner() == ThisThread::get_id());
|
2017-01-26 08:59:12 +00:00
|
|
|
MBED_ASSERT(_mutex._count == 1);
|
2017-11-15 22:15:27 +00:00
|
|
|
_add_wait_list(&_wait_list, ¤t_thread);
|
2017-01-26 08:59:12 +00:00
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
|
2019-03-18 10:21:51 +00:00
|
|
|
bool timeout = !current_thread.sem.try_acquire_for(millisec);
|
2017-01-26 08:59:12 +00:00
|
|
|
|
|
|
|
_mutex.lock();
|
|
|
|
|
|
|
|
if (current_thread.in_list) {
|
2017-11-15 22:15:27 +00:00
|
|
|
_remove_wait_list(&_wait_list, ¤t_thread);
|
2017-01-26 08:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2017-11-10 13:01:51 +00:00
|
|
|
bool ConditionVariable::wait_until(uint64_t millisec)
|
|
|
|
{
|
|
|
|
uint64_t now = Kernel::get_ms_count();
|
|
|
|
|
|
|
|
if (now >= millisec) {
|
|
|
|
// Time has already passed - standard behaviour is to
|
|
|
|
// treat as a "try".
|
|
|
|
return wait_for(0);
|
|
|
|
} else if (millisec - now >= osWaitForever) {
|
|
|
|
// Exceeds maximum delay of underlying wait_for -
|
|
|
|
// spuriously wake after 49 days, indicating no timeout.
|
|
|
|
wait_for(osWaitForever - 1);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return wait_for(millisec - now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 08:59:12 +00:00
|
|
|
void ConditionVariable::notify_one()
|
|
|
|
{
|
2018-10-09 11:41:36 +00:00
|
|
|
MBED_ASSERT(_mutex.get_owner() == ThisThread::get_id());
|
2019-07-04 12:37:47 +00:00
|
|
|
if (_wait_list != nullptr) {
|
2017-01-26 08:59:12 +00:00
|
|
|
_wait_list->sem.release();
|
2017-11-15 22:15:27 +00:00
|
|
|
_remove_wait_list(&_wait_list, _wait_list);
|
2017-01-26 08:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConditionVariable::notify_all()
|
|
|
|
{
|
2018-10-09 11:41:36 +00:00
|
|
|
MBED_ASSERT(_mutex.get_owner() == ThisThread::get_id());
|
2019-07-04 12:37:47 +00:00
|
|
|
while (_wait_list != nullptr) {
|
2017-01-26 08:59:12 +00:00
|
|
|
_wait_list->sem.release();
|
2017-11-15 22:15:27 +00:00
|
|
|
_remove_wait_list(&_wait_list, _wait_list);
|
2017-01-26 08:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:15:27 +00:00
|
|
|
void ConditionVariable::_add_wait_list(Waiter **wait_list, Waiter *waiter)
|
2017-01-26 08:59:12 +00:00
|
|
|
{
|
2019-07-04 12:37:47 +00:00
|
|
|
if (nullptr == *wait_list) {
|
2017-01-26 08:59:12 +00:00
|
|
|
// Nothing in the list so add it directly.
|
2017-11-15 22:15:27 +00:00
|
|
|
// Update prev and next pointer to reference self
|
|
|
|
*wait_list = waiter;
|
|
|
|
waiter->next = waiter;
|
2017-01-26 08:59:12 +00:00
|
|
|
waiter->prev = waiter;
|
|
|
|
} else {
|
|
|
|
// Add after the last element
|
2017-11-15 22:15:27 +00:00
|
|
|
Waiter *first = *wait_list;
|
|
|
|
Waiter *last = (*wait_list)->prev;
|
|
|
|
|
|
|
|
// Update new entry
|
|
|
|
waiter->next = first;
|
2017-01-26 08:59:12 +00:00
|
|
|
waiter->prev = last;
|
2017-11-15 22:15:27 +00:00
|
|
|
|
|
|
|
// Insert into the list
|
|
|
|
first->prev = waiter;
|
|
|
|
last->next = waiter;
|
2017-01-26 08:59:12 +00:00
|
|
|
}
|
|
|
|
waiter->in_list = true;
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:15:27 +00:00
|
|
|
void ConditionVariable::_remove_wait_list(Waiter **wait_list, Waiter *waiter)
|
2017-01-26 08:59:12 +00:00
|
|
|
{
|
2017-11-15 22:15:27 +00:00
|
|
|
Waiter *prev = waiter->prev;
|
|
|
|
Waiter *next = waiter->next;
|
|
|
|
|
|
|
|
// Remove from list
|
|
|
|
prev->next = waiter->next;
|
|
|
|
next->prev = waiter->prev;
|
|
|
|
*wait_list = waiter->next;
|
|
|
|
|
|
|
|
if (*wait_list == waiter) {
|
|
|
|
// This was the last element in the list
|
2019-07-04 12:37:47 +00:00
|
|
|
*wait_list = nullptr;
|
2017-01-26 08:59:12 +00:00
|
|
|
}
|
2017-11-15 22:15:27 +00:00
|
|
|
|
|
|
|
// Invalidate pointers
|
2019-07-04 12:37:47 +00:00
|
|
|
waiter->next = nullptr;
|
|
|
|
waiter->prev = nullptr;
|
2017-01-26 08:59:12 +00:00
|
|
|
waiter->in_list = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConditionVariable::~ConditionVariable()
|
|
|
|
{
|
2019-07-04 12:37:47 +00:00
|
|
|
MBED_ASSERT(nullptr == _wait_list);
|
2017-01-26 08:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-14 11:36:02 +00:00
|
|
|
|
|
|
|
#endif
|