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 1/7] core: shrink src_loc 8 bytes on x86-64
Date: Mon, 19 Dec 2022 11:19:16 +0000	[thread overview]
Message-ID: <20221219111922.1079128-2-e@80x24.org> (raw)
In-Reply-To: <20221219111922.1079128-1-e@80x24.org>

We can rely on the stable value of ->loc_hash to assign the
mutex at when it requires locking rather than relying on a
monotonically increasing counter.
---
 mwrap_core.h | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/mwrap_core.h b/mwrap_core.h
index 7681ca5..7be0a7a 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -93,7 +93,6 @@ union padded_mutex {
 #else /* only tested on Linux + glibc */
 #  define STATIC_MTX_INIT_OK (1)
 #endif
-static size_t mutex_i;
 static union padded_mutex mutexes[MUTEX_NR] = {
 #if STATIC_MTX_INIT_OK
 	[0 ... (MUTEX_NR-1)].mtx = PTHREAD_MUTEX_INITIALIZER
@@ -105,11 +104,6 @@ static union padded_mutex mutexes[MUTEX_NR] = {
 static_assert(UINT32_MAX > PATH_MAX, "UINT32_MAX > PATH_MAX");
 #endif
 
-static pthread_mutex_t *mutex_assign(void)
-{
-	return &mutexes[uatomic_add_return(&mutex_i, 1) & MUTEX_MASK].mtx;
-}
-
 static struct cds_lfht *lfht_new(size_t size)
 {
 	return cds_lfht_new(size, 1, 0, CDS_LFHT_AUTO_RESIZE, 0);
@@ -163,7 +157,6 @@ struct src_file {
 
 /* allocated via real_malloc, immortal for safety reasons */
 struct src_loc {
-	pthread_mutex_t *mtx;
 	size_t total;
 	size_t freed_bytes;
 	size_t allocations;
@@ -309,7 +302,6 @@ again:
 		if (!l) return l;
 		memcpy(l, k, n);
 		l->freed_bytes = 0;
-		l->mtx = mutex_assign();
 		l->age_total = 0;
 		l->max_lifespan = 0;
 		l->freed_bytes = 0;
@@ -470,6 +462,13 @@ static void free_hdr_rcu(struct rcu_head *dead)
 	real_free(h->real);
 }
 
+static pthread_mutex_t *src_loc_mutex_lock(const struct src_loc *l)
+{
+	pthread_mutex_t *mtx = &mutexes[l->loc_hash & MUTEX_MASK].mtx;
+	CHECK(int, 0, pthread_mutex_lock(mtx));
+	return mtx;
+}
+
 void free(void *p)
 {
 	if (p) {
@@ -485,11 +484,11 @@ void free(void *p)
 			uatomic_inc(&l->frees);
 			uatomic_add(&l->age_total, age);
 
-			CHECK(int, 0, pthread_mutex_lock(l->mtx));
+			pthread_mutex_t *mtx = src_loc_mutex_lock(l);
 			cds_list_del_rcu(&h->anode);
 			if (age > l->max_lifespan)
 				l->max_lifespan = age;
-			CHECK(int, 0, pthread_mutex_unlock(l->mtx));
+			CHECK(int, 0, pthread_mutex_unlock(mtx));
 
 			call_rcu(&h->as.dead, free_hdr_rcu);
 		} else {
@@ -509,9 +508,9 @@ alloc_insert_rcu(struct src_loc *l, struct alloc_hdr *h, size_t size,
 	h->as.live.loc = l;
 	h->as.live.gen = generation;
 	if (l) {
-		CHECK(int, 0, pthread_mutex_lock(l->mtx));
+		pthread_mutex_t *mtx = src_loc_mutex_lock(l);
 		cds_list_add_rcu(&h->anode, &l->allocs);
-		CHECK(int, 0, pthread_mutex_unlock(l->mtx));
+		CHECK(int, 0, pthread_mutex_unlock(mtx));
 	}
 }
 

  reply	other threads:[~2022-12-19 11:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-19 11:19 [PATCH 0/7] shrinkage Eric Wong
2022-12-19 11:19 ` Eric Wong [this message]
2022-12-19 11:19 ` [PATCH 2/7] core: shrink src_loc by another 8 bytes on x86-64 Eric Wong
2022-12-19 11:19 ` [PATCH 3/7] core: drop FreeBSD-specific mutex initialization Eric Wong
2022-12-19 11:19 ` [PATCH 4/7] calloc: consolidate ENOMEM handling Eric Wong
2022-12-19 11:19 ` [PATCH 5/7] *alloc: limit scope of RCU lock Eric Wong
2022-12-19 11:19 ` [PATCH 6/7] core: simplify callers of alloc_insert_rcu Eric Wong
2022-12-19 11:19 ` [PATCH 7/7] README: add some caveats (signalfd usage w/ current URCU) Eric Wong
2022-12-19 22:57   ` [WIP] attempt to workaround older URCU for signalfd 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=20221219111922.1079128-2-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).