Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: parts of a thread stack mapping, pthread_main_np()



On Sat, Apr 25, 2026 at 06:15:06AM +0100, Jason Thorpe wrote:
> 
> 
> > On Apr 24, 2026, at 11:33 PM, Martin Husemann <martin%duskware.de@localhost> wrote:
> > 
> > On Sat, Apr 25, 2026 at 12:28:36AM +0200, Thomas Klausner wrote:
> >> I also noticed that NetBSD does not provide pthread_main_np() to
> >> identify the main thread of a program; which would be helpful for some
> >> code since the stack layouts are different for main and other threads.
> >> 
> >> Is there a reason for not providing this, or should we just add it?
> > 
> > My vote: we should just add it.
> 
> Yah, I would agree.

Here's my suggested patch (I haven't run a full build with this yet,
but my test program works).
 Thomas
Index: lib/libpthread/Makefile
===================================================================
RCS file: /cvsroot/src/lib/libpthread/Makefile,v
retrieving revision 1.103
diff -u -r1.103 Makefile
--- lib/libpthread/Makefile	23 Nov 2025 22:11:41 -0000	1.103
+++ lib/libpthread/Makefile	26 Apr 2026 21:12:18 -0000
@@ -164,7 +164,7 @@
 	pthread_getname_np.3 \
 	pthread_getspecific.3 pthread_join.3 \
 	pthread_key_create.3 pthread_kill.3 \
-	pthread_mutex.3 pthread_mutexattr.3 \
+	pthread_main_np.3 pthread_mutex.3 pthread_mutexattr.3 \
 	pthread_once.3 pthread_rwlock.3 pthread_rwlockattr.3 \
 	pthread_schedparam.3 pthread_self.3 \
 	pthread_sigmask.3 pthread_spin.3 \
Index: lib/libpthread/pthread.c
===================================================================
RCS file: /cvsroot/src/lib/libpthread/pthread.c,v
retrieving revision 1.190
diff -u -r1.190 pthread.c
--- lib/libpthread/pthread.c	27 Oct 2025 16:29:15 -0000	1.190
+++ lib/libpthread/pthread.c	26 Apr 2026 21:12:18 -0000
@@ -137,6 +137,7 @@
 size_t	pthread__pagesize;
 static struct __pthread_st *pthread__main;
 static size_t __pthread_st_size;
+static pthread_t __pthread_main_thread;
 
 int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
 
@@ -273,6 +274,8 @@
 		err(EXIT_FAILURE, "_lwp_ctl");
 	}
 
+	__pthread_main_thread = first;
+
 	/* Start subsystems */
 	PTHREAD_MD_INIT
 
@@ -1443,3 +1446,13 @@
 	}
 	return (pri < min || pri > max) ? EINVAL : 0;
 }
+
+int
+pthread_main_np(void)
+{
+	if (__predict_false(__uselibcstub))
+		return -1;
+	if (pthread_self() == __pthread_main_thread)
+		return 1;
+	return 0;
+}
Index: lib/libpthread/pthread.h
===================================================================
RCS file: /cvsroot/src/lib/libpthread/pthread.h,v
retrieving revision 1.45
diff -u -r1.45 pthread.h
--- lib/libpthread/pthread.h	27 Oct 2025 16:29:15 -0000	1.45
+++ lib/libpthread/pthread.h	26 Apr 2026 21:12:18 -0000
@@ -165,6 +165,8 @@
 
 unsigned int	pthread_curcpu_np(void);
 
