test: fix ThreadSanitizer data race warning
Legitimate if fairly benign warning: the `stop` global variable was read and written without proper synchronization; `volatile` isn't sufficient. Refs: https://github.com/libuv/libuv/issues/3681
This commit is contained in:
parent
7b5a21deaa
commit
f328457cb1
@ -50,18 +50,18 @@ enum signal_action {
|
||||
};
|
||||
|
||||
static uv_sem_t sem;
|
||||
static uv_mutex_t counter_lock;
|
||||
static volatile int stop = 0;
|
||||
static uv_mutex_t lock;
|
||||
static int stop = 0;
|
||||
|
||||
static volatile int signal1_cb_counter = 0;
|
||||
static volatile int signal2_cb_counter = 0;
|
||||
static volatile int loop_creation_counter = 0;
|
||||
static int signal1_cb_counter = 0;
|
||||
static int signal2_cb_counter = 0;
|
||||
static int loop_creation_counter = 0;
|
||||
|
||||
|
||||
static void increment_counter(volatile int* counter) {
|
||||
uv_mutex_lock(&counter_lock);
|
||||
static void increment_counter(int* counter) {
|
||||
uv_mutex_lock(&lock);
|
||||
++(*counter);
|
||||
uv_mutex_unlock(&counter_lock);
|
||||
uv_mutex_unlock(&lock);
|
||||
}
|
||||
|
||||
|
||||
@ -162,6 +162,8 @@ static void signal_unexpected_cb(uv_signal_t* handle, int signum) {
|
||||
|
||||
|
||||
static void loop_creating_worker(void* context) {
|
||||
int done;
|
||||
|
||||
(void) context;
|
||||
|
||||
do {
|
||||
@ -188,7 +190,11 @@ static void loop_creating_worker(void* context) {
|
||||
free(loop);
|
||||
|
||||
increment_counter(&loop_creation_counter);
|
||||
} while (!stop);
|
||||
|
||||
uv_mutex_lock(&lock);
|
||||
done = stop;
|
||||
uv_mutex_unlock(&lock);
|
||||
} while (!done);
|
||||
}
|
||||
|
||||
|
||||
@ -215,7 +221,7 @@ TEST_IMPL(signal_multiple_loops) {
|
||||
r = uv_sem_init(&sem, 0);
|
||||
ASSERT(r == 0);
|
||||
|
||||
r = uv_mutex_init(&counter_lock);
|
||||
r = uv_mutex_init(&lock);
|
||||
ASSERT(r == 0);
|
||||
|
||||
/* Create a couple of threads that create a destroy loops continuously. */
|
||||
@ -272,7 +278,9 @@ TEST_IMPL(signal_multiple_loops) {
|
||||
}
|
||||
|
||||
/* Tell all loop creating threads to stop. */
|
||||
uv_mutex_lock(&lock);
|
||||
stop = 1;
|
||||
uv_mutex_unlock(&lock);
|
||||
|
||||
/* Wait for all loop creating threads to exit. */
|
||||
for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user