mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH 0/4] various cleanups and fixes from the Ruby side
@ 2022-09-03 11:18 Eric Wong
  2022-09-03 11:18 ` [PATCH 1/4] favor _Thread_local under C11 Eric Wong
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Eric Wong @ 2022-09-03 11:18 UTC (permalink / raw)
  To: mwrap-perl

Eric Wong (4):
  favor _Thread_local under C11
  cleanup some FreeBSD-related workarounds
  use macro to quiet uninitialized and unused variable warnings
  workaround breakage from urcu v0.11.4

 Mwrap.xs | 77 ++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 47 insertions(+), 30 deletions(-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] favor _Thread_local under C11
  2022-09-03 11:18 [PATCH 0/4] various cleanups and fixes from the Ruby side Eric Wong
@ 2022-09-03 11:18 ` Eric Wong
  2022-09-03 11:18 ` [PATCH 2/4] cleanup some FreeBSD-related workarounds Eric Wong
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Eric Wong @ 2022-09-03 11:18 UTC (permalink / raw)
  To: mwrap-perl

__thread is a gcc-ism which clang supports, but _Thread_local is
part of C11 and more likely to be supported by future compilers.
---
 Mwrap.xs | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index bb51930..e888a1c 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -26,6 +26,14 @@
 #include <urcu/rculist.h>
 #include "jhash.h"
 
+#if __STDC_VERSION__ >= 201112
+#	define MWRAP_TSD _Thread_local
+#elif defined(__GNUC__)
+#	define MWRAP_TSD __thread
+#else
+#	error _Thread_local nor __thread supported
+#endif
+
 /*
  * Perl doesn't have a GC the same way (C) Ruby does, so no GC count.
  * Instead, the relative age of an object is the number of total bytes
@@ -62,7 +70,7 @@ static int resolving_malloc;
 	} \
 } while (0)
 
-static __thread size_t locating;
+static MWRAP_TSD size_t locating;
 #ifndef PERL_IMPLICIT_CONTEXT
 static size_t *root_locating; /* determines if PL_curcop is our thread */
 #endif
@@ -237,7 +245,7 @@ struct alloc_hdr {
 	size_t size;
 };
 
-static __thread char kbuf[
+static MWRAP_TSD char kbuf[
 	PATH_MAX + INT2STR_MAX + sizeof(struct alloc_hdr) + 2
 ];
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] cleanup some FreeBSD-related workarounds
  2022-09-03 11:18 [PATCH 0/4] various cleanups and fixes from the Ruby side Eric Wong
  2022-09-03 11:18 ` [PATCH 1/4] favor _Thread_local under C11 Eric Wong
@ 2022-09-03 11:18 ` Eric Wong
  2022-09-04  7:01   ` Eric Wong
  2022-09-03 11:18 ` [PATCH 3/4] use macro to quiet uninitialized and unused variable warnings Eric Wong
  2022-09-03 11:18 ` [PATCH 4/4] workaround breakage from urcu v0.11.4 Eric Wong
  3 siblings, 1 reply; 6+ messages in thread
From: Eric Wong @ 2022-09-03 11:18 UTC (permalink / raw)
  To: mwrap-perl

This is to keep up with the Ruby version.
---
 Mwrap.xs | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index e888a1c..3392fee 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -84,9 +84,16 @@ union padded_mutex {
 /* a round-robin pool of mutexes */
 #define MUTEX_NR   (1 << 6)
 #define MUTEX_MASK (MUTEX_NR - 1)
+#ifdef __FreeBSD__
+#  define STATIC_MTX_INIT_OK (0)
+#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
+#endif
 };
 
 static pthread_mutex_t *mutex_assign(void)
@@ -106,12 +113,11 @@ __attribute__((constructor)) static void resolve_malloc(void)
 
 	++locating;
 
