about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <mwrap-perl@80x24.org>2022-12-09 03:14:33 +0000
committerEric Wong <e@80x24.org>2022-12-09 03:17:50 +0000
commit4a9dc7275992ac31fa74f0ea7b58dfcd2b453939 (patch)
tree01c09aa62f59cffd18acfefa8bc66fc23e7a7b10
parentb44d333bb1a8067680d87b287554770e626cbb86 (diff)
downloadmwrap-4a9dc7275992ac31fa74f0ea7b58dfcd2b453939.tar.gz
pthread_mutex_lock needs malloc under FreeBSD :<
-rw-r--r--mymalloc.h14
1 files changed, 12 insertions, 2 deletions
diff --git a/mymalloc.h b/mymalloc.h
index 72cb013..4f504b7 100644
--- a/mymalloc.h
+++ b/mymalloc.h
@@ -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;
 }