Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "greentea-client/test_env.h"
00018 #include "unity.h"
00019 #include "utest.h"
00020 #include "rtos.h"
00021 
00022 #if defined(MBED_RTOS_SINGLE_THREAD)
00023   #error [NOT_SUPPORTED] test not supported
00024 #endif
00025 
00026 using namespace utest::v1;
00027 
00028 #define TEST_STACK_SIZE 512
00029 #define TEST_DELAY 10
00030 
00031 static int change_counter = 0;
00032 static Mutex mutex;
00033 static ConditionVariable cond(mutex);
00034 
00035 void increment_on_signal()
00036 {
00037     mutex.lock();
00038 
00039     cond.wait();
00040     change_counter++;
00041 
00042     mutex.unlock();
00043 }
00044 
00045 void test_notify_one()
00046 {
00047     Thread t1(osPriorityNormal, TEST_STACK_SIZE);
00048     Thread t2(osPriorityNormal, TEST_STACK_SIZE);
00049 
00050     change_counter = 0;
00051     t1.start(increment_on_signal);
00052     t2.start(increment_on_signal);
00053 
00054     wait_ms(TEST_DELAY);
00055     TEST_ASSERT_EQUAL(0, change_counter);
00056 
00057     mutex.lock();
00058     cond.notify_one();
00059     mutex.unlock();
00060 
00061     wait_ms(TEST_DELAY);
00062     TEST_ASSERT_EQUAL(1, change_counter);
00063 
00064     mutex.lock();
00065     cond.notify_one();
00066     mutex.unlock();
00067 
00068     t1.join();
00069     t2.join();
00070 }
00071 
00072 void test_notify_all()
00073 {
00074     Thread t1(osPriorityNormal, TEST_STACK_SIZE);
00075     Thread t2(osPriorityNormal, TEST_STACK_SIZE);
00076 
00077     change_counter = 0;
00078     t1.start(increment_on_signal);
00079     t2.start(increment_on_signal);
00080 
00081     wait_ms(TEST_DELAY);
00082     TEST_ASSERT_EQUAL(0, change_counter);
00083 
00084     mutex.lock();
00085     cond.notify_all();
00086     mutex.unlock();
00087 
00088     wait_ms(TEST_DELAY);
00089     TEST_ASSERT_EQUAL(2, change_counter);
00090 
00091     t1.join();
00092     t2.join();
00093 }
00094 
00095 
00096 class TestConditionVariable : public ConditionVariable {
00097 
00098 public:
00099     static void test_linked_list(void)
00100     {
00101         Waiter *list = NULL;
00102         Waiter w1;
00103         Waiter w2;
00104         Waiter w3;
00105         Waiter w4;
00106 
00107         TEST_ASSERT_EQUAL(0, validate_and_get_size(&list));
00108 
00109         // Add 4 nodes
00110         _add_wait_list(&list, &w1);
00111         TEST_ASSERT_EQUAL(1, validate_and_get_size(&list));
00112         _add_wait_list(&list, &w2);
00113         TEST_ASSERT_EQUAL(2, validate_and_get_size(&list));
00114         _add_wait_list(&list, &w3);
00115         TEST_ASSERT_EQUAL(3, validate_and_get_size(&list));
00116         _add_wait_list(&list, &w4);
00117         TEST_ASSERT_EQUAL(4, validate_and_get_size(&list));
00118 
00119         // Remove a middle node
00120         _remove_wait_list(&list, &w2);
00121         TEST_ASSERT_EQUAL(3, validate_and_get_size(&list));
00122 
00123         // Remove front node
00124         _remove_wait_list(&list, &w1);
00125         TEST_ASSERT_EQUAL(2, validate_and_get_size(&list));
00126 
00127         // remove back node
00128         _remove_wait_list(&list, &w4);
00129         TEST_ASSERT_EQUAL(1, validate_and_get_size(&list));
00130 
00131         // remove last node
00132         _remove_wait_list(&list, &w3);
00133         TEST_ASSERT_EQUAL(0, validate_and_get_size(&list));
00134 
00135         TEST_ASSERT_EQUAL_PTR(NULL, list);
00136     }
00137 
00138     /**
00139      * Validate the linked list an return the number of elements
00140      *
00141      * If this list is invalid then this function asserts and does not
00142      * return.
00143      *
00144      * Every node in a valid linked list has the properties:
00145      * 1. node->prev->next == node
00146      * 2. node->next->prev == node
00147      */
00148     static int validate_and_get_size(Waiter **list)
00149     {
00150         Waiter *first = *list;
00151         if (NULL == first) {
00152             // List is empty
00153             return 0;
00154         }
00155 
00156         int size = 0;
00157         Waiter *current = first;
00158         do {
00159             TEST_ASSERT_EQUAL_PTR(current, current->prev->next);
00160             TEST_ASSERT_EQUAL_PTR(current, current->next->prev);
00161             current = current->next;
00162             size++;
00163         } while (current != first);
00164         return size;
00165     }
00166 
00167 };
00168 
00169 utest::v1::status_t test_setup(const size_t number_of_cases)
00170 {
00171     GREENTEA_SETUP(10, "default_auto");
00172     return verbose_test_setup_handler(number_of_cases);
00173 }
00174 
00175 Case cases[] = {
00176     Case("Test notify one", test_notify_one),
00177     Case("Test notify all", test_notify_all),
00178     Case("Test linked list", TestConditionVariable::test_linked_list),
00179 };
00180 
00181 Specification specification(test_setup, cases);
00182 
00183 int main()
00184 {
00185     return !Harness::run(specification);
00186 }