-#ifdef __FreeBSD__
 	/*
 	 * 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++) {
@@ -124,19 +130,20 @@ __attribute__((constructor)) static void resolve_malloc(void)
 		/* 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);
+		}
 	}
-#else /* !FreeBSD (tested on GNU/Linux) */
-	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 */
+#endif /* !__FreeBSD__ */
 	err = pthread_atfork(call_rcu_before_fork,
 				call_rcu_after_fork_parent,
 				call_rcu_after_fork_child);

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] use macro to quiet uninitialized and unused variable warnings
  2022-09-03 11:18 [PATCH 0/4] various cleanups and fixes from the Ruby side Eric Wong
  2022-09-03 11:18 ` [PATCH 1/4] favor _Thread_local under C11 Eric Wong
  2022-09-03 11:18 ` [PATCH 2/4] cleanup some FreeBSD-related workarounds Eric Wong
@ 2022-09-03 11:18 ` Eric Wong
  2022-09-03 11:18 ` [PATCH 4/4] workaround breakage from urcu v0.11.4 Eric Wong
  3 siblings, 0 replies; 6+ messages in thread
From: Eric Wong @ 2022-09-03 11:18 UTC (permalink / raw)
  To: mwrap-perl

This allows us to avoid some #ifdefs inside function bodies.
This cleanup comes from the Ruby version.
---
 Mwrap.xs | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index 3392fee..9e9f571 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -153,14 +153,18 @@ __attribute__((constructor)) static void resolve_malloc(void)
 	--locating;
 }
 
+#ifdef NDEBUG
+#define QUIET_CC_WARNING(var) (void)var;
+#else
+#define QUIET_CC_WARNING(var)
+#endif
+
 static void
 mutex_lock(pthread_mutex_t *m)
 {
 	int err = pthread_mutex_lock(m);
 	assert(err == 0);
-#ifdef NDEBUG
-	(void)err; /* quiet gcc warning */
-#endif
+	QUIET_CC_WARNING(err)
 }
 
 static void
@@ -168,9 +172,7 @@ mutex_unlock(pthread_mutex_t *m)
 {
 	int err = pthread_mutex_unlock(m);
 	assert(err == 0);
-#ifdef NDEBUG
-	(void)err; /* quiet gcc warning */
-#endif
+	QUIET_CC_WARNING(err)
 }
 
 #ifndef HAVE_MEMPCPY

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] workaround breakage from urcu v0.11.4
  2022-09-03 11:18 [PATCH 0/4] various cleanups and fixes from the Ruby side Eric Wong
                   ` (2 preceding siblings ...)
  2022-09-03 11:18 ` [PATCH 3/4] use macro to quiet uninitialized and unused variable warnings Eric Wong
@ 2022-09-03 11:18 ` Eric Wong
  3 siblings, 0 replies; 6+ messages in thread
From: Eric Wong @ 2022-09-03 11:18 UTC (permalink / raw)
  To: mwrap-perl

urcu v0.11.4+ introduced commit
7ca7fe9c03 (Make temporary variable in _rcu_dereference non-const, 2021-07-29)
which conflicts with our use of _LGPL_SOURCE.  In retrospect,
CMM_LOAD_SHARED and CMM_STORE_SHARED seem sufficient for our use
of the `totals' cds_lfht pointer since the constructur should always
fire before any threads are running.

This is fixed in urcu v0.12.4 and v0.13.2 (released 2022-08-18)
but I suspect older versions will live on in enterprise/LTS
distros for a long while.

Link: https://lore.kernel.org/lttng-dev/20220809181927.GA3718@dcvr/
---
 Mwrap.xs | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index 9e9f571..9a85f36 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -298,7 +298,7 @@ static struct src_loc *totals_add_rcu(struct src_loc *k)
 	struct cds_lfht *t;
 
 again:
-	t = rcu_dereference(totals);
+	t = CMM_LOAD_SHARED(totals);
 	if (!t) goto out_unlock;
 	cds_lfht_lookup(t, k->hval, loc_eq, k, &iter);
 	cur = cds_lfht_iter_get_node(&iter);
@@ -341,7 +341,7 @@ update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller)
 	static const size_t xlen = sizeof(caller);
 	char *dst;
 	const COP *cop;
-	struct cds_lfht *t = rcu_dereference(totals);
+	struct cds_lfht *t = CMM_LOAD_SHARED(totals);
 
 	if (caa_unlikely(!t)) return 0;
 	if (locating++) goto out; /* do not recurse into another *alloc */
