From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 62B781F601 for ; Fri, 9 Dec 2022 03:13:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1670555639; bh=eMP4gXEYsKGoqfcVLtGXb/zD+MpWcyJyBXUm6eQaQC0=; h=From:To:Subject:Date:From; b=Ki3n6WUO2qaf/tSsHgDXnh/DYUOgjH91ug7e/0FeCIs7OfH4gl4FidGh/DwZ9+dEr OukM42ZQchSjyERheE2oSkTdbGy9RTj5BGGS4wA853wQp6otKz/OUOuiGdWjXBoxA5 zw+wt0umDWo/pNLRlMug9YPAF/GE9Wvu4qzJBLrk= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH] freebsd: fix infinite recursion during startup Date: Fri, 9 Dec 2022 03:14:33 +0000 Message-Id: <20221209031433.8311-1-mwrap-perl@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: pthread_mutex_lock needs malloc under FreeBSD :< --- mymalloc.h | 14 ++++++++++++-- 1 file 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; }