mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH 3/6] block signals when spawning URCU-related threads
Date: Tue, 15 Nov 2022 19:33:38 +0000	[thread overview]
Message-ID: <20221115193341.3948245-4-e@80x24.org> (raw)
In-Reply-To: <20221115193341.3948245-1-e@80x24.org>

rculfhash, call_rcu, and urcu-bp will spawn background
threads.  Ensure we spawn all of these threads while signals
are blocked since the underlying codebase may block signals
and rely on signalfd.
---
 Mwrap.xs | 127 ++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 74 insertions(+), 53 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index e87fe30..f441ca8 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -21,6 +21,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <pthread.h>
+#include <signal.h>
 #include <urcu-bp.h>
 #include <urcu/rculfhash.h>
 #include <urcu/rculist.h>
@@ -101,60 +102,9 @@ static pthread_mutex_t *mutex_assign(void)
 	return &mutexes[uatomic_add_return(&mutex_i, 1) & MUTEX_MASK].mtx;
 }
 
-static struct cds_lfht *
-lfht_new(void)
+static struct cds_lfht *lfht_new(void)
 {
-	return cds_lfht_new(16384, 1, 0, CDS_LFHT_AUTO_RESIZE, 0);
-}
-
-__attribute__((constructor)) static void resolve_malloc(void)
-{
-	int err;
-
-	++locating;
-
-	/*
-	 * PTHREAD_MUTEX_INITIALIZER on FreeBSD means lazy initialization,
-	 * which happens at pthread_mutex_lock, and that calls calloc
-	 */
-	if (!STATIC_MTX_INIT_OK) {
-		size_t i;
-
-		for (i = 0; i < MUTEX_NR; i++) {
-			err = pthread_mutex_init(&mutexes[i].mtx, 0);
-			if (err) {
-				fprintf(stderr, "error: %s\n", strerror(err));
-				_exit(1);
-			}
-		}
-		/* initialize mutexes used by urcu-bp */
-		rcu_read_lock();
-		rcu_read_unlock();
-#ifndef __FreeBSD__
-	} else {
-		if (!real_malloc) {
-			resolving_malloc = 1;
-			real_malloc = dlsym(RTLD_NEXT, "malloc");
-		}
-		real_free = dlsym(RTLD_NEXT, "free");
-		if (!real_malloc || !real_free) {
-			fprintf(stderr, "missing malloc/aligned_alloc/free\n"
-				"\t%p %p\n", real_malloc, real_free);
-			_exit(1);
-		}
-#endif /* !__FreeBSD__ */
-	}
-	CMM_STORE_SHARED(totals, lfht_new());
-	if (!CMM_LOAD_SHARED(totals))
-		fprintf(stderr, "failed to allocate totals table\n");
-
-	err = pthread_atfork(call_rcu_before_fork,
-				call_rcu_after_fork_parent,
-				call_rcu_after_fork_child);
-	if (err)
-		fprintf(stderr, "pthread_atfork failed: %s\n", strerror(err));
-	page_size = sysconf(_SC_PAGESIZE);
-	--locating;
+	return cds_lfht_new(8192, 1, 0, CDS_LFHT_AUTO_RESIZE, 0);
 }
 
 #ifdef NDEBUG
@@ -807,6 +757,77 @@ out:
 	--locating;
 }
 
+__attribute__((constructor)) static void mwrap_ctor(void)
+{
+	sigset_t set, old;
+	struct alloc_hdr *h;
+	int err;
+
+	++locating;
+
+	/* block signals */
+	err = sigfillset(&set);
+	assert(err == 0);
+	err = pthread_sigmask(SIG_SETMASK, &set, &old);
+	assert(err == 0);
+
+	page_size = sysconf(_SC_PAGESIZE);
+	/*
+	 * PTHREAD_MUTEX_INITIALIZER on FreeBSD means lazy initialization,
+	 * which happens at pthread_mutex_lock, and that calls calloc
+	 */
+	if (!STATIC_MTX_INIT_OK) {
+		size_t i;
+
+		for (i = 0; i < MUTEX_NR; i++) {
+			err = pthread_mutex_init(&mutexes[i].mtx, 0);
+			if (err) {
+				fprintf(stderr, "error: %s\n", strerror(err));
+				_exit(1);
+			}
+		}
+		/* initialize mutexes used by urcu-bp */
+		rcu_read_lock();
+		rcu_read_unlock();
+#ifndef __FreeBSD__
+	} else {
+		if (!real_malloc) {
+			resolving_malloc = 1;
+			real_malloc = dlsym(RTLD_NEXT, "malloc");
+		}
+		real_free = dlsym(RTLD_NEXT, "free");
+		if (!real_malloc || !real_free) {
+			fprintf(stderr, "missing malloc/aligned_alloc/free\n"
+				"\t%p %p\n", real_malloc, real_free);
+			_exit(1);
+		}
+#endif /* !__FreeBSD__ */
+	}
+	CMM_STORE_SHARED(totals, lfht_new());
+	if (!CMM_LOAD_SHARED(totals)) {
+		fprintf(stderr, "failed to allocate `totals' table\n");
+		abort();
+	}
+	h = real_malloc(sizeof(struct alloc_hdr));
+	if (h) { /* force call_rcu to start background thread */
+		h->real = h;
+		call_rcu(&h->as.dead, free_hdr_rcu);
+	} else
+		fprintf(stderr, "malloc failed: %s\n", strerror(errno));
+
+	/* start more background threads before unblocking signals */
+	cds_lfht_resize(CMM_LOAD_SHARED(totals), 16384);
+	err = pthread_sigmask(SIG_SETMASK, &old, NULL);
+	assert(err == 0);
+	err = pthread_atfork(call_rcu_before_fork,
+				call_rcu_after_fork_parent,
+				call_rcu_after_fork_child);
+	if (err)
+		fprintf(stderr, "pthread_atfork failed: %s\n", strerror(err));
+
+	--locating;
+}
+
 MODULE = Devel::Mwrap	PACKAGE = Devel::Mwrap	PREFIX = mwrap_
 
 BOOT:

  parent reply	other threads:[~2022-11-15 19:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-15 19:33 [PATCH 0/6] bugfixes, workarounds, and built-in malloc Eric Wong
2022-11-15 19:33 ` [PATCH 1/6] test cfree and aligned_alloc aliases Eric Wong
2022-11-15 19:33 ` [PATCH 2/6] Devel::Mwrap::reset is a no-op if `totals' not initialized Eric Wong
2022-11-15 19:33 ` Eric Wong [this message]
2022-11-15 19:33 ` [PATCH 4/6] document of kbuf allocation size Eric Wong
2022-11-15 19:33 ` [PATCH 5/6] mwrap-perl: use grep and fix regexp Eric Wong
2022-11-15 19:33 ` [PATCH 6/6] split out mwrap_core.h and use provide our own malloc Eric Wong

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=20221115193341.3948245-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=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).