mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH] freebsd: fix infinite recursion during startup
@ 2022-12-09  3:14 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2022-12-09  3:14 UTC (permalink / raw)
  To: mwrap-perl

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-12-09  3:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-09  3:14 [PATCH] freebsd: fix infinite recursion during startup Eric Wong

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).