Skip to content

Commit 51d9aa4

Browse files
committed
Initialize ba_alloc_global in every pool that is build as a separate lib
Since ba_alloc sources are compiled directly into those pools they each have a separate copy of base_alloc.
1 parent 0fb7119 commit 51d9aa4

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

src/pool/pool_jemalloc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@
1515

1616
#include "base_alloc_global.h"
1717
#include "utils_common.h"
18+
#include "utils_concurrency.h"
1819
#include <umf/memory_pool.h>
1920
#include <umf/memory_pool_ops.h>
2021
#include <umf/pools/pool_jemalloc.h>
2122

2223
#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
2324

25+
static UTIL_ONCE_FLAG jemalloc_is_initialized = UTIL_ONCE_FLAG_INIT;
26+
27+
static void jemalloc_global_init(void) {
28+
umf_ba_create_global();
29+
atexit(umf_ba_destroy_global);
30+
}
31+
2432
typedef struct jemalloc_memory_pool_t {
2533
umf_memory_provider_handle_t provider;
2634
unsigned int arena_index; // index of jemalloc arena
@@ -353,6 +361,8 @@ static void *je_aligned_alloc(void *pool, size_t size, size_t alignment) {
353361

354362
static umf_result_t je_initialize(umf_memory_provider_handle_t provider,
355363
void *params, void **out_pool) {
364+
util_init_once(&jemalloc_is_initialized, jemalloc_global_init);
365+
356366
assert(provider);
357367
assert(out_pool);
358368
(void)params; // unused

src/pool/pool_scalable.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "base_alloc_global.h"
2727
#include "utils_common.h"
28+
#include "utils_concurrency.h"
2829
#include "utils_sanitizers.h"
2930

3031
typedef void *(*raw_alloc_tbb_type)(intptr_t, size_t *);
@@ -61,15 +62,15 @@ struct tbb_memory_pool {
6162
};
6263

6364
static struct tbb_callbacks g_tbb_ops;
64-
static pthread_once_t tbb_is_initialized = PTHREAD_ONCE_INIT;
65-
static bool Load_tbb_symbols_failed;
65+
static UTIL_ONCE_FLAG tbb_is_initialized = UTIL_ONCE_FLAG_INIT;
66+
static bool Init_tbb_global_state_failed;
6667

67-
static void load_tbb_symbols(void) {
68+
static void init_tbb_global_state(void) {
6869
const char so_name[] = "libtbbmalloc.so.2";
6970
void *tbb_handle = dlopen(so_name, RTLD_LAZY);
7071
if (!tbb_handle) {
7172
fprintf(stderr, "%s not found.\n", so_name);
72-
Load_tbb_symbols_failed = true;
73+
Init_tbb_global_state_failed = true;
7374
return;
7475
}
7576

@@ -99,10 +100,13 @@ static void load_tbb_symbols(void) {
99100
!tbb_ops.pool_identify) {
100101
fprintf(stderr, "Could not find symbols in %s.\n", so_name);
101102
dlclose(tbb_handle);
102-
Load_tbb_symbols_failed = true;
103+
Init_tbb_global_state_failed = true;
103104
return;
104105
}
105106

107+
umf_ba_create_global();
108+
atexit(umf_ba_destroy_global);
109+
106110
g_tbb_ops = tbb_ops;
107111
}
108112

@@ -144,8 +148,8 @@ static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider,
144148
.keep_all_memory = false,
145149
.reserved = 0};
146150

147-
pthread_once(&tbb_is_initialized, load_tbb_symbols);
148-
if (Load_tbb_symbols_failed) {
151+
util_init_once(&tbb_is_initialized, init_tbb_global_state);
152+
if (Init_tbb_global_state_failed) {
149153
fprintf(stderr, "loading TBB symbols failed\n");
150154
return UMF_RESULT_ERROR_UNKNOWN;
151155
}
@@ -176,7 +180,7 @@ static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider,
176180
}
177181

178182
static void tbb_pool_finalize(void *pool) {
179-
pthread_once(&tbb_is_initialized, load_tbb_symbols);
183+
util_init_once(&tbb_is_initialized, init_tbb_global_state);
180184
struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool;
181185
g_tbb_ops.pool_destroy(pool_data->tbb_pool);
182186
umf_ba_free(pool_data->base_allocator, pool_data);

src/utils/utils_concurrency.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ void util_mutex_destroy_not_free(os_mutex_t *m);
3636
int util_mutex_lock(os_mutex_t *mutex);
3737
int util_mutex_unlock(os_mutex_t *mutex);
3838

39+
#if defined(_WIN32)
40+
#define UTIL_ONCE_FLAG INIT_ONCE
41+
#define UTIL_ONCE_FLAG_INIT INIT_ONCE_STATIC_INIT
42+
#else
43+
#define UTIL_ONCE_FLAG pthread_once_t
44+
#define UTIL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
45+
#endif
46+
47+
void util_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
48+
3949
#if defined(_WIN32)
4050
static __inline unsigned char util_lssb_index(long long value) {
4151
unsigned long ret;

src/utils/utils_posix_concurrency.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ int util_mutex_lock(os_mutex_t *m) {
3333
int util_mutex_unlock(os_mutex_t *m) {
3434
return pthread_mutex_unlock((pthread_mutex_t *)m);
3535
}
36+
37+
void util_init_once(UTIL_ONCE_FLAG *flag, void (*oneCb)(void)) {
38+
pthread_once(flag, oneCb);
39+
}

src/utils/utils_windows_concurrency.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ int util_mutex_unlock(os_mutex_t *mutex) {
3939
LeaveCriticalSection(&mutex_internal->lock);
4040
return 0;
4141
}
42+
43+
static BOOL CALLBACK initOnceCb(PINIT_ONCE InitOnce, PVOID Parameter,
44+
PVOID *lpContext) {
45+
void (*onceCb)(void) = (void (*)(void))(Parameter);
46+
onceCb();
47+
return TRUE;
48+
}
49+
50+
void util_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void)) {
51+
InitOnceExecuteOnce(flag, initOnceCb, (void *)&onceCb, NULL);
52+
}

0 commit comments

Comments
 (0)