Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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