Skip to content

Commit 0c55d8f

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 0c55d8f

File tree

6 files changed

+56
-8
lines changed

6 files changed

+56
-8
lines changed

src/pool/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
if(UMF_BUILD_SHARED_LIBRARY)
66
set(POOL_EXTRA_SRCS ${BA_SOURCES})
7+
set(POOL_COMPILE_DEFINITIONS UMF_SHARED_LIBRARY)
78
endif()
89

910
# libumf_pool_disjoint
@@ -12,6 +13,7 @@ if(UMF_BUILD_LIBUMF_POOL_DISJOINT)
1213
TYPE STATIC
1314
SRCS pool_disjoint.cpp ${POOL_EXTRA_SRCS}
1415
LIBS umf_utils)
16+
target_compile_definitions(disjoint_pool PUBLIC ${POOL_COMPILE_DEFINITIONS})
1517

1618
add_library(${PROJECT_NAME}::disjoint_pool ALIAS disjoint_pool)
1719

@@ -40,6 +42,7 @@ if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
4042
TYPE STATIC
4143
SRCS pool_jemalloc.c ${POOL_EXTRA_SRCS}
4244
LIBS c jemalloc umf_utils)
45+
target_compile_definitions(jemalloc_pool PUBLIC ${POOL_COMPILE_DEFINITIONS})
4346
add_library(${PROJECT_NAME}::jemalloc_pool ALIAS jemalloc_pool)
4447
install(TARGETS jemalloc_pool
4548
EXPORT ${PROJECT_NAME}-targets
@@ -56,6 +59,7 @@ if(UMF_BUILD_LIBUMF_POOL_SCALABLE)
5659
TYPE STATIC
5760
SRCS pool_scalable.c ${POOL_EXTRA_SRCS}
5861
LIBS dl umf_utils)
62+
target_compile_definitions(scalable_pool PUBLIC ${POOL_COMPILE_DEFINITIONS})
5963
add_library(${PROJECT_NAME}::scalable_pool ALIAS scalable_pool)
6064
install(TARGETS scalable_pool
6165
EXPORT ${PROJECT_NAME}-targets

src/pool/pool_jemalloc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
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+
#ifdef UMF_SHARED_LIBRARY
26+
static UTIL_ONCE_FLAG jemalloc_is_initialized = UTIL_ONCE_FLAG_INIT;
27+
static void jemalloc_global_init(void) {
28+
umf_ba_create_global();
29+
atexit(umf_ba_destroy_global);
30+
}
31+
#endif
32+
2433
typedef struct jemalloc_memory_pool_t {
2534
umf_memory_provider_handle_t provider;
2635
unsigned int arena_index; // index of jemalloc arena
@@ -353,6 +362,10 @@ static void *je_aligned_alloc(void *pool, size_t size, size_t alignment) {
353362

354363
static umf_result_t je_initialize(umf_memory_provider_handle_t provider,
355364
void *params, void **out_pool) {
365+
#ifdef UMF_SHARED_LIBRARY
366+
util_init_once(&jemalloc_is_initialized, jemalloc_global_init);
367+
#endif
368+
356369
assert(provider);
357370
assert(out_pool);
358371
(void)params; // unused

src/pool/pool_scalable.c

Lines changed: 14 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,15 @@ 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+
#ifdef UMF_SHARED_LIBRARY
108+
umf_ba_create_global();
109+
atexit(umf_ba_destroy_global);
110+
#endif
111+
106112
g_tbb_ops = tbb_ops;
107113
}
108114

@@ -144,8 +150,8 @@ static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider,
144150
.keep_all_memory = false,
145151
.reserved = 0};
146152

147-
pthread_once(&tbb_is_initialized, load_tbb_symbols);
148-
if (Load_tbb_symbols_failed) {
153+
util_init_once(&tbb_is_initialized, init_tbb_global_state);
154+
if (Init_tbb_global_state_failed) {
149155
fprintf(stderr, "loading TBB symbols failed\n");
150156
return UMF_RESULT_ERROR_UNKNOWN;
151157
}
@@ -176,7 +182,7 @@ static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider,
176182
}
177183

178184
static void tbb_pool_finalize(void *pool) {
179-
pthread_once(&tbb_is_initialized, load_tbb_symbols);
185+
util_init_once(&tbb_is_initialized, init_tbb_global_state);
180186
struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool;
181187
g_tbb_ops.pool_destroy(pool_data->tbb_pool);
182188
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)