[Lldb-commits] [lldb] r269025 - Fix race in TestExitDuringStep and unify pseudo_barrier handling

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue May 10 00:54:27 PDT 2016


Author: labath
Date: Tue May 10 02:54:25 2016
New Revision: 269025

URL: http://llvm.org/viewvc/llvm-project?rev=269025&view=rev
Log:
Fix race in TestExitDuringStep and unify pseudo_barrier handling

Summary:
TestExitDuringStep was very rarely hanging on the buildbots. I can't be sure, but I believe this
was because of the fact that it declared its pseudo_barrier variable as "volatile int", which is
not sufficient to guarantee corectness (also, all other tests used atomic variables for this, and
they were passing reliably AFAIK). Besides switching to an atomic variable in this test as well,
I have also took this opportunity to unify all the copies of the pseudo_barrier code to a single
place to reduce the chance of this happening again.

Reviewers: clayborg

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D20065

Modified:
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/break_after_join/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/concurrent_events/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/create_during_step/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_break/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_step/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/multi_break/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/step_out/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/thread_exit/main.cpp
    lldb/trunk/packages/Python/lldbsuite/test/make/test_common.h

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/break_after_join/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/break_after_join/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/break_after_join/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/break_after_join/main.cpp Tue May 10 02:54:25 2016
@@ -19,24 +19,12 @@
 
 volatile int g_test = 0;
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
-#define do_nothing()  
-
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
 // A barrier to synchronize all the threads.
-std::atomic_int g_barrier1;
+pseudo_barrier_t g_barrier1;
 
 // A barrier to keep the threads from exiting until after the breakpoint has
 // been passed.
-std::atomic_int g_barrier2;
+pseudo_barrier_t g_barrier2;
 
 void *
 break_thread_func ()

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/concurrent_events/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/concurrent_events/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/concurrent_events/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/concurrent_events/main.cpp Tue May 10 02:54:25 2016
@@ -23,22 +23,10 @@ using namespace std;
 #include <sys/types.h>
 #include <unistd.h>
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
-#define do_nothing()
-
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
 typedef std::vector<std::pair<unsigned, void*(*)(void*)> > action_counts;
 typedef std::vector<pthread_t> thread_vector;
 
-std::atomic_int g_barrier;
+pseudo_barrier_t g_barrier;
 int g_breakpoint = 0;
 int g_sigusr1_count = 0;
 std::atomic_int g_watchme;

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/create_during_step/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/create_during_step/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/create_during_step/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/create_during_step/main.cpp Tue May 10 02:54:25 2016
@@ -13,19 +13,9 @@
 #include <atomic>
 #include <thread>
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
 #define do_nothing()
 
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
-std::atomic_int g_barrier;
+pseudo_barrier_t g_barrier;
 
 volatile int g_thread_created = 0;
 volatile int g_test = 0;

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_break/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_break/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_break/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_break/main.cpp Tue May 10 02:54:25 2016
@@ -19,27 +19,15 @@
 
 volatile int g_test = 0;
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
-#define do_nothing()  
-
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
 // A barrier to synchronize all the threads except the one that will exit.
-std::atomic_int g_barrier1;
+pseudo_barrier_t g_barrier1;
 
 // A barrier to synchronize all the threads including the one that will exit.
-std::atomic_int g_barrier2;
+pseudo_barrier_t g_barrier2;
 
 // A barrier to keep the first group of threads from exiting until after the
 // breakpoint has been passed.
-std::atomic_int g_barrier3;
+pseudo_barrier_t g_barrier3;
 
 void *
 break_thread_func ()

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_step/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_step/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_step/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/exit_during_step/main.cpp Tue May 10 02:54:25 2016
@@ -12,20 +12,10 @@
 
 #include <thread>
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
 #define do_nothing()
 
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
 // A barrier to synchronize thread start.
-volatile int g_barrier;
+pseudo_barrier_t g_barrier;
 
 volatile int g_thread_exited = 0;
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/multi_break/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/multi_break/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/multi_break/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/multi_break/main.cpp Tue May 10 02:54:25 2016
@@ -15,19 +15,7 @@
 #include <atomic>
 #include <thread>
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
-#define do_nothing()
-
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
-std::atomic_int g_barrier;
+pseudo_barrier_t g_barrier;
 
 volatile int g_test = 0;
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/step_out/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/step_out/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/step_out/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/step_out/main.cpp Tue May 10 02:54:25 2016
@@ -13,19 +13,7 @@
 #include <atomic>
 #include <thread>
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
-#define do_nothing()
-
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
-std::atomic_int g_barrier;
+pseudo_barrier_t g_barrier;
 
 volatile int g_test = 0;
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/thread_exit/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/thread_exit/main.cpp?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/thread_exit/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/thread/thread_exit/main.cpp Tue May 10 02:54:25 2016
@@ -12,21 +12,9 @@
 #include <atomic>
 #include <thread>
 
-// Note that although hogging the CPU while waiting for a variable to change
-// would be terrible in production code, it's great for testing since it
-// avoids a lot of messy context switching to get multiple threads synchronized.
-#define do_nothing()
-
-#define pseudo_barrier_wait(bar) \
-    --bar;                       \
-    while (bar > 0)              \
-        do_nothing();
-
-#define pseudo_barrier_init(bar, count) (bar = count)
-
-std::atomic_int g_barrier1;
-std::atomic_int g_barrier2;
-std::atomic_int g_barrier3;
+pseudo_barrier_t g_barrier1;
+pseudo_barrier_t g_barrier2;
+pseudo_barrier_t g_barrier3;
 
 void *
 thread1 ()

Modified: lldb/trunk/packages/Python/lldbsuite/test/make/test_common.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/make/test_common.h?rev=269025&r1=269024&r2=269025&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/make/test_common.h (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/make/test_common.h Tue May 10 02:54:25 2016
@@ -42,3 +42,26 @@
 #define lldb_enable_attach()
 
 #endif
+
+#ifdef __cplusplus
+#include <atomic>
+
+// Note that although hogging the CPU while waiting for a variable to change
+// would be terrible in production code, it's great for testing since it
+// avoids a lot of messy context switching to get multiple threads synchronized.
+
+typedef std::atomic<int> pseudo_barrier_t;
+#define pseudo_barrier_wait(barrier)        \
+    do                                      \
+    {                                       \
+        --(barrier);                        \
+        while ((barrier).load() > 0)        \
+            ;                               \
+    } while (0)
+
+#define pseudo_barrier_init(barrier, count) \
+    do                                      \
+    {                                       \
+        (barrier) = (count);                \
+    } while (0)
+#endif // __cplusplus




More information about the lldb-commits mailing list