+int	pthread_main_np(void);
+
 int	pthread_getcpuclockid(pthread_t, clockid_t *);
 
 struct pthread_cleanup_store {
Index: lib/libpthread/pthread_main_np.3
===================================================================
RCS file: lib/libpthread/pthread_main_np.3
diff -N lib/libpthread/pthread_main_np.3
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ lib/libpthread/pthread_main_np.3	26 Apr 2026 21:12:18 -0000
@@ -0,0 +1,57 @@
+.\" $NetBSD$
+.\"
+.\" Copyright (c) 2026 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Thomas Klausner.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd April 26, 2026
+.Dt PTHREAD_MAIN_NP 3
+.Os
+.Sh NAME
+.Nm pthread_main_np
+.Nd identify the initial thread
+.Sh LIBRARY
+.Lb libpthread
+.Sh SYNOPSIS
+.In pthread.h
+.Ft int
+.Fn pthread_main_np "void"
+.Sh DESCRIPTION
+The
+.Fn pthread_main_np
+function identifies the initial (main) thread.
+.Sh RETURN VALUES
+The
+.Fn pthread_main_np
+function returns 1 if the calling thread is the main thread,
+0 if not, and \-1 if threading has not been initialized.
+.Sh SEE ALSO
+.Xr pthread 3 ,
+.Xr pthread_self 3
+.Sh STANDARDS
+The
+.Fn pthread_main_np
+function is a non-standard extensions.
Index: lib/libpthread/pthread_mi.expsym
===================================================================
RCS file: /cvsroot/src/lib/libpthread/pthread_mi.expsym,v
retrieving revision 1.5
diff -u -r1.5 pthread_mi.expsym
--- lib/libpthread/pthread_mi.expsym	27 Oct 2025 16:29:15 -0000	1.5
+++ lib/libpthread/pthread_mi.expsym	26 Apr 2026 21:12:18 -0000
@@ -200,6 +200,7 @@
 pthread_key_delete
 pthread_keys_max
 pthread_kill
+pthread_main_np
 pthread_mutex_destroy
 pthread_mutex_getprioceiling
 pthread_mutex_held_np
Index: lib/libpthread/shlib_version
===================================================================
RCS file: /cvsroot/src/lib/libpthread/shlib_version,v
retrieving revision 1.24
diff -u -r1.24 shlib_version
--- lib/libpthread/shlib_version	29 Nov 2025 14:39:36 -0000	1.24
+++ lib/libpthread/shlib_version	26 Apr 2026 21:12:18 -0000
@@ -25,4 +25,4 @@
 # 20251122 LDADD+= -Wl,-z,nodelete
 #
 major=1
-minor=6
+minor=7
Index: distrib/sets/lists/base/shl.mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/base/shl.mi,v
retrieving revision 1.1036
diff -u -r1.1036 shl.mi
--- distrib/sets/lists/base/shl.mi	8 Apr 2026 21:03:24 -0000	1.1036
+++ distrib/sets/lists/base/shl.mi	26 Apr 2026 21:12:19 -0000
@@ -85,7 +85,7 @@
 ./lib/libprop.so.1.3				base-sys-shlib		dynamicroot
 ./lib/libpthread.so				base-sys-shlib		dynamicroot
 ./lib/libpthread.so.1				base-sys-shlib		dynamicroot
-./lib/libpthread.so.1.6				base-sys-shlib		dynamicroot
+./lib/libpthread.so.1.7				base-sys-shlib		dynamicroot
 ./lib/libradius.so				base-sys-shlib		dynamicroot
 ./lib/libradius.so.5				base-sys-shlib		dynamicroot
 ./lib/libradius.so.5.0				base-sys-shlib		dynamicroot
@@ -537,7 +537,7 @@
 ./usr/lib/libprop.so.1.3			base-sys-shlib		compatfile
 ./usr/lib/libpthread.so				base-sys-shlib		compatfile
 ./usr/lib/libpthread.so.1			base-sys-shlib		compatfile
-./usr/lib/libpthread.so.1.6			base-sys-shlib		compatfile
+./usr/lib/libpthread.so.1.7			base-sys-shlib		compatfile
 ./usr/lib/libpthread_dbg.so			base-obsolete		obsolete
 ./usr/lib/libpuffs.so				base-puffs-shlib	compatfile
 ./usr/lib/libpuffs.so.2				base-puffs-shlib	compatfile
Index: distrib/sets/lists/comp/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/comp/mi,v
retrieving revision 1.2522
diff -u -r1.2522 mi
--- distrib/sets/lists/comp/mi	9 Apr 2026 14:42:15 -0000	1.2522
+++ distrib/sets/lists/comp/mi	26 Apr 2026 21:12:31 -0000
@@ -10466,6 +10466,7 @@
 ./usr/share/man/cat3/pthread_key_create.0	comp-c-catman		.cat
 ./usr/share/man/cat3/pthread_key_delete.0	comp-c-catman		.cat
 ./usr/share/man/cat3/pthread_kill.0		comp-c-catman		.cat
+./usr/share/man/cat3/pthread_main_np.0		comp-c-catman		.cat
 ./usr/share/man/cat3/pthread_mutex.0		comp-c-catman		.cat
 ./usr/share/man/cat3/pthread_mutex_destroy.0	comp-c-catman		.cat
 ./usr/share/man/cat3/pthread_mutex_getprioceiling.0	comp-c-catman	.cat
@@ -19427,6 +19428,7 @@
 ./usr/share/man/html3/pthread_key_create.html	comp-c-htmlman		html
 ./usr/share/man/html3/pthread_key_delete.html	comp-c-htmlman		html
 ./usr/share/man/html3/pthread_kill.html		comp-c-htmlman		html
+./usr/share/man/html3/pthread_main_np.html	comp-c-htmlman		html
 ./usr/share/man/html3/pthread_mutex.html	comp-c-htmlman		html
 ./usr/share/man/html3/pthread_mutex_destroy.html	comp-c-htmlman	html
 ./usr/share/man/html3/pthread_mutex_getprioceiling.html	comp-c-htmlman	html
@@ -28384,6 +28386,7 @@
 ./usr/share/man/man3/pthread_key_create.3	comp-c-man		.man
 ./usr/share/man/man3/pthread_key_delete.3	comp-c-man		.man
 ./usr/share/man/man3/pthread_kill.3		comp-c-man		.man
+./usr/share/man/man3/pthread_main_np.3		comp-c-man		.man
 ./usr/share/man/man3/pthread_mutex.3		comp-c-man		.man
 ./usr/share/man/man3/pthread_mutex_destroy.3	comp-c-man		.man
 ./usr/share/man/man3/pthread_mutex_getprioceiling.3	comp-c-man	.man
Index: distrib/sets/lists/debug/shl.mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/debug/shl.mi,v
retrieving revision 1.396
diff -u -r1.396 shl.mi
--- distrib/sets/lists/debug/shl.mi	8 Apr 2026 21:03:24 -0000	1.396
+++ distrib/sets/lists/debug/shl.mi	26 Apr 2026 21:12:31 -0000
@@ -26,7 +26,7 @@
 ./usr/libdata/debug/lib/libppath.so.0.0.debug			comp-sys-debug	debug,dynamicroot
 ./usr/libdata/debug/lib/libprop.so.1.2.debug			comp-obsolete	obsolete
 ./usr/libdata/debug/lib/libprop.so.1.3.debug			comp-sys-debug	debug,dynamicroot
-./usr/libdata/debug/lib/libpthread.so.1.6.debug			comp-sys-debug	debug,dynamicroot
+./usr/libdata/debug/lib/libpthread.so.1.7.debug			comp-sys-debug	debug,dynamicroot
 ./usr/libdata/debug/lib/libradius.so.5.0.debug			comp-sys-debug	debug,dynamicroot
 ./usr/libdata/debug/lib/librumpclient.so.0.0.debug		comp-rump-debug	debug,dynamicroot,rump
 ./usr/libdata/debug/lib/librumpres.so.0.0.debug			comp-rump-debug	debug,dynamicroot,rump
@@ -190,7 +190,7 @@
 ./usr/libdata/debug/usr/lib/libproc.so.1.0.debug		comp-sys-debug	debug,compatfile,dtrace
 ./usr/libdata/debug/usr/lib/libprop.so.1.2.debug		comp-obsolete   obsolete
 ./usr/libdata/debug/usr/lib/libprop.so.1.3.debug		comp-sys-debug	debug,compatfile
-./usr/libdata/debug/usr/lib/libpthread.so.1.6.debug		comp-sys-debug	debug,compatfile
+./usr/libdata/debug/usr/lib/libpthread.so.1.7.debug		comp-sys-debug	debug,compatfile
 ./usr/libdata/debug/usr/lib/libpuffs.so.2.0.debug		comp-puffs-debug	debug,compatfile
 ./usr/libdata/debug/usr/lib/libquadmath.so.0.0.debug		comp-sys-debug	quadmath,debug,compatfile,gcc=14
 ./usr/libdata/debug/usr/lib/libquota.so.1.0.debug		comp-sys-debug	debug,compatfile


Home | Main Index | Thread Index | Old Index