From 6f7797807c76bcb38b2ff9ab887137256c5018e7 Mon Sep 17 00:00:00 2001 From: Jaakko Korhonen Date: Wed, 12 Jul 2017 13:44:45 +0300 Subject: [PATCH 1/3] Added multithreaded lock test. This should detect issues with uneven locking and releasing. --- test/CMakeLists.txt | 2 ++ test/Test.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d67c574..a3b78b2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -16,6 +16,8 @@ if(DEFINED TARGET_LIKE_X86_WINDOWS_NATIVE OR DEFINED TARGET_LIKE_X86_LINUX_NATIV endif() if(DEFINED TARGET_LIKE_X86_LINUX_NATIVE) SET(TEST_EXECUTABLE "../../../build/x86-linux-native/test/mbed_trace_test") + find_package (Threads) + target_link_libraries (mbed_trace_test ${CMAKE_THREAD_LIBS_INIT}) add_test(mbed_trace_test ${TEST_EXECUTABLE}) add_dependencies(all_tests mbed_trace_test) endif() diff --git a/test/Test.cpp b/test/Test.cpp index 52b5934..f2238e7 100644 --- a/test/Test.cpp +++ b/test/Test.cpp @@ -9,6 +9,8 @@ #include #include #include +#include /* pthread_create() */ +#include /* usleep() */ #include "mbed-cpputest/CppUTest/TestHarness.h" #include "mbed-cpputest/CppUTest/SimpleString.h" @@ -474,3 +476,33 @@ TEST(trace, uninitialized) STRCMP_EQUAL("hello", buf); } +void* multithread_printer(void *ti) +{ + volatile int thread_index = *(int *)ti; + for(int i=0; i<10; i++) { + //printf("thread index = %d, round %d\n", thread_index, i); + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "thread index = %d, round %d", thread_index, i); + usleep(1000 * (1 + thread_index)); /* (1 + thread_index) ms delay */ + } + return NULL; +} + +TEST(trace, multithread) +{ + const int thread_amount = 10; + pthread_t threads[thread_amount]; + int thread_indexes[thread_amount]; + + for(int j=0; j<10; j++) + { + for(int i=0; i<(sizeof(threads)/sizeof(pthread_t)); i++) { + //printf("creating %d\n", i); + thread_indexes[i] = i; + pthread_create(&threads[i], NULL, &multithread_printer, (void *)&thread_indexes[i]); + } + for(int i=0; i<(sizeof(threads)/sizeof(pthread_t)); i++) { + //printf("joining %d\n", i); + pthread_join(threads[i], NULL); + } + } +} From eedef06037954ba2044ec0b63c03b860881a2cf7 Mon Sep 17 00:00:00 2001 From: Jaakko Korhonen Date: Wed, 12 Jul 2017 13:47:31 +0300 Subject: [PATCH 2/3] Modified loops to use thread_mount instead of sizeof() calculation. --- test/Test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Test.cpp b/test/Test.cpp index f2238e7..25f93c7 100644 --- a/test/Test.cpp +++ b/test/Test.cpp @@ -495,12 +495,12 @@ TEST(trace, multithread) for(int j=0; j<10; j++) { - for(int i=0; i<(sizeof(threads)/sizeof(pthread_t)); i++) { + for(int i=0; i Date: Wed, 12 Jul 2017 13:49:02 +0300 Subject: [PATCH 3/3] Explicit declaration for i and j. --- test/Test.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/Test.cpp b/test/Test.cpp index 25f93c7..9f24efa 100644 --- a/test/Test.cpp +++ b/test/Test.cpp @@ -492,15 +492,16 @@ TEST(trace, multithread) const int thread_amount = 10; pthread_t threads[thread_amount]; int thread_indexes[thread_amount]; + int i, j; - for(int j=0; j<10; j++) + for(j=0; j<10; j++) { - for(int i=0; i