mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <mwrap-perl@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH] freebsd: fix infinite recursion during startup
Date: Fri,  9 Dec 2022 03:14:33 +0000	[thread overview]
Message-ID: <20221209031433.8311-1-mwrap-perl@80x24.org> (raw)

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;
 }

                 reply	other threads:[~2022-12-09  3:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221209031433.8311-1-mwrap-perl@80x24.org \
    --to=mwrap-perl@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).