![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbed_drivers/race_test/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 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved |
kadonotakashi | 0:8fdf9a60065b | 3 | * SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kadonotakashi | 0:8fdf9a60065b | 6 | * not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | * |
kadonotakashi | 0:8fdf9a60065b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kadonotakashi | 0:8fdf9a60065b | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | */ |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "rtos.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "utest/utest.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "SingletonPtr.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include <stdio.h> |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | #ifdef MBED_RTOS_SINGLE_THREAD |
kadonotakashi | 0:8fdf9a60065b | 26 | #error [NOT_SUPPORTED] test not supported for single threaded enviroment |
kadonotakashi | 0:8fdf9a60065b | 27 | #endif |
kadonotakashi | 0:8fdf9a60065b | 28 | |
kadonotakashi | 0:8fdf9a60065b | 29 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 30 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 31 | #endif |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 34 | |
kadonotakashi | 0:8fdf9a60065b | 35 | #define TEST_STACK_SIZE 512 |
kadonotakashi | 0:8fdf9a60065b | 36 | static uint32_t instance_count = 0; |
kadonotakashi | 0:8fdf9a60065b | 37 | |
kadonotakashi | 0:8fdf9a60065b | 38 | class TestClass { |
kadonotakashi | 0:8fdf9a60065b | 39 | public: |
kadonotakashi | 0:8fdf9a60065b | 40 | TestClass() |
kadonotakashi | 0:8fdf9a60065b | 41 | { |
kadonotakashi | 0:8fdf9a60065b | 42 | Thread::wait(500); |
kadonotakashi | 0:8fdf9a60065b | 43 | instance_count++; |
kadonotakashi | 0:8fdf9a60065b | 44 | } |
kadonotakashi | 0:8fdf9a60065b | 45 | |
kadonotakashi | 0:8fdf9a60065b | 46 | void do_something() |
kadonotakashi | 0:8fdf9a60065b | 47 | { |
kadonotakashi | 0:8fdf9a60065b | 48 | Thread::wait(100); |
kadonotakashi | 0:8fdf9a60065b | 49 | } |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | ~TestClass() |
kadonotakashi | 0:8fdf9a60065b | 52 | { |
kadonotakashi | 0:8fdf9a60065b | 53 | instance_count--; |
kadonotakashi | 0:8fdf9a60065b | 54 | } |
kadonotakashi | 0:8fdf9a60065b | 55 | }; |
kadonotakashi | 0:8fdf9a60065b | 56 | |
kadonotakashi | 0:8fdf9a60065b | 57 | static TestClass *get_test_class() |
kadonotakashi | 0:8fdf9a60065b | 58 | { |
kadonotakashi | 0:8fdf9a60065b | 59 | static TestClass tc; |
kadonotakashi | 0:8fdf9a60065b | 60 | return &tc; |
kadonotakashi | 0:8fdf9a60065b | 61 | } |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | static SingletonPtr<TestClass> test_class; |
kadonotakashi | 0:8fdf9a60065b | 64 | |
kadonotakashi | 0:8fdf9a60065b | 65 | static void main_func_race() |
kadonotakashi | 0:8fdf9a60065b | 66 | { |
kadonotakashi | 0:8fdf9a60065b | 67 | get_test_class(); |
kadonotakashi | 0:8fdf9a60065b | 68 | TEST_ASSERT_EQUAL_UINT32(1, instance_count); |
kadonotakashi | 0:8fdf9a60065b | 69 | } |
kadonotakashi | 0:8fdf9a60065b | 70 | |
kadonotakashi | 0:8fdf9a60065b | 71 | static void main_class_race() |
kadonotakashi | 0:8fdf9a60065b | 72 | { |
kadonotakashi | 0:8fdf9a60065b | 73 | test_class->do_something(); |
kadonotakashi | 0:8fdf9a60065b | 74 | TEST_ASSERT_EQUAL_UINT32(1, instance_count); |
kadonotakashi | 0:8fdf9a60065b | 75 | } |
kadonotakashi | 0:8fdf9a60065b | 76 | |
kadonotakashi | 0:8fdf9a60065b | 77 | void test_case_func_race() |
kadonotakashi | 0:8fdf9a60065b | 78 | { |
kadonotakashi | 0:8fdf9a60065b | 79 | Callback<void()> cb(main_func_race); |
kadonotakashi | 0:8fdf9a60065b | 80 | Thread t1(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 81 | Thread t2(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 82 | |
kadonotakashi | 0:8fdf9a60065b | 83 | // Start start first thread |
kadonotakashi | 0:8fdf9a60065b | 84 | t1.start(cb); |
kadonotakashi | 0:8fdf9a60065b | 85 | // Start second thread while the first is inside the constructor |
kadonotakashi | 0:8fdf9a60065b | 86 | Thread::wait(250); |
kadonotakashi | 0:8fdf9a60065b | 87 | t2.start(cb); |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | // Wait for the threads to finish |
kadonotakashi | 0:8fdf9a60065b | 90 | t1.join(); |
kadonotakashi | 0:8fdf9a60065b | 91 | t2.join(); |
kadonotakashi | 0:8fdf9a60065b | 92 | |
kadonotakashi | 0:8fdf9a60065b | 93 | TEST_ASSERT_EQUAL_UINT32(1, instance_count); |
kadonotakashi | 0:8fdf9a60065b | 94 | |
kadonotakashi | 0:8fdf9a60065b | 95 | // Reset instance count |
kadonotakashi | 0:8fdf9a60065b | 96 | instance_count = 0; |
kadonotakashi | 0:8fdf9a60065b | 97 | } |
kadonotakashi | 0:8fdf9a60065b | 98 | |
kadonotakashi | 0:8fdf9a60065b | 99 | void test_case_class_race() |
kadonotakashi | 0:8fdf9a60065b | 100 | { |
kadonotakashi | 0:8fdf9a60065b | 101 | Callback<void()> cb(main_class_race); |
kadonotakashi | 0:8fdf9a60065b | 102 | Thread t1(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 103 | Thread t2(osPriorityNormal, TEST_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 104 | |
kadonotakashi | 0:8fdf9a60065b | 105 | // Start start first thread |
kadonotakashi | 0:8fdf9a60065b | 106 | t1.start(cb); |
kadonotakashi | 0:8fdf9a60065b | 107 | // Start second thread while the first is inside the constructor |
kadonotakashi | 0:8fdf9a60065b | 108 | Thread::wait(250); |
kadonotakashi | 0:8fdf9a60065b | 109 | t2.start(cb); |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | // Wait for the threads to finish |
kadonotakashi | 0:8fdf9a60065b | 112 | t1.join(); |
kadonotakashi | 0:8fdf9a60065b | 113 | t2.join(); |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | TEST_ASSERT_EQUAL_UINT32(1, instance_count); |
kadonotakashi | 0:8fdf9a60065b | 116 | |
kadonotakashi | 0:8fdf9a60065b | 117 | // Reset instance count |
kadonotakashi | 0:8fdf9a60065b | 118 | instance_count = 0; |
kadonotakashi | 0:8fdf9a60065b | 119 | } |
kadonotakashi | 0:8fdf9a60065b | 120 | |
kadonotakashi | 0:8fdf9a60065b | 121 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 122 | Case("function init race", test_case_func_race), |
kadonotakashi | 0:8fdf9a60065b | 123 | Case("class init race", test_case_class_race), |
kadonotakashi | 0:8fdf9a60065b | 124 | }; |
kadonotakashi | 0:8fdf9a60065b | 125 | |
kadonotakashi | 0:8fdf9a60065b | 126 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 127 | { |
kadonotakashi | 0:8fdf9a60065b | 128 | GREENTEA_SETUP(20, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 129 | return greentea_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 130 | } |
kadonotakashi | 0:8fdf9a60065b | 131 | |
kadonotakashi | 0:8fdf9a60065b | 132 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | int main() |
kadonotakashi | 0:8fdf9a60065b | 135 | { |
kadonotakashi | 0:8fdf9a60065b | 136 | Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 137 | } |