Skip to content

Commit

Permalink
Fix semaphore test (#4725)
Browse files Browse the repository at this point in the history
* Make counter in semaphore test an atomic variable

* Revert using atomic counter and fix counter access outside of semaphore
  • Loading branch information
qkoziol authored Aug 19, 2024
1 parent b4731e4 commit adc3a37
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/H5TSsemaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ H5TS_semaphore_wait(H5TS_semaphore_t *sem)
*
* Purpose: Increments (unlocks) the semaphore. If the semaphore's value
* becomes greater than zero, then another thread blocked in a wait
* call will be woken up and proceed to lock the semaphore.
* call will proceed to lock the semaphore.
*
* Return: Non-negative on success / Negative on failure
*
Expand Down
2 changes: 0 additions & 2 deletions test/ttsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ main(int argc, char *argv[])
AddTest("rec_rwlock_4", tts_rec_rw_lock_smoke_check_4, NULL,
"recursive R/W lock smoke check 4 -- mixed mob", NULL);
#endif /* !H5_HAVE_WIN_THREADS */
#ifdef H5_HAVE_STDATOMIC_H
AddTest("semaphore", tts_semaphore, NULL, "lightweight system semaphores", NULL);
#endif /* H5_HAVE_STDATOMIC_H */

#ifdef H5_HAVE_THREADSAFE
AddTest("thread_id", tts_thread_id, NULL, "thread IDs", NULL);
Expand Down
2 changes: 0 additions & 2 deletions test/ttsafe.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ void tts_is_threadsafe(void);
#ifdef H5_HAVE_THREADS
void tts_thread_pool(void);
void tts_atomics(void);
#ifdef H5_HAVE_STDATOMIC_H
void tts_semaphore(void);
#endif /* H5_HAVE_STDATOMIC_H */
void tts_rec_rw_lock_smoke_check_1(void);
void tts_rec_rw_lock_smoke_check_2(void);
void tts_rec_rw_lock_smoke_check_3(void);
Expand Down
14 changes: 8 additions & 6 deletions test/ttsafe_semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "ttsafe.h"

#if defined(H5_HAVE_THREADS) && defined(H5_HAVE_STDATOMIC_H)
#if defined(H5_HAVE_THREADS)

#define NUM_PINGPONG (1000 * 1000)
#define NUM_CLIENTSERVER (50 * 1000)
Expand All @@ -40,18 +40,19 @@ static H5TS_THREAD_RETURN_TYPE
ping(void *_test_info)
{
pingpong_t *test_info = (pingpong_t *)_test_info;
unsigned count;
herr_t result;
H5TS_thread_ret_t ret_value = 0;

do {
result = H5TS_semaphore_wait(&test_info->ping_sem);
CHECK_I(result, "H5TS_semaphore_wait");

test_info->counter++;
count = ++test_info->counter;

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

return ret_value;
}
Expand All @@ -60,18 +61,19 @@ static H5TS_THREAD_RETURN_TYPE
pong(void *_test_info)
{
pingpong_t *test_info = (pingpong_t *)_test_info;
unsigned count;
herr_t result;
H5TS_thread_ret_t ret_value = 0;

do {
result = H5TS_semaphore_wait(&test_info->pong_sem);
CHECK_I(result, "H5TS_semaphore_wait");

test_info->counter++;
count = ++test_info->counter;

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

return ret_value;
}
Expand Down Expand Up @@ -268,4 +270,4 @@ tts_semaphore(void)
tts_semaphore_clientserver();
} /* end tts_semaphore() */

#endif /* defined(H5_HAVE_THREADS) && defined(H5_HAVE_STDATOMIC_H) */
#endif /* defined(H5_HAVE_THREADS) */

0 comments on commit adc3a37

Please sign in to comment.