init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 #ifdef MBED_RTOS_SINGLE_THREAD
00026   #error [NOT_SUPPORTED] test not supported for single threaded enviroment
00027 #endif
00028 
00029 using namespace utest::v1;
00030 
00031 #define TEST_STACK_SIZE     512
00032 static uint32_t instance_count = 0;
00033 
00034 class TestClass {
00035 public:
00036     TestClass() {
00037         Thread::wait(500);
00038         instance_count++;
00039     }
00040 
00041     void do_something() {
00042         Thread::wait(100);
00043     }
00044 
00045     ~TestClass() {
00046         instance_count--;
00047     }
00048 };
00049 
00050 static TestClass* get_test_class()
00051 {
00052     static TestClass tc;
00053     return &tc;
00054 }
00055 
00056 static SingletonPtr<TestClass> test_class;
00057 
00058 static void main_func_race()
00059 {
00060     get_test_class();
00061     TEST_ASSERT_EQUAL_UINT32(1, instance_count);
00062 }
00063 
00064 static void main_class_race()
00065 {
00066     test_class->do_something();
00067     TEST_ASSERT_EQUAL_UINT32(1, instance_count);
00068 }
00069 
00070 void test_case_func_race()
00071 {
00072     Callback<void()> cb(main_func_race);
00073     Thread t1(osPriorityNormal, TEST_STACK_SIZE);
00074     Thread t2(osPriorityNormal, TEST_STACK_SIZE);
00075 
00076     // Start start first thread
00077     t1.start(cb);
00078     // Start second thread while the first is inside the constructor
00079     Thread::wait(250);
00080     t2.start(cb);
00081 
00082     // Wait for the threads to finish
00083     t1.join();
00084     t2.join();
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     Callback<void()> cb(main_class_race);
00095     Thread t1(osPriorityNormal, TEST_STACK_SIZE);
00096     Thread t2(osPriorityNormal, TEST_STACK_SIZE);
00097 
00098     // Start start first thread
00099     t1.start(cb);
00100     // Start second thread while the first is inside the constructor
00101     Thread::wait(250);
00102     t2.start(cb);
00103 
00104     // Wait for the threads to finish
00105     t1.join();
00106     t2.join();
00107 
00108     TEST_ASSERT_EQUAL_UINT32(1, instance_count);
00109 
00110     // Reset instance count
00111     instance_count = 0;
00112 }
00113 
00114 Case cases[] = {
00115     Case("function init race", test_case_func_race),
00116     Case("class init race", test_case_class_race),
00117 };
00118 
00119 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
00120 {
00121     GREENTEA_SETUP(20, "default_auto");
00122     return greentea_test_setup_handler(number_of_cases);
00123 }
00124 
00125 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00126 
00127 int main()
00128 {
00129     Harness::run(specification);
00130 }