Skip to content

Add ConditionVariable to mbed rtos #3648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions TESTS/mbedmicro-rtos-mbed/condition_variable/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "rtos.h"

#if defined(MBED_RTOS_SINGLE_THREAD)
#error [NOT_SUPPORTED] test not supported
#endif

using namespace utest::v1;

#define TEST_STACK_SIZE 512
#define TEST_DELAY 10

static int change_counter = 0;
static Mutex mutex;
static ConditionVariable cond(mutex);

void increment_on_signal()
{
mutex.lock();

cond.wait();
change_counter++;

mutex.unlock();
}

void test_notify_one()
{
Thread t1(osPriorityNormal, TEST_STACK_SIZE);
Thread t2(osPriorityNormal, TEST_STACK_SIZE);

change_counter = 0;
t1.start(increment_on_signal);
t2.start(increment_on_signal);

wait_ms(TEST_DELAY);
TEST_ASSERT_EQUAL(0, change_counter);

mutex.lock();
cond.notify_one();
mutex.unlock();

wait_ms(TEST_DELAY);
TEST_ASSERT_EQUAL(1, change_counter);

mutex.lock();
cond.notify_one();
mutex.unlock();

t1.join();
t2.join();
}

void test_notify_all()
{
Thread t1(osPriorityNormal, TEST_STACK_SIZE);
Thread t2(osPriorityNormal, TEST_STACK_SIZE);

change_counter = 0;
t1.start(increment_on_signal);
t2.start(increment_on_signal);

wait_ms(TEST_DELAY);
TEST_ASSERT_EQUAL(0, change_counter);

mutex.lock();
cond.notify_all();
mutex.unlock();

wait_ms(TEST_DELAY);
TEST_ASSERT_EQUAL(2, change_counter);

t1.join();
t2.join();
}

utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Test notify one", test_notify_one),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't we agree on having tests documented (this test unit would contain a header file with documentation) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for the HAL specification which will be transcluded into our docs. CC @bulislaw @pan-

Case("Test notify all", test_notify_all),
};

Specification specification(test_setup, cases);

int main()
{
return !Harness::run(specification);
}
136 changes: 136 additions & 0 deletions rtos/ConditionVariable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* mbed Microcontroller Library
* Copyright (c) 2017-2017 ARM Limited
*
* 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"
#include "rtos/Semaphore.h"
#include "rtos/Thread.h"

#include "mbed_error.h"
#include "mbed_assert.h"

namespace rtos {

#define RESUME_SIGNAL (1 << 15)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to avoid the reservation of a signal ID or ensure user code won't use this signal ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMSIS-RTOS2 gives event flags, could use those.


struct Waiter {
Waiter();
Semaphore sem;
Waiter *prev;
Waiter *next;
bool in_list;
};

Waiter::Waiter(): sem(0), prev(NULL), next(NULL), in_list(false)
{
// No initialization to do
}

ConditionVariable::ConditionVariable(Mutex &mutex): _mutex(mutex), _wait_list(NULL)
{
// No initialization to do
}

void ConditionVariable::wait()
{
wait_for(osWaitForever);
}

bool ConditionVariable::wait_for(uint32_t millisec)
{
Waiter current_thread;
MBED_ASSERT(_mutex.get_owner() == Thread::gettid());
MBED_ASSERT(_mutex._count == 1);
_add_wait_list(&current_thread);

_mutex.unlock();

int32_t sem_count = current_thread.sem.wait(millisec);
bool timeout = (sem_count > 0) ? false : true;

_mutex.lock();

if (current_thread.in_list) {
_remove_wait_list(&current_thread);
}

return timeout;
}

void ConditionVariable::notify_one()
{
MBED_ASSERT(_mutex.get_owner() == Thread::gettid());
if (_wait_list != NULL) {
_wait_list->sem.release();
_remove_wait_list(_wait_list);
}
}

void ConditionVariable::notify_all()
{
MBED_ASSERT(_mutex.get_owner() == Thread::gettid());
while (_wait_list != NULL) {
_wait_list->sem.release();
_remove_wait_list(_wait_list);
}
}

void ConditionVariable::_add_wait_list(Waiter * waiter)
{
if (NULL == _wait_list) {
// Nothing in the list so add it directly.
// Update prev pointer to reference self
_wait_list = waiter;
waiter->prev = waiter;
} else {
// Add after the last element
Waiter *last = _wait_list->prev;
last->next = waiter;
waiter->prev = last;
_wait_list->prev = waiter;
}
waiter->in_list = true;
}

void ConditionVariable::_remove_wait_list(Waiter * waiter)
{
// Remove this element from the start of the list
Waiter * next = waiter->next;
if (waiter == _wait_list) {
_wait_list = next;
}
if (next != NULL) {
next = waiter->prev;
}
Waiter * prev = waiter->prev;
if (prev != NULL) {
prev = waiter->next;
}
waiter->next = NULL;
waiter->prev = NULL;
waiter->in_list = false;
}

ConditionVariable::~ConditionVariable()
{
MBED_ASSERT(NULL == _wait_list);
}

}
Loading