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

llvm-svn: 269025
This commit is contained in:
Pavel Labath 2016-05-10 07:54:25 +00:00
parent bdb54eb457
commit ebc7135f8e
9 changed files with 36 additions and 105 deletions

View File

@ -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 ()

View File

@ -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;

View File

@ -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;

View File

@ -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 ()

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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 ()

View File

@ -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