Author: chaoren Date: Mon Mar 2 13:12:18 2015 New Revision: 230993 URL: http://llvm.org/viewvc/llvm-project?rev=230993&view=rev Log: Applying D7950 to four near copies of the file.
Summary: Using std::thread/random instead of pthread/rand. Two of them also suffered from the synchronization problem in D7916. Reviewers: clayborg, zturner, ki.stfu Subscribers: lldb-commits Differential Revision: http://reviews.llvm.org/D7991 Modified: lldb/trunk/test/functionalities/stop-hook/multiple_threads/Makefile lldb/trunk/test/functionalities/stop-hook/multiple_threads/main.cpp lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/Makefile lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/main.cpp lldb/trunk/test/functionalities/watchpoint/multiple_threads/Makefile lldb/trunk/test/functionalities/watchpoint/multiple_threads/main.cpp lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/Makefile lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/main.cpp lldb/trunk/test/python_api/watchpoint/watchlocation/Makefile lldb/trunk/test/python_api/watchpoint/watchlocation/main.cpp Modified: lldb/trunk/test/functionalities/stop-hook/multiple_threads/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/stop-hook/multiple_threads/Makefile?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/stop-hook/multiple_threads/Makefile (original) +++ lldb/trunk/test/functionalities/stop-hook/multiple_threads/Makefile Mon Mar 2 13:12:18 2015 @@ -1,6 +1,6 @@ LEVEL = ../../../make CXX_SOURCES := main.cpp -ENABLE_THREADS := YES +ENABLE_STD_THREADS := YES include $(LEVEL)/Makefile.rules Modified: lldb/trunk/test/functionalities/stop-hook/multiple_threads/main.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/stop-hook/multiple_threads/main.cpp?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/stop-hook/multiple_threads/main.cpp (original) +++ lldb/trunk/test/functionalities/stop-hook/multiple_threads/main.cpp Mon Mar 2 13:12:18 2015 @@ -7,41 +7,37 @@ // //===----------------------------------------------------------------------===// -// C includes -#include <pthread.h> -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <unistd.h> - -pthread_t g_thread_1 = NULL; -pthread_t g_thread_2 = NULL; -pthread_t g_thread_3 = NULL; +#include <chrono> +#include <cstdio> +#include <mutex> +#include <random> +#include <thread> -uint32_t g_val = 0; +std::default_random_engine g_random_engine{std::random_device{}()}; +std::uniform_int_distribution<> g_distribution{0, 3000000}; -uint32_t access_pool (uint32_t flag = 0); +uint32_t g_val = 0; uint32_t -access_pool (uint32_t flag) +access_pool (bool flag = false) { - static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER; - if (flag == 0) - ::pthread_mutex_lock (&g_access_mutex); + static std::mutex g_access_mutex; + if (!flag) + g_access_mutex.lock(); uint32_t old_val = g_val; - if (flag != 0) + if (flag) g_val = old_val + 1; - if (flag == 0) - ::pthread_mutex_unlock (&g_access_mutex); + if (!flag) + g_access_mutex.unlock(); return g_val; } -void * -thread_func (void *arg) +void +thread_func (uint32_t thread_index) { - uint32_t thread_index = *((uint32_t *)arg); // Break here to test that the stop-hook mechanism works for multiple threads. + // Break here to test that the stop-hook mechanism works for multiple threads. printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index); uint32_t count = 0; @@ -49,40 +45,33 @@ thread_func (void *arg) while (count++ < 15) { // random micro second sleep from zero to 3 seconds - int usec = ::rand() % 3000000; + int usec = g_distribution(g_random_engine); printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); - ::usleep (usec); - + std::this_thread::sleep_for(std::chrono::microseconds{usec}); + if (count < 7) val = access_pool (); else - val = access_pool (1); - + val = access_pool (true); + printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count); } printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index); - return NULL; } int main (int argc, char const *argv[]) { - int err; - void *thread_result = NULL; - uint32_t thread_index_1 = 1; - uint32_t thread_index_2 = 2; - uint32_t thread_index_3 = 3; + std::thread threads[3]; printf ("Before turning all three threads loose...\n"); // Set break point at this line, and add a stop-hook. // Create 3 threads - err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1); - err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2); - err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3); + for (auto &thread : threads) + thread = std::thread{thread_func, std::distance(threads, &thread)}; // Join all of our threads - err = ::pthread_join (g_thread_1, &thread_result); - err = ::pthread_join (g_thread_2, &thread_result); - err = ::pthread_join (g_thread_3, &thread_result); + for (auto &thread : threads) + thread.join(); return 0; } Modified: lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/Makefile?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/Makefile (original) +++ lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/Makefile Mon Mar 2 13:12:18 2015 @@ -1,6 +1,6 @@ LEVEL = ../../../make -ENABLE_THREADS := YES +ENABLE_STD_THREADS := YES CXX_SOURCES := main.cpp include $(LEVEL)/Makefile.rules Modified: lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/main.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/main.cpp?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/main.cpp (original) +++ lldb/trunk/test/functionalities/watchpoint/hello_watchlocation/main.cpp Mon Mar 2 13:12:18 2015 @@ -70,12 +70,12 @@ thread_func (uint32_t thread_index) int usec = g_distribution(g_random_engine); printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); std::this_thread::sleep_for(std::chrono::microseconds{usec}); - + if (count < 7) val = access_pool (); else val = access_pool (true); - + printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count); } printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index); @@ -84,11 +84,10 @@ thread_func (uint32_t thread_index) int main (int argc, char const *argv[]) { - g_char_ptr = new char; - *g_char_ptr = 0; - - std::thread threads[3]; g_count = 4; + std::thread threads[3]; + + g_char_ptr = new char{}; // Create 3 threads for (auto &thread : threads) Modified: lldb/trunk/test/functionalities/watchpoint/multiple_threads/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/multiple_threads/Makefile?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/watchpoint/multiple_threads/Makefile (original) +++ lldb/trunk/test/functionalities/watchpoint/multiple_threads/Makefile Mon Mar 2 13:12:18 2015 @@ -1,6 +1,6 @@ LEVEL = ../../../make -ENABLE_THREADS := YES +ENABLE_STD_THREADS := YES CXX_SOURCES := main.cpp include $(LEVEL)/Makefile.rules Modified: lldb/trunk/test/functionalities/watchpoint/multiple_threads/main.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/multiple_threads/main.cpp?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/watchpoint/multiple_threads/main.cpp (original) +++ lldb/trunk/test/functionalities/watchpoint/multiple_threads/main.cpp Mon Mar 2 13:12:18 2015 @@ -7,44 +7,42 @@ // //===----------------------------------------------------------------------===// -// C includes -#include <pthread.h> -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <unistd.h> - -pthread_t g_thread_1 = NULL; -pthread_t g_thread_2 = NULL; -pthread_t g_thread_3 = NULL; +#include <chrono> +#include <cstdio> +#include <mutex> +#include <random> +#include <thread> + +std::default_random_engine g_random_engine{std::random_device{}()}; +std::uniform_int_distribution<> g_distribution{0, 3000000}; uint32_t g_val = 0; -uint32_t access_pool (uint32_t flag = 0); uint32_t -access_pool (uint32_t flag) +access_pool (bool flag = false) { - static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER; - if (flag == 0) - ::pthread_mutex_lock (&g_access_mutex); + static std::mutex g_access_mutex; + if (!flag) + g_access_mutex.lock(); uint32_t old_val = g_val; - if (flag != 0) { - printf("changing g_val to %d...\n", (old_val + 1)); + if (flag) + { + printf("changing g_val to %d...\n", old_val + 1); g_val = old_val + 1; } - if (flag == 0) - ::pthread_mutex_unlock (&g_access_mutex); + if (!flag) + g_access_mutex.unlock(); return g_val; } -void * -thread_func (void *arg) +void +thread_func (uint32_t thread_index) { - uint32_t thread_index = *((uint32_t *)arg); // Break here in order to allow the thread - // to inherit the global watchpoint state. + // Break here in order to allow the thread + // to inherit the global watchpoint state. printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index); uint32_t count = 0; @@ -52,41 +50,34 @@ thread_func (void *arg) while (count++ < 15) { // random micro second sleep from zero to 3 seconds - int usec = ::rand() % 3000000; + int usec = g_distribution(g_random_engine); printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); - ::usleep (usec); - + std::this_thread::sleep_for(std::chrono::microseconds{usec}); + if (count < 7) val = access_pool (); else - val = access_pool (1); - + val = access_pool (true); + printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count); } printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index); - return NULL; } int main (int argc, char const *argv[]) { - int err; - void *thread_result = NULL; - uint32_t thread_index_1 = 1; - uint32_t thread_index_2 = 2; - uint32_t thread_index_3 = 3; + std::thread threads[3]; printf ("Before turning all three threads loose...\n"); // Set break point at this line, // in order to set our watchpoint. // Create 3 threads - err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1); - err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2); - err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3); + for (auto &thread : threads) + thread = std::thread{thread_func, std::distance(threads, &thread)}; // Join all of our threads - err = ::pthread_join (g_thread_1, &thread_result); - err = ::pthread_join (g_thread_2, &thread_result); - err = ::pthread_join (g_thread_3, &thread_result); + for (auto &thread : threads) + thread.join(); return 0; } Modified: lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/Makefile?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/Makefile (original) +++ lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/Makefile Mon Mar 2 13:12:18 2015 @@ -1,6 +1,6 @@ LEVEL = ../../../make -ENABLE_THREADS := YES +ENABLE_STD_THREADS := YES CXX_SOURCES := main.cpp include $(LEVEL)/Makefile.rules Modified: lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/main.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/main.cpp?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/main.cpp (original) +++ lldb/trunk/test/functionalities/watchpoint/watchpoint_set_command/main.cpp Mon Mar 2 13:12:18 2015 @@ -7,18 +7,29 @@ // //===----------------------------------------------------------------------===// -// C includes -#include <pthread.h> -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <unistd.h> - -pthread_t g_thread_1 = NULL; -pthread_t g_thread_2 = NULL; -pthread_t g_thread_3 = NULL; +#include <chrono> +#include <condition_variable> +#include <cstdio> +#include <random> +#include <thread> + +std::default_random_engine g_random_engine{std::random_device{}()}; +std::uniform_int_distribution<> g_distribution{0, 3000000}; +std::condition_variable g_condition_variable; +std::mutex g_mutex; +int g_count; -char *g_char_ptr = NULL; +char *g_char_ptr = nullptr; + +void +barrier_wait() +{ + std::unique_lock<std::mutex> lock{g_mutex}; + if (--g_count > 0) + g_condition_variable.wait(lock); + else + g_condition_variable.notify_all(); +} void do_bad_thing_with_location(unsigned index, char *char_ptr, char new_val) @@ -28,75 +39,68 @@ do_bad_thing_with_location(unsigned inde char_ptr[index] = new_val; } -uint32_t access_pool (uint32_t flag = 0); - uint32_t -access_pool (uint32_t flag) +access_pool (bool flag = false) { - static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER; + static std::mutex g_access_mutex; static unsigned idx = 0; // Well-behaving thread only writes into indexs from 0..6. - if (flag == 0) - ::pthread_mutex_lock (&g_access_mutex); + if (!flag) + g_access_mutex.lock(); // idx valid range is [0, 6]. if (idx > 6) idx = 0; - if (flag != 0) { + if (flag) + { // Write into a forbidden area. do_bad_thing_with_location(7, g_char_ptr, 99); } unsigned index = idx++; - if (flag == 0) - ::pthread_mutex_unlock (&g_access_mutex); + if (!flag) + g_access_mutex.unlock(); return g_char_ptr[index]; } -void * -thread_func (void *arg) +void +thread_func (uint32_t thread_index) { - uint32_t thread_index = *((uint32_t *)arg); printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index); + barrier_wait(); + uint32_t count = 0; uint32_t val; while (count++ < 15) { // random micro second sleep from zero to 3 seconds - int usec = ::rand() % 3000000; + int usec = g_distribution(g_random_engine); printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); - ::usleep (usec); - + std::this_thread::sleep_for(std::chrono::microseconds{usec}); + if (count < 7) val = access_pool (); else - val = access_pool (1); - + val = access_pool (true); + printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count); } printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index); - return NULL; } int main (int argc, char const *argv[]) { - int err; - void *thread_result = NULL; - uint32_t thread_index_1 = 1; - uint32_t thread_index_2 = 2; - uint32_t thread_index_3 = 3; - - g_char_ptr = (char *)malloc (10); - for (int i = 0; i < 10; ++i) - *g_char_ptr = 0; + g_count = 4; + std::thread threads[3]; + + g_char_ptr = new char[10]{}; // Create 3 threads - err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1); - err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2); - err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3); + for (auto &thread : threads) + thread = std::thread{thread_func, std::distance(threads, &thread)}; struct { int a; @@ -105,11 +109,13 @@ int main (int argc, char const *argv[]) } MyAggregateDataType; printf ("Before turning all three threads loose...\n"); // Set break point at this line. + barrier_wait(); // Join all of our threads - err = ::pthread_join (g_thread_1, &thread_result); - err = ::pthread_join (g_thread_2, &thread_result); - err = ::pthread_join (g_thread_3, &thread_result); + for (auto &thread : threads) + thread.join(); + + delete[] g_char_ptr; return 0; } Modified: lldb/trunk/test/python_api/watchpoint/watchlocation/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/watchpoint/watchlocation/Makefile?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/python_api/watchpoint/watchlocation/Makefile (original) +++ lldb/trunk/test/python_api/watchpoint/watchlocation/Makefile Mon Mar 2 13:12:18 2015 @@ -1,6 +1,6 @@ LEVEL = ../../../make -ENABLE_THREADS := YES +ENABLE_STD_THREADS := YES CXX_SOURCES := main.cpp include $(LEVEL)/Makefile.rules Modified: lldb/trunk/test/python_api/watchpoint/watchlocation/main.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/watchpoint/watchlocation/main.cpp?rev=230993&r1=230992&r2=230993&view=diff ============================================================================== --- lldb/trunk/test/python_api/watchpoint/watchlocation/main.cpp (original) +++ lldb/trunk/test/python_api/watchpoint/watchlocation/main.cpp Mon Mar 2 13:12:18 2015 @@ -7,18 +7,29 @@ // //===----------------------------------------------------------------------===// -// C includes -#include <pthread.h> -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <unistd.h> - -pthread_t g_thread_1 = NULL; -pthread_t g_thread_2 = NULL; -pthread_t g_thread_3 = NULL; +#include <chrono> +#include <condition_variable> +#include <cstdio> +#include <random> +#include <thread> + +std::default_random_engine g_random_engine{std::random_device{}()}; +std::uniform_int_distribution<> g_distribution{0, 3000000}; +std::condition_variable g_condition_variable; +std::mutex g_mutex; +int g_count; -char *g_char_ptr = NULL; +char *g_char_ptr = nullptr; + +void +barrier_wait() +{ + std::unique_lock<std::mutex> lock{g_mutex}; + if (--g_count > 0) + g_condition_variable.wait(lock); + else + g_condition_variable.notify_all(); +} void do_bad_thing_with_location(char *char_ptr, char new_val) @@ -26,73 +37,68 @@ do_bad_thing_with_location(char *char_pt *char_ptr = new_val; } -uint32_t access_pool (uint32_t flag = 0); - uint32_t -access_pool (uint32_t flag) +access_pool (bool flag = false) { - static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER; - if (flag == 0) - ::pthread_mutex_lock (&g_access_mutex); + static std::mutex g_access_mutex; + if (!flag) + g_access_mutex.lock(); char old_val = *g_char_ptr; - if (flag != 0) + if (flag) do_bad_thing_with_location(g_char_ptr, old_val + 1); - if (flag == 0) - ::pthread_mutex_unlock (&g_access_mutex); + if (!flag) + g_access_mutex.unlock(); return *g_char_ptr; } -void * -thread_func (void *arg) +void +thread_func (uint32_t thread_index) { - uint32_t thread_index = *((uint32_t *)arg); printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index); + barrier_wait(); + uint32_t count = 0; uint32_t val; while (count++ < 15) { // random micro second sleep from zero to 3 seconds - int usec = ::rand() % 3000000; + int usec = g_distribution(g_random_engine); printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); - ::usleep (usec); - + std::this_thread::sleep_for(std::chrono::microseconds{usec}); + if (count < 7) val = access_pool (); else - val = access_pool (1); - + val = access_pool (true); + printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count); } printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index); - return NULL; } int main (int argc, char const *argv[]) { - int err; - void *thread_result = NULL; - uint32_t thread_index_1 = 1; - uint32_t thread_index_2 = 2; - uint32_t thread_index_3 = 3; + g_count = 4; + std::thread threads[3]; - g_char_ptr = (char *)malloc (1); - *g_char_ptr = 0; + g_char_ptr = new char{}; // Create 3 threads - err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1); - err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2); - err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3); + for (auto &thread : threads) + thread = std::thread{thread_func, std::distance(threads, &thread)}; printf ("Before turning all three threads loose...\n"); // Set break point at this line. + barrier_wait(); // Join all of our threads - err = ::pthread_join (g_thread_1, &thread_result); - err = ::pthread_join (g_thread_2, &thread_result); - err = ::pthread_join (g_thread_3, &thread_result); + for (auto &thread : threads) + thread.join(); + + delete g_char_ptr; return 0; } _______________________________________________ lldb-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits
