|
25 | 25 |
|
26 | 26 | #include "base_alloc_global.h" |
27 | 27 | #include "utils_common.h" |
| 28 | +#include "utils_concurrency.h" |
28 | 29 | #include "utils_sanitizers.h" |
29 | 30 |
|
30 | 31 | typedef void *(*raw_alloc_tbb_type)(intptr_t, size_t *); |
@@ -61,15 +62,15 @@ struct tbb_memory_pool { |
61 | 62 | }; |
62 | 63 |
|
63 | 64 | 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; |
66 | 67 |
|
67 | | -static void load_tbb_symbols(void) { |
| 68 | +static void init_tbb_global_state(void) { |
68 | 69 | const char so_name[] = "libtbbmalloc.so.2"; |
69 | 70 | void *tbb_handle = dlopen(so_name, RTLD_LAZY); |
70 | 71 | if (!tbb_handle) { |
71 | 72 | fprintf(stderr, "%s not found.\n", so_name); |
72 | | - Load_tbb_symbols_failed = true; |
| 73 | + Init_tbb_global_state_failed = true; |
73 | 74 | return; |
74 | 75 | } |
75 | 76 |
|
@@ -99,10 +100,15 @@ static void load_tbb_symbols(void) { |
99 | 100 | !tbb_ops.pool_identify) { |
100 | 101 | fprintf(stderr, "Could not find symbols in %s.\n", so_name); |
101 | 102 | dlclose(tbb_handle); |
102 | | - Load_tbb_symbols_failed = true; |
| 103 | + Init_tbb_global_state_failed = true; |
103 | 104 | return; |
104 | 105 | } |
105 | 106 |
|
| 107 | +#ifdef UMF_SHARED_LIBRARY |
| 108 | + umf_ba_create_global(); |
| 109 | + atexit(umf_ba_destroy_global); |
| 110 | +#endif |
| 111 | + |
106 | 112 | g_tbb_ops = tbb_ops; |
107 | 113 | } |
108 | 114 |
|
@@ -144,8 +150,8 @@ static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider, |
144 | 150 | .keep_all_memory = false, |
145 | 151 | .reserved = 0}; |
146 | 152 |
|
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) { |
149 | 155 | fprintf(stderr, "loading TBB symbols failed\n"); |
150 | 156 | return UMF_RESULT_ERROR_UNKNOWN; |
151 | 157 | } |
@@ -176,7 +182,7 @@ static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider, |
176 | 182 | } |
177 | 183 |
|
178 | 184 | 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); |
180 | 186 | struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool; |
181 | 187 | g_tbb_ops.pool_destroy(pool_data->tbb_pool); |
182 | 188 | umf_ba_free(pool_data->base_allocator, pool_data); |
|
0 commit comments