add duplicate init detection
This commit is contained in:
parent
7ea75b5f4d
commit
b0cde80b54
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,6 +2,7 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
.cache
|
||||
compile_commands.json
|
||||
|
||||
# Object files
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -96,6 +96,7 @@ int ts_test()
|
||||
int main()
|
||||
{
|
||||
topo_init(1, 1);
|
||||
topo_init(1, 0);
|
||||
ts_test();
|
||||
return 0;
|
||||
}
|
16
topo.c
16
topo.c
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user