public inbox for
 help / color / mirror / Atom feed
Subject: [PATCH v2] libgo: Portable access to thread ID in struct sigevent
Date: Fri, 23 Sep 2022 15:59:14 +0200	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

From: Sören Tempel <>

Tested on x86_64 Arch Linux (glibc) and Alpine Linux (musl libc).

Previously, libgo relied on the _sigev_un implementation-specific
field in struct sigevent, which is only available on glibc. This
patch uses the sigev_notify_thread_id macro instead which is mandated
by timer_create(2). In theory, this should work with any libc
implementation for Linux. Unfortunately, there is an open glibc bug
as glibc does not define this macro. For this reason, a glibc-specific
workaround is required. Other libcs (such as musl) define the macro
and don't require the workaround.

This makes go_signal compatible with musl libc.


Signed-off-by: Sören Tempel <>
Changes since v1: Add workaround for glibc.

 libgo/go/runtime/os_linux.go |  4 +++-
 libgo/runtime/go-signal.c    | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/libgo/go/runtime/os_linux.go b/libgo/go/runtime/os_linux.go
index 96fb1788..6653d85e 100644
--- a/libgo/go/runtime/os_linux.go
+++ b/libgo/go/runtime/os_linux.go
@@ -22,6 +22,8 @@ type mOS struct {
 	profileTimerValid uint32
+func setProcID(uintptr, int32)
 func getProcID() uint64 {
 	return uint64(gettid())
@@ -365,7 +367,7 @@ func setThreadCPUProfiler(hz int32) {
 	var sevp _sigevent
 	sevp.sigev_notify = _SIGEV_THREAD_ID
 	sevp.sigev_signo = _SIGPROF
-	*((*int32)(unsafe.Pointer(&sevp._sigev_un))) = int32(mp.procid)
+	setProcID(uintptr(unsafe.Pointer(&sevp)), int32(mp.procid))
 	ret := timer_create(_CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid)
 	if ret != 0 {
 		// If we cannot create a timer for this M, leave profileTimerValid false
diff --git a/libgo/runtime/go-signal.c b/libgo/runtime/go-signal.c
index 528d9b6d..c56350cc 100644
--- a/libgo/runtime/go-signal.c
+++ b/libgo/runtime/go-signal.c
@@ -16,6 +16,11 @@
   #define SA_RESTART 0
+// Workaround for
+#if __linux__ && !defined(sigev_notify_thread_id)
+  #define sigev_notify_thread_id _sigev_un._tid
 extern void __splitstack_getcontext(void *context[10]);
@@ -183,6 +188,16 @@ setSigactionHandler(struct sigaction* sa, uintptr handler)
 	sa->sa_sigaction = (void*)(handler);
+void setProcID(uintptr_t, int32_t)
+	__asm__ (GOSYM_PREFIX "runtime.setProcID");
+setProcID(uintptr_t ptr, int32_t v)
+	struct sigevent *s = (void *)ptr;
+	s->sigev_notify_thread_id = v;
 // C code to fetch values from the siginfo_t and ucontext_t pointers
 // passed to a signal handler.

  parent reply	other threads:[~2022-09-23 13:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <>
     [not found] ` <>
2022-08-20 10:31   ` [PATCH] libgo: Access to glibc-specific field " Sören Tempel
2022-09-05 12:31     ` Sören Tempel
2022-09-23 13:59     ` soeren [this message]
2022-09-27 16:31       ` [PATCH v2] libgo: Portable access to thread ID " Ian Lance Taylor

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:

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

  git send-email \ \ \ \ \ \

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