Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "mbed.h" 00018 #include "rtos.h" 00019 #include "greentea-client/test_env.h" 00020 #include "unity/unity.h" 00021 #include "utest/utest.h" 00022 #include "SingletonPtr.h" 00023 #include <stdio.h> 00024 00025 using namespace utest::v1; 00026 00027 #define TEST_STACK_SIZE 1024 00028 static uint32_t instance_count = 0; 00029 00030 class TestClass { 00031 public: 00032 TestClass() { 00033 printf("TestClass ctor start\r\n"); 00034 Thread::wait(500); 00035 instance_count++; 00036 printf("TestClass ctor end\r\n"); 00037 } 00038 00039 void do_something() { 00040 printf("Do something called\r\n"); 00041 } 00042 00043 ~TestClass() { 00044 instance_count--; 00045 } 00046 }; 00047 00048 static TestClass* get_test_class() 00049 { 00050 static TestClass tc; 00051 return &tc; 00052 } 00053 00054 static SingletonPtr<TestClass> test_class; 00055 00056 static void main_func_race() 00057 { 00058 get_test_class(); 00059 } 00060 00061 static void main_class_race() 00062 { 00063 test_class->do_something(); 00064 } 00065 00066 void test_case_func_race() 00067 { 00068 printf("Running function race test\r\n"); 00069 Callback<void()> cb(main_func_race); 00070 Thread *t1 = new Thread(osPriorityNormal, TEST_STACK_SIZE); 00071 Thread *t2 = new Thread(osPriorityNormal, TEST_STACK_SIZE); 00072 00073 // Start start first thread 00074 t1->start(cb); 00075 // Start second thread while the first is inside the constructor 00076 Thread::wait(250); 00077 t2->start(cb); 00078 00079 // Wait for the threads to finish 00080 t1->join(); 00081 t2->join(); 00082 00083 delete t1; 00084 delete t2; 00085 00086 TEST_ASSERT_EQUAL_UINT32(1, instance_count); 00087 00088 // Reset instance count 00089 instance_count = 0; 00090 } 00091 00092 void test_case_class_race() 00093 { 00094 printf("Running class race test\r\n"); 00095 Callback<void()> cb(main_class_race); 00096 Thread *t1 = new Thread(osPriorityNormal, TEST_STACK_SIZE); 00097 Thread *t2 = new Thread(osPriorityNormal, TEST_STACK_SIZE); 00098 00099 // Start start first thread 00100 t1->start(cb); 00101 // Start second thread while the first is inside the constructor 00102 Thread::wait(250); 00103 t2->start(cb); 00104 00105 // Wait for the threads to finish 00106 t1->join(); 00107 t2->join(); 00108 00109 delete t1; 00110 delete t2; 00111 00112 TEST_ASSERT_EQUAL_UINT32(1, instance_count); 00113 00114 // Reset instance count 00115 instance_count = 0; 00116 } 00117 00118 Case cases[] = { 00119 Case("function init race", test_case_func_race), 00120 Case("class init race", test_case_class_race), 00121 }; 00122 00123 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) 00124 { 00125 GREENTEA_SETUP(20, "default_auto"); 00126 return greentea_test_setup_handler(number_of_cases); 00127 } 00128 00129 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00130 00131 int main() 00132 { 00133 Harness::run(specification); 00134 }
Generated on Tue Jul 12 2022 17:34:48 by
1.7.2