Skip to content

Commit

Permalink
Resolve race around single counter by using two counters
Browse files Browse the repository at this point in the history
Signed-off-by: Quincey Koziol <[email protected]>
  • Loading branch information
qkoziol committed Aug 30, 2024
1 parent d587796 commit 2c5ab37
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions test/ttsafe_semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

#if defined(H5_HAVE_THREADS)

#define NUM_PINGPONG (500 * 1000)
#define NUM_PINGPONG (250 * 1000)
#define NUM_CLIENTSERVER (50 * 1000)

#define NUM_THREADS 16

typedef struct {
H5TS_semaphore_t ping_sem, pong_sem;
H5TS_atomic_uint_t counter;
H5TS_atomic_uint_t ping_counter;
H5TS_atomic_uint_t pong_counter;
} pingpong_t;

typedef struct {
Expand All @@ -47,11 +48,11 @@ ping(void *_test_info)
result = H5TS_semaphore_wait(&test_info->ping_sem);
CHECK_I(result, "H5TS_semaphore_wait");

H5TS_atomic_fetch_add_uint(&test_info->counter, (unsigned)1);
H5TS_atomic_fetch_add_uint(&test_info->ping_counter, (unsigned)1);

result = H5TS_semaphore_signal(&test_info->pong_sem);
CHECK_I(result, "H5TS_semaphore_signal");
} while (H5TS_atomic_load_uint(&test_info->counter) < NUM_PINGPONG);
} while (H5TS_atomic_load_uint(&test_info->ping_counter) < NUM_PINGPONG);

return ret_value;
}
Expand All @@ -67,11 +68,11 @@ pong(void *_test_info)
result = H5TS_semaphore_wait(&test_info->pong_sem);
CHECK_I(result, "H5TS_semaphore_wait");

H5TS_atomic_fetch_add_uint(&test_info->counter, (unsigned)1);
H5TS_atomic_fetch_add_uint(&test_info->pong_counter, (unsigned)1);

result = H5TS_semaphore_signal(&test_info->ping_sem);
CHECK_I(result, "H5TS_semaphore_signal");
} while (H5TS_atomic_load_uint(&test_info->counter) < NUM_PINGPONG);
} while (H5TS_atomic_load_uint(&test_info->pong_counter) < NUM_PINGPONG);

return ret_value;
}
Expand All @@ -93,7 +94,8 @@ tts_semaphore_pingpong(void)
CHECK_I(result, "H5TS_semaphore_init");
result = H5TS_semaphore_init(&test_info.pong_sem, 0);
CHECK_I(result, "H5TS_semaphore_init");
H5TS_atomic_init_uint(&test_info.counter, (unsigned)0);
H5TS_atomic_init_uint(&test_info.ping_counter, (unsigned)0);
H5TS_atomic_init_uint(&test_info.pong_counter, (unsigned)0);

/* Start ping & pong threads */
result = H5TS_thread_create(&ping_thread, ping, &test_info);
Expand All @@ -111,15 +113,17 @@ tts_semaphore_pingpong(void)
result = H5TS_thread_join(pong_thread, NULL);
CHECK_I(result, "H5TS_thread_join");

VERIFY(H5TS_atomic_load_uint(&test_info.counter), (NUM_PINGPONG + 1), "ping pong");
VERIFY(H5TS_atomic_load_uint(&test_info.ping_counter), NUM_PINGPONG, "ping counter");
VERIFY(H5TS_atomic_load_uint(&test_info.pong_counter), NUM_PINGPONG, "pong counter");

/* Destroy semaphores, etc. */
result = H5TS_semaphore_destroy(&test_info.ping_sem);
CHECK_I(result, "H5TS_semaphore_destroy");
result = H5TS_semaphore_destroy(&test_info.pong_sem);
CHECK_I(result, "H5TS_semaphore_destroy");

H5TS_atomic_destroy_uint(&test_info.counter);
H5TS_atomic_destroy_uint(&test_info.ping_counter);
H5TS_atomic_destroy_uint(&test_info.pong_counter);
} /* end tts_semaphore_pingpong() */

static H5TS_THREAD_RETURN_TYPE
Expand Down

0 comments on commit 2c5ab37

Please sign in to comment.