Skip to content

Commit

Permalink
Test concurrent library init/close
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjala committed Nov 26, 2024
1 parent 784c8fb commit 6013700
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/H5.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ H5_init_library(void)
if (H5_INIT_GLOBAL || H5_TERM_GLOBAL)
HGOTO_DONE(SUCCEED);

/* Detect attempted double-initialization due to concurrency */
assert(!H5_INIT_GLOBAL && !H5_TERM_GLOBAL);

/* Set the 'library initialized' flag as early as possible, to avoid
* possible re-entrancy.
*/
Expand Down Expand Up @@ -318,6 +321,9 @@ H5_term_library(void)
if (!(H5_INIT_GLOBAL))
goto done;

/* Detect attempted double-initialization due to concurrency */
assert(H5_INIT_GLOBAL && !(H5_TERM_GLOBAL));

/* Indicate that the library is being shut down */
H5_TERM_GLOBAL = TRUE;

Expand Down
2 changes: 1 addition & 1 deletion test/threads/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ noinst_PROGRAMS = testmthdf5
# Programs to be built when testing
check_PROGRAMS = testmthdf5

testmthdf5_SOURCES=testmthdf5.c testmthdf5.h unit/mt_vl_test.c
testmthdf5_SOURCES=testmthdf5.c testmthdf5.h unit/mt_vl_test.c unit/mt_misc_test.c

testmthdf5_LDADD = $(LIBH5TEST) $(LIBHDF5)

Expand Down
10 changes: 7 additions & 3 deletions test/threads/testmthdf5.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ int main(int argc, char *argv[])
int num_errs_occurred = 0;
mt_test_params params;

/* Silence compiler warnings */
(void) params;

/* Initialize testing framework */
TestInit(argv[0], NULL, NULL);

Expand Down Expand Up @@ -106,9 +109,10 @@ int main(int argc, char *argv[])
AddTest("mt_reg_search", mt_test_register_and_search,
NULL, "MT reg/unreg of connectors while searching for connector", &params, 0);

#else /* H5_HAVE_MULTITHREAD */
/* Silence compiler warning */
(void)params;
/* Misc MT tests */
AddTest("mt_library_init", mt_test_library_init,
NULL, "MT usage of H5open/H5close", &params, 0);

#endif /* H5_HAVE_MULTITHREAD */
/* Display testing information */
TestInfo(argv[0]);
Expand Down
1 change: 1 addition & 0 deletions test/threads/testmthdf5.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ void mt_test_registration_operation_cleanup(void);
void mt_test_vol_wrap_ctx(void);
void mt_test_vol_wrap_ctx_cleanup(void);

void mt_test_library_init(void);
#endif /* H5_HAVE_MULTITHREAD */
#endif /* MTSAFE_H */
52 changes: 52 additions & 0 deletions test/threads/unit/mt_misc_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "../testmthdf5.h"
#include "h5test.h"
#include <pthread.h>

void *mt_test_library_init_helper(void *arg);

/* Test attempted concurrent library initialization/termination */
void mt_test_library_init(void) {
int max_num_threads = GetTestMaxNumThreads();
const mt_test_params *params = (const mt_test_params *)GetTestParameters();
pthread_t *threads = NULL;
int ret = 0;

assert(params != NULL);
alarm(params->subtest_timeout);

if (max_num_threads <= 0) {
TestErrPrintf("Invalid number of threads in MT test: %d\n", max_num_threads);
goto done;
}

threads = calloc((size_t) max_num_threads, sizeof(pthread_t));
CHECK(threads, NULL, "calloc");

for (int num_threads = 1; num_threads <= max_num_threads; num_threads++) {
for (int j = 0; j < num_threads; j++) {
ret = pthread_create(&threads[j], NULL, mt_test_library_init_helper, (void*)params->num_repetitions);
VERIFY(ret, 0, "pthread_create");
}

for (int j = 0; j < num_threads; j++) {
ret = pthread_join(threads[j], NULL);
VERIFY(ret, 0, "pthread_join");
}
}

free(threads);

done:
return;
}

void *mt_test_library_init_helper(void *arg) {
size_t num_repetitions = (size_t)arg;

for (size_t i = 0; i < num_repetitions; i++) {
H5open();
H5close();
}

return NULL;
}

0 comments on commit 6013700

Please sign in to comment.