diff --git a/.gitignore b/.gitignore index 3f268e6..332a8d7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # Prerequisites *.d +.cache compile_commands.json # Object files diff --git a/inc/topo.h b/inc/topo.h index 9fae90c..8a30018 100644 --- a/inc/topo.h +++ b/inc/topo.h @@ -1,6 +1,7 @@ #pragma once #include +#include #ifdef __cplusplus extern "C" { diff --git a/test/test.c b/test/test.c index 9ca3c39..e35228b 100644 --- a/test/test.c +++ b/test/test.c @@ -96,6 +96,7 @@ int ts_test() int main() { topo_init(1, 1); + topo_init(1, 0); ts_test(); return 0; } \ No newline at end of file diff --git a/topo.c b/topo.c index 6011445..6d20a46 100644 --- a/topo.c +++ b/topo.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "libxml/parser.h" @@ -413,6 +414,9 @@ topo_core_to_numa(int coreid) return _topo_core_to_numa(g_tobj, coreid); } +static _Atomic(int) initialized = 0; +static volatile int init_rc = 0; + void topo_destroy() { @@ -422,5 +426,15 @@ topo_destroy() int topo_init(int verbose, int alloc_init) { - return _topo_init(verbose, alloc_init, &g_tobj); + int expected = 0; + if (atomic_compare_exchange_strong(&initialized, &expected, 2)) { + init_rc = _topo_init(verbose, alloc_init, &g_tobj); + atomic_store(&initialized, 1); + } else { + while(atomic_load(&initialized) != 1) {} // wait for init + if (verbose) { + fprintf(stdout, "libtopo: already initialized with rc = %d\n", init_rc); + } + } + return init_rc; } \ No newline at end of file