Skip to content

RTOS : added empty and full functions to Mail and Queue #5158

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 4 commits into from
Nov 2, 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
41 changes: 40 additions & 1 deletion TESTS/mbedmicro-rtos-mbed/mail/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,43 @@ void test_calloc()
TEST_ASSERT_EQUAL(0, *mail);
}

/** Test mail empty

Given a mail of uint32_t data
before data is inserted the mail should be empty
after data is inserted the mail shouldn't be empty
*/
void test_mail_empty()
{
Mail<mail_t, 1> m;

mail_t *mail = m.alloc();

TEST_ASSERT_EQUAL(true, m.empty());

m.put(mail);

TEST_ASSERT_EQUAL(false, m.empty());
}

/** Test mail empty

Given a mail of uint32_t data with size of 1
before data is inserted the mail shouldn't be full
after data is inserted the mail should be full
*/
void test_mail_full()
{
Mail<mail_t, 1> m;

mail_t *mail = m.alloc();

TEST_ASSERT_EQUAL(false, m.full());

m.put(mail);

TEST_ASSERT_EQUAL(true, m.full());
}

utest::v1::status_t test_setup(const size_t number_of_cases)
{
Expand All @@ -475,7 +512,9 @@ Case cases[] = {
Case("Test invalid message free", test_free_wrong),
Case("Test message send/receive single thread and order", test_single_thread_order),
Case("Test message send/receive multi-thread and per thread order", test_multi_thread_order),
Case("Test message send/receive multi-thread, multi-Mail and per thread order", test_multi_thread_multi_mail_order)
Case("Test message send/receive multi-thread, multi-Mail and per thread order", test_multi_thread_multi_mail_order),
Case("Test mail empty", test_mail_empty),
Case("Test mail full", test_mail_full)
};

Specification specification(test_setup, cases);
Expand Down
38 changes: 37 additions & 1 deletion TESTS/mbedmicro-rtos-mbed/queue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,40 @@ void test_msg_prio()
TEST_ASSERT_EQUAL(TEST_UINT_MSG, evt.value.v);
}

/** Test queue empty

Given a queue of uint32_t data
before data is inserted the queue should be empty
after data is inserted the queue shouldn't be empty
*/
void test_queue_empty()
{
Queue<uint32_t, 1> q;

TEST_ASSERT_EQUAL(true, q.empty());

q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT, 1);

TEST_ASSERT_EQUAL(false, q.empty());
}

/** Test queue empty

Given a queue of uint32_t data with size of 1
before data is inserted the queue shouldn't be full
after data is inserted the queue should be full
*/
void test_queue_full()
{
Queue<uint32_t, 1> q;

TEST_ASSERT_EQUAL(false, q.full());

q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT, 1);

TEST_ASSERT_EQUAL(true, q.full());
}

utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(5, "default_auto");
Expand All @@ -295,7 +329,9 @@ Case cases[] = {
Case("Test put full timeout", test_put_full_timeout),
Case("Test put full wait forever", test_put_full_waitforever),
Case("Test message ordering", test_msg_order),
Case("Test message priority", test_msg_prio)
Case("Test message priority", test_msg_prio),
Case("Test queue empty", test_queue_empty),
Case("Test queue full", test_queue_full)
};

Specification specification(test_setup, cases);
Expand Down
16 changes: 16 additions & 0 deletions rtos/Mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
/** Create and Initialise Mail queue. */
Mail() { };

/** Check if the mail queue is empty
*
* @return True if the mail queue is empty, false if not
*/
bool empty() const {
return _queue.empty();
}

/** Check if the mail queue is full
*
* @return True if the mail queue is full, false if not
*/
bool full() const {
return _queue.full();
}

/** Allocate a memory block of type T
@param millisec timeout value or 0 in case of no time-out. (default: 0).
@return pointer to memory block that can be filled with mail or NULL in case error.
Expand Down
16 changes: 16 additions & 0 deletions rtos/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
osMessageQueueDelete(_id);
}

/** Check if the queue is empty
*
* @return True if the queue is empty, false if not
*/
bool empty() const {
return osMessageQueueGetCount(_id) == 0;
}

/** Check if the queue is full
*
* @return True if the queue is full, false if not
*/
bool full() const {
return osMessageQueueGetSpace(_id) == 0;
}

/** Put a message in a Queue.
@param data message pointer.
@param millisec timeout value or 0 in case of no time-out. (default: 0)
Expand Down