@@ -684,7 +684,7 @@ static void *dump_to_file(struct dump_arg *a)
 
 	++locating;
 	rcu_read_lock();
-	t = rcu_dereference(totals);
+	t = CMM_LOAD_SHARED(totals);
 	if (!t)
 		goto out_unlock;
 	cds_lfht_for_each_entry(t, &iter, l, hnode) {
@@ -805,8 +805,8 @@ BOOT:
 #ifndef PERL_IMPLICIT_CONTEXT
 	root_locating = &locating;
 #endif
-	totals = lfht_new();
-	if (!totals)
+	CMM_STORE_SHARED(totals, lfht_new());
+	if (!CMM_LOAD_SHARED(totals))
 		fprintf(stderr, "failed to allocate totals table\n");
 
 PROTOTYPES: ENABLE
@@ -851,7 +851,7 @@ PREINIT:
 CODE:
 	++locating;
 	rcu_read_lock();
-	t = rcu_dereference(totals);
+	t = CMM_LOAD_SHARED(totals);
 	if (t) {
 		bool err = false;
 
@@ -903,7 +903,7 @@ CODE:
 	uatomic_set(&total_bytes_dec, 0);
 
 	rcu_read_lock();
-	t = rcu_dereference(totals);
+	t = CMM_LOAD_SHARED(totals);
 	cds_lfht_for_each_entry(t, &iter, l, hnode) {
 		uatomic_set(&l->total, 0);
 		uatomic_set(&l->allocations, 0);
@@ -949,7 +949,7 @@ CODE:
 		XSRETURN_UNDEF;
 
 	rcu_read_lock();
-	t = rcu_dereference(totals);
+	t = CMM_LOAD_SHARED(totals);
 	if (!t) goto out_unlock;
 
 	cds_lfht_lookup(t, k->hval, loc_eq, k, &iter);

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/4] cleanup some FreeBSD-related workarounds
  2022-09-03 11:18 ` [PATCH 2/4] cleanup some FreeBSD-related workarounds Eric Wong
@ 2022-09-04  7:01   ` Eric Wong
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Wong @ 2022-09-04  7:01 UTC (permalink / raw)
  To: Eric Wong

Eric Wong <mwrap-perl@80x24.org> wrote:
> diff --git a/Mwrap.xs b/Mwrap.xs
> index e888a1c..3392fee 100644
> --- a/Mwrap.xs
> +++ b/Mwrap.xs
> @@ -124,19 +130,20 @@ __attribute__((constructor)) static void resolve_malloc(void)
>  		/* 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);
> +		}
>  	}
> -#else /* !FreeBSD (tested on GNU/Linux) */
> -	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 */
> +#endif /* !__FreeBSD__ */
>  	err = pthread_atfork(call_rcu_before_fork,
>  				call_rcu_after_fork_parent,
>  				call_rcu_after_fork_child);

Will squash this in so that it actually compiles :x

diff --git a/Mwrap.xs b/Mwrap.xs
index 3392fee..bec4664 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -142,8 +142,8 @@ __attribute__((constructor)) static void resolve_malloc(void)
 				"\t%p %p\n", real_malloc, real_free);
 			_exit(1);
 		}
-	}
 #endif /* !__FreeBSD__ */
+	}
 	err = pthread_atfork(call_rcu_before_fork,
 				call_rcu_after_fork_parent,
 				call_rcu_after_fork_child);

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-09-04  7:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-03 11:18 [PATCH 0/4] various cleanups and fixes from the Ruby side Eric Wong
2022-09-03 11:18 ` [PATCH 1/4] favor _Thread_local under C11 Eric Wong
2022-09-03 11:18 ` [PATCH 2/4] cleanup some FreeBSD-related workarounds Eric Wong
2022-09-04  7:01   ` Eric Wong
2022-09-03 11:18 ` [PATCH 3/4] use macro to quiet uninitialized and unused variable warnings Eric Wong
2022-09-03 11:18 ` [PATCH 4/4] workaround breakage from urcu v0.11.4 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).