add duplicate init detection

This commit is contained in:
quackerd 2022-05-27 02:46:56 +08:00
parent 7ea75b5f4d
commit b0cde80b54
4 changed files with 18 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
# Prerequisites
*.d
.cache
compile_commands.json
# Object files

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {

View File

@ -96,6 +96,7 @@ int ts_test()
int main()
{
topo_init(1, 1);
topo_init(1, 0);
ts_test();
return 0;
}

16
topo.c
View File

@ -8,6 +8,7 @@
#include <sys/_cpuset.h>
#include <sys/cpuset.h>
#include <sys/sysctl.h>
#include <stdatomic.h>
#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;
}