diff options
-rw-r--r-- | mymalloc.h | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -147,9 +147,18 @@ ATTR_COLD static void atfork_child(void) call_rcu_after_fork_child(); } +#if defined(__GLIBC__) +# define FIRST_TIME 0 +#else /* pthread_mutex_lock calls malloc on FreeBSD */ + static int once; +# define FIRST_TIME (uatomic_cmpxchg(&once, 0, 1)) +#endif + static __attribute__((noinline)) mstate mstate_acquire_harder(void) { - CHECK(int, 0, pthread_mutex_lock(&global_mtx)); + bool do_lock = FIRST_TIME ? false : true; + if (do_lock) + CHECK(int, 0, pthread_mutex_lock(&global_mtx)); if (cds_list_empty(&arenas_unused)) { ms_tsd = create_mspace(0, 0); ms_tsd->seg.sflags = EXTERN_BIT | USE_MMAP_BIT; @@ -163,7 +172,8 @@ static __attribute__((noinline)) mstate mstate_acquire_harder(void) if (!tlskey) CHECK(int, 0, pthread_key_create(&tlskey, mstate_tsd_dtor)); - CHECK(int, 0, pthread_mutex_unlock(&global_mtx)); + if (do_lock) + CHECK(int, 0, pthread_mutex_unlock(&global_mtx)); CHECK(int, 0, pthread_setspecific(tlskey, ms_tsd)); return ms_tsd; } |