![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbedmicro-rtos-mbed/condition_variable/main.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2017 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "unity.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "rtos.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | #if defined(MBED_RTOS_SINGLE_THREAD) |
kadonotakashi | 0:8fdf9a60065b | 23 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 24 | #endif |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 27 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 28 | #endif |
kadonotakashi | 0:8fdf9a60065b | 29 | |
kadonotakashi | 0:8fdf9a60065b | 30 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | #define TEST_STACK_SIZE 512 |
kadonotakashi | 0:8fdf9a60065b | 33 | #define TEST_DELAY 10 |
kadonotakashi | 0:8fdf9a60065b | 34 | |
kadonotakashi | 0:8fdf9a60065b | 35 | static int change_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 36 | static Mutex mutex; |
kadonotakashi | 0:8fdf9a60065b | 37 | static ConditionVariable cond(mutex); |
kadonotakashi | 0:8fdf9a60065b | 38 | |
kadonotakashi | 0:8fdf9a60065b | 39 | void increment_on_signal() |
kadonotakashi | 0:8fdf9a60065b | 40 | { |
kadonotakashi | 0:8fdf9a60065b | 41 | mutex.lock(); |
kadonotakashi | 0:8fdf9a60065b | 42 | |
kadonotakashi | 0:8fdf9a60065b | 43 | cond.wait(); |
kadonotakashi | 0:8fdf9a60065b | 44 | change_counter++; |
kadonotakashi | 0:8fdf9a60065b | 45 | |
kadonotakashi | 0:8fdf9a60065b | 46 | mutex.unlock(); |
kadonotakashi | 0:8fdf9a60065b | 47 | } |
kadonotakashi | 0:8fdf9a60065b | 48 | |
kadonotakashi | 0:8fdf9a60065b | 49 | void test_notify_one() |
kadonotakashi | 0:8fdf9a60065b | 50 | { |
kadonotakashi | 0:8fdf9a60065b | 51 | Thread t1(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 52 | Thread t2(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 53 | |
kadonotakashi | 0:8fdf9a60065b | 54 | change_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 55 | t1.start(increment_on_signal); |
kadonotakashi | 0:8fdf9a60065b | 56 | t2.start(increment_on_signal); |
kadonotakashi | 0:8fdf9a60065b | 57 | |
kadonotakashi | 0:8fdf9a60065b | 58 | wait_ms(TEST_DELAY); |
kadonotakashi | 0:8fdf9a60065b | 59 | TEST_ASSERT_EQUAL(0, change_counter); |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | mutex.lock(); |
kadonotakashi | 0:8fdf9a60065b | 62 | cond.notify_one(); |
kadonotakashi | 0:8fdf9a60065b | 63 | mutex.unlock(); |
kadonotakashi | 0:8fdf9a60065b | 64 | |
kadonotakashi | 0:8fdf9a60065b | 65 | wait_ms(TEST_DELAY); |
kadonotakashi | 0:8fdf9a60065b | 66 | TEST_ASSERT_EQUAL(1, change_counter); |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | mutex.lock(); |
kadonotakashi | 0:8fdf9a60065b | 69 | cond.notify_one(); |
kadonotakashi | 0:8fdf9a60065b | 70 | mutex.unlock(); |
kadonotakashi | 0:8fdf9a60065b | 71 | |
kadonotakashi | 0:8fdf9a60065b | 72 | t1.join(); |
kadonotakashi | 0:8fdf9a60065b | 73 | t2.join(); |
kadonotakashi | 0:8fdf9a60065b | 74 | } |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | void test_notify_all() |
kadonotakashi | 0:8fdf9a60065b | 77 | { |
kadonotakashi | 0:8fdf9a60065b | 78 | Thread t1(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 79 | Thread t2(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 80 | |
kadonotakashi | 0:8fdf9a60065b | 81 | change_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 82 | t1.start(increment_on_signal); |
kadonotakashi | 0:8fdf9a60065b | 83 | t2.start(increment_on_signal); |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | wait_ms(TEST_DELAY); |
kadonotakashi | 0:8fdf9a60065b | 86 | TEST_ASSERT_EQUAL(0, change_counter); |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | mutex.lock(); |
kadonotakashi | 0:8fdf9a60065b | 89 | cond.notify_all(); |
kadonotakashi | 0:8fdf9a60065b | 90 | mutex.unlock(); |
kadonotakashi | 0:8fdf9a60065b | 91 | |
kadonotakashi | 0:8fdf9a60065b | 92 | wait_ms(TEST_DELAY); |
kadonotakashi | 0:8fdf9a60065b | 93 | TEST_ASSERT_EQUAL(2, change_counter); |
kadonotakashi | 0:8fdf9a60065b | 94 | |
kadonotakashi | 0:8fdf9a60065b | 95 | t1.join(); |
kadonotakashi | 0:8fdf9a60065b | 96 | t2.join(); |
kadonotakashi | 0:8fdf9a60065b | 97 | } |
kadonotakashi | 0:8fdf9a60065b | 98 | |
kadonotakashi | 0:8fdf9a60065b | 99 | |
kadonotakashi | 0:8fdf9a60065b | 100 | class TestConditionVariable : public ConditionVariable { |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | public: |
kadonotakashi | 0:8fdf9a60065b | 103 | static void test_linked_list(void) |
kadonotakashi | 0:8fdf9a60065b | 104 | { |
kadonotakashi | 0:8fdf9a60065b | 105 | Waiter *list = NULL; |
kadonotakashi | 0:8fdf9a60065b | 106 | Waiter w1; |
kadonotakashi | 0:8fdf9a60065b | 107 | Waiter w2; |
kadonotakashi | 0:8fdf9a60065b | 108 | Waiter w3; |
kadonotakashi | 0:8fdf9a60065b | 109 | Waiter w4; |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | TEST_ASSERT_EQUAL(0, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | // Add 4 nodes |
kadonotakashi | 0:8fdf9a60065b | 114 | _add_wait_list(&list, &w1); |
kadonotakashi | 0:8fdf9a60065b | 115 | TEST_ASSERT_EQUAL(1, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 116 | _add_wait_list(&list, &w2); |
kadonotakashi | 0:8fdf9a60065b | 117 | TEST_ASSERT_EQUAL(2, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 118 | _add_wait_list(&list, &w3); |
kadonotakashi | 0:8fdf9a60065b | 119 | TEST_ASSERT_EQUAL(3, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 120 | _add_wait_list(&list, &w4); |
kadonotakashi | 0:8fdf9a60065b | 121 | TEST_ASSERT_EQUAL(4, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 122 | |
kadonotakashi | 0:8fdf9a60065b | 123 | // Remove a middle node |
kadonotakashi | 0:8fdf9a60065b | 124 | _remove_wait_list(&list, &w2); |
kadonotakashi | 0:8fdf9a60065b | 125 | TEST_ASSERT_EQUAL(3, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | // Remove front node |
kadonotakashi | 0:8fdf9a60065b | 128 | _remove_wait_list(&list, &w1); |
kadonotakashi | 0:8fdf9a60065b | 129 | TEST_ASSERT_EQUAL(2, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 130 | |
kadonotakashi | 0:8fdf9a60065b | 131 | // remove back node |
kadonotakashi | 0:8fdf9a60065b | 132 | _remove_wait_list(&list, &w4); |
kadonotakashi | 0:8fdf9a60065b | 133 | TEST_ASSERT_EQUAL(1, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | // remove last node |
kadonotakashi | 0:8fdf9a60065b | 136 | _remove_wait_list(&list, &w3); |
kadonotakashi | 0:8fdf9a60065b | 137 | TEST_ASSERT_EQUAL(0, validate_and_get_size(&list)); |
kadonotakashi | 0:8fdf9a60065b | 138 | |
kadonotakashi | 0:8fdf9a60065b | 139 | TEST_ASSERT_EQUAL_PTR(NULL, list); |
kadonotakashi | 0:8fdf9a60065b | 140 | } |
kadonotakashi | 0:8fdf9a60065b | 141 | |
kadonotakashi | 0:8fdf9a60065b | 142 | /** |
kadonotakashi | 0:8fdf9a60065b | 143 | * Validate the linked list an return the number of elements |
kadonotakashi | 0:8fdf9a60065b | 144 | * |
kadonotakashi | 0:8fdf9a60065b | 145 | * If this list is invalid then this function asserts and does not |
kadonotakashi | 0:8fdf9a60065b | 146 | * return. |
kadonotakashi | 0:8fdf9a60065b | 147 | * |
kadonotakashi | 0:8fdf9a60065b | 148 | * Every node in a valid linked list has the properties: |
kadonotakashi | 0:8fdf9a60065b | 149 | * 1. node->prev->next == node |
kadonotakashi | 0:8fdf9a60065b | 150 | * 2. node->next->prev == node |
kadonotakashi | 0:8fdf9a60065b | 151 | */ |
kadonotakashi | 0:8fdf9a60065b | 152 | static int validate_and_get_size(Waiter **list) |
kadonotakashi | 0:8fdf9a60065b | 153 | { |
kadonotakashi | 0:8fdf9a60065b | 154 | Waiter *first = *list; |
kadonotakashi | 0:8fdf9a60065b | 155 | if (NULL == first) { |
kadonotakashi | 0:8fdf9a60065b | 156 | // List is empty |
kadonotakashi | 0:8fdf9a60065b | 157 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 158 | } |
kadonotakashi | 0:8fdf9a60065b | 159 | |
kadonotakashi | 0:8fdf9a60065b | 160 | int size = 0; |
kadonotakashi | 0:8fdf9a60065b | 161 | Waiter *current = first; |
kadonotakashi | 0:8fdf9a60065b | 162 | do { |
kadonotakashi | 0:8fdf9a60065b | 163 | TEST_ASSERT_EQUAL_PTR(current, current->prev->next); |
kadonotakashi | 0:8fdf9a60065b | 164 | TEST_ASSERT_EQUAL_PTR(current, current->next->prev); |
kadonotakashi | 0:8fdf9a60065b | 165 | current = current->next; |
kadonotakashi | 0:8fdf9a60065b | 166 | size++; |
kadonotakashi | 0:8fdf9a60065b | 167 | } while (current != first); |
kadonotakashi | 0:8fdf9a60065b | 168 | return size; |
kadonotakashi | 0:8fdf9a60065b | 169 | } |
kadonotakashi | 0:8fdf9a60065b | 170 | |
kadonotakashi | 0:8fdf9a60065b | 171 | }; |
kadonotakashi | 0:8fdf9a60065b | 172 | |
kadonotakashi | 0:8fdf9a60065b | 173 | utest::v1::status_t test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 174 | { |
kadonotakashi | 0:8fdf9a60065b | 175 | GREENTEA_SETUP(10, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 176 | return verbose_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 177 | } |
kadonotakashi | 0:8fdf9a60065b | 178 | |
kadonotakashi | 0:8fdf9a60065b | 179 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 180 | Case("Test notify one", test_notify_one), |
kadonotakashi | 0:8fdf9a60065b | 181 | Case("Test notify all", test_notify_all), |
kadonotakashi | 0:8fdf9a60065b | 182 | Case("Test linked list", TestConditionVariable::test_linked_list), |
kadonotakashi | 0:8fdf9a60065b | 183 | }; |
kadonotakashi | 0:8fdf9a60065b | 184 | |
kadonotakashi | 0:8fdf9a60065b | 185 | Specification specification(test_setup, cases); |
kadonotakashi | 0:8fdf9a60065b | 186 | |
kadonotakashi | 0:8fdf9a60065b | 187 | int main() |
kadonotakashi | 0:8fdf9a60065b | 188 | { |
kadonotakashi | 0:8fdf9a60065b | 189 | return !Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 190 | } |