2016-08-26 20:53:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-2016, ARM Limited, All Rights Reserved
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include "mbed.h"
|
|
|
|
#include "rtos.h"
|
|
|
|
#include "greentea-client/test_env.h"
|
|
|
|
#include "unity/unity.h"
|
|
|
|
#include "utest/utest.h"
|
|
|
|
#include "SingletonPtr.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-05-25 13:47:28 +00:00
|
|
|
#ifdef MBED_RTOS_SINGLE_THREAD
|
2018-07-27 09:17:07 +00:00
|
|
|
#error [NOT_SUPPORTED] test not supported for single threaded enviroment
|
2017-04-20 09:53:01 +00:00
|
|
|
#endif
|
|
|
|
|
2018-01-09 07:46:40 +00:00
|
|
|
#if !DEVICE_USTICKER
|
|
|
|
#error [NOT_SUPPORTED] test not supported
|
|
|
|
#endif
|
|
|
|
|
2016-08-26 20:53:43 +00:00
|
|
|
using namespace utest::v1;
|
|
|
|
|
2017-05-14 23:55:19 +00:00
|
|
|
#define TEST_STACK_SIZE 512
|
2016-08-26 20:53:43 +00:00
|
|
|
static uint32_t instance_count = 0;
|
|
|
|
|
|
|
|
class TestClass {
|
|
|
|
public:
|
2018-07-27 09:17:07 +00:00
|
|
|
TestClass()
|
|
|
|
{
|
2018-10-09 11:41:36 +00:00
|
|
|
ThisThread::sleep_for(500);
|
2016-08-26 20:53:43 +00:00
|
|
|
instance_count++;
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
void do_something()
|
|
|
|
{
|
2018-10-09 11:41:36 +00:00
|
|
|
ThisThread::sleep_for(100);
|
2016-08-26 20:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
~TestClass()
|
|
|
|
{
|
2016-08-26 20:53:43 +00:00
|
|
|
instance_count--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
static TestClass *get_test_class()
|
2016-08-26 20:53:43 +00:00
|
|
|
{
|
|
|
|
static TestClass tc;
|
|
|
|
return &tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SingletonPtr<TestClass> test_class;
|
|
|
|
|
|
|
|
static void main_func_race()
|
|
|
|
{
|
|
|
|
get_test_class();
|
2017-12-01 16:28:14 +00:00
|
|
|
TEST_ASSERT_EQUAL_UINT32(1, instance_count);
|
2016-08-26 20:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void main_class_race()
|
|
|
|
{
|
|
|
|
test_class->do_something();
|
2017-12-01 16:28:14 +00:00
|
|
|
TEST_ASSERT_EQUAL_UINT32(1, instance_count);
|
2016-08-26 20:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_case_func_race()
|
|
|
|
{
|
|
|
|
Callback<void()> cb(main_func_race);
|
2017-10-25 07:40:05 +00:00
|
|
|
Thread t1(osPriorityNormal, TEST_STACK_SIZE);
|
|
|
|
Thread t2(osPriorityNormal, TEST_STACK_SIZE);
|
2016-08-26 20:53:43 +00:00
|
|
|
|
|
|
|
// Start start first thread
|
2017-10-25 07:40:05 +00:00
|
|
|
t1.start(cb);
|
2016-08-26 20:53:43 +00:00
|
|
|
// Start second thread while the first is inside the constructor
|
2018-10-09 11:41:36 +00:00
|
|
|
ThisThread::sleep_for(250);
|
2017-10-25 07:40:05 +00:00
|
|
|
t2.start(cb);
|
2016-08-26 20:53:43 +00:00
|
|
|
|
|
|
|
// Wait for the threads to finish
|
2017-10-25 07:40:05 +00:00
|
|
|
t1.join();
|
|
|
|
t2.join();
|
2016-08-26 20:53:43 +00:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(1, instance_count);
|
|
|
|
|
|
|
|
// Reset instance count
|
|
|
|
instance_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_case_class_race()
|
|
|
|
{
|
|
|
|
Callback<void()> cb(main_class_race);
|
2017-10-25 07:40:05 +00:00
|
|
|
Thread t1(osPriorityNormal, TEST_STACK_SIZE);
|
|
|
|
Thread t2(osPriorityNormal, TEST_STACK_SIZE);
|
2016-08-26 20:53:43 +00:00
|
|
|
|
|
|
|
// Start start first thread
|
2017-10-25 07:40:05 +00:00
|
|
|
t1.start(cb);
|
2016-08-26 20:53:43 +00:00
|
|
|
// Start second thread while the first is inside the constructor
|
2018-10-09 11:41:36 +00:00
|
|
|
ThisThread::sleep_for(250);
|
2017-10-25 07:40:05 +00:00
|
|
|
t2.start(cb);
|
2016-08-26 20:53:43 +00:00
|
|
|
|
|
|
|
// Wait for the threads to finish
|
2017-10-25 07:40:05 +00:00
|
|
|
t1.join();
|
|
|
|
t2.join();
|
2016-08-26 20:53:43 +00:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(1, instance_count);
|
|
|
|
|
|
|
|
// Reset instance count
|
|
|
|
instance_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Case cases[] = {
|
|
|
|
Case("function init race", test_case_func_race),
|
|
|
|
Case("class init race", test_case_class_race),
|
|
|
|
};
|
|
|
|
|
|
|
|
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
|
|
|
|
{
|
|
|
|
GREENTEA_SETUP(20, "default_auto");
|
|
|
|
return greentea_test_setup_handler(number_of_cases);
|
|
|
|
}
|
|
|
|
|
|
|
|
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Harness::run(specification);
|
|
|
|
}
|