Hi.

I sent a diff originally to a smaller audience but finally decided to post
it in the mailing list.
So it is to introduce the pthread_set_name_np's counterpart so
pthread_get_name_np.
Some softwares use it (NSPR for example) and anyway internally in my
workplace we have multiplatform needing this feature would be nice having
this for OpenBSD as well :-)

Let me know what you think.

Kind regards.
Index: lib/libc/stdlib/malloc.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.228
diff -u -p -r1.228 malloc.c
--- lib/libc/stdlib/malloc.c    10 Jul 2017 09:44:16 -0000      1.228
+++ lib/libc/stdlib/malloc.c    15 Jul 2017 19:54:00 -0000
@@ -23,7 +23,7 @@
  * can buy me a beer in return. Poul-Henning Kamp
  */
 
-/* #define MALLOC_STATS */
+#define MALLOC_STATS
 
 #include <sys/types.h>
 #include <sys/param.h> /* PAGE_SHIFT ALIGN */
Index: lib/libpthread/include/pthread_np.h
===================================================================
RCS file: /cvs/src/lib/libpthread/include/pthread_np.h,v
retrieving revision 1.11
diff -u -p -r1.11 pthread_np.h
--- lib/libpthread/include/pthread_np.h 22 Mar 2012 17:21:36 -0000      1.11
+++ lib/libpthread/include/pthread_np.h 15 Jul 2017 19:54:00 -0000
@@ -46,6 +46,7 @@ __BEGIN_DECLS
 int pthread_mutexattr_getkind_np(pthread_mutexattr_t);
 int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int);
 void pthread_set_name_np(pthread_t, const char *);
+void pthread_get_name_np(pthread_t, char *, size_t);
 int pthread_stackseg_np(pthread_t, stack_t *);
 int pthread_main_np(void);
 __END_DECLS
Index: lib/libpthread/man/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libpthread/man/Makefile.inc,v
retrieving revision 1.36
diff -u -p -r1.36 Makefile.inc
--- lib/libpthread/man/Makefile.inc     30 Mar 2016 06:38:42 -0000      1.36
+++ lib/libpthread/man/Makefile.inc     15 Jul 2017 19:54:00 -0000
@@ -31,6 +31,7 @@ MAN+= \
        pthread_detach.3 \
        pthread_equal.3 \
        pthread_exit.3 \
+       pthread_get_name_np.3 \
        pthread_getconcurrency.3 \
        pthread_getcpuclockid.3 \
        pthread_getspecific.3 \
Index: lib/libpthread/man/pthread_get_name_np.3
===================================================================
RCS file: lib/libpthread/man/pthread_get_name_np.3
diff -N lib/libpthread/man/pthread_get_name_np.3
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/libpthread/man/pthread_get_name_np.3    15 Jul 2017 19:54:00 -0000
@@ -0,0 +1,29 @@
+.Dd $Mdocdate: July 15 2017 $
+.Dt PTHREAD_GET_NAME_NP 3
+.Os
+.Sh NAME
+.Nm pthread_get_name_np
+.Nd get the name of a thread
+.Sh SYNOPSIS
+.In pthread.h
+.In pthread_np.h
+.Ft void
+.Fn pthread_get_name_np "pthread_t thread" "char *name" "size_t len"
+.Sh DESCRIPTION
+The
+.Fn pthread_get_name_np
+function retrieves
+.Fa name associated
+with
+.Fa thread .
+.Pp
+If pthread_set_name_np was not called for this
+.Fa thread
+the name parameter will be empty.
+.Sh SEE ALSO
+.Xr pthreads 3
+.Sh STANDARDS
+The
+.Fn pthread_get_name_np
+function is non-portable and may not be supported with the above
+semantics on other POSIX systems.
Index: lib/libpthread/man/pthreads.3
===================================================================
RCS file: /cvs/src/lib/libpthread/man/pthreads.3,v
retrieving revision 1.41
diff -u -p -r1.41 pthreads.3
--- lib/libpthread/man/pthreads.3       16 Jul 2013 15:21:11 -0000      1.41
+++ lib/libpthread/man/pthreads.3       15 Jul 2017 19:54:00 -0000
@@ -169,6 +169,8 @@ The functions available are as follows:
 Identify the main thread.
 .It Fn pthread_set_name_np
 Set the name of a thread.
+.It Fn pthread_get_name_np
+Get the name of a thread
 .It Fn pthread_stackseg_np
 Return stack size and location.
 .It Fn pthread_yield
@@ -421,6 +423,7 @@ with larger numbers generating more verb
 .Xr pthread_detach 3 ,
 .Xr pthread_equal 3 ,
 .Xr pthread_exit 3 ,
+.Xr pthread_get_name_np 3 ,
 .Xr pthread_getcpuclockid 3 ,
 .Xr pthread_getspecific 3 ,
 .Xr pthread_join 3 ,
Index: lib/librthread/Symbols.map
===================================================================
RCS file: /cvs/src/lib/librthread/Symbols.map,v
retrieving revision 1.3
diff -u -p -r1.3 Symbols.map
--- lib/librthread/Symbols.map  27 Feb 2017 07:15:22 -0000      1.3
+++ lib/librthread/Symbols.map  15 Jul 2017 19:54:00 -0000
@@ -54,6 +54,7 @@
                pthread_detach;
                pthread_equal;
                pthread_exit;
+               pthread_get_name_np;
                pthread_getconcurrency;
                pthread_getcpuclockid;
                pthread_getprio;
Index: lib/librthread/pthread_np.h
===================================================================
RCS file: /cvs/src/lib/librthread/pthread_np.h,v
retrieving revision 1.1
diff -u -p -r1.1 pthread_np.h
--- lib/librthread/pthread_np.h 2 Apr 2016 19:56:53 -0000       1.1
+++ lib/librthread/pthread_np.h 15 Jul 2017 19:54:00 -0000
@@ -24,6 +24,7 @@ PROTO_DEPRECATED(pthread_main_np);
 PROTO_DEPRECATED(pthread_mutexattr_getkind_np);
 PROTO_DEPRECATED(pthread_mutexattr_setkind_np);
 PROTO_DEPRECATED(pthread_set_name_np);
+PROTO_DEPRECATED(pthread_get_name_np);
 PROTO_DEPRECATED(pthread_stackseg_np);
 
 #endif /* !_LIBPTHREAD_PTHREAD_NP_H_ */
Index: lib/librthread/rthread_np.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_np.c,v
retrieving revision 1.19
diff -u -p -r1.19 rthread_np.c
--- lib/librthread/rthread_np.c 7 May 2016 19:05:22 -0000       1.19
+++ lib/librthread/rthread_np.c 15 Jul 2017 19:54:00 -0000
@@ -43,6 +43,20 @@ pthread_set_name_np(pthread_t thread, co
        strlcpy(thread->name, name, sizeof(thread->name));
 }
 
+void
+pthread_get_name_np(pthread_t thread, char *name, size_t len)
+{
+       if (!name)
+               return;
+       if (!thread->name)
+               name[0] = '\0';
+       else {
+               if (len > sizeof(thread->name))
+                       len = sizeof(thread->name);
+               strlcpy(name, thread->name, len);
+       }
+}
+
 int
 pthread_main_np(void)
 {
Index: regress/lib/libpthread/fork/fork.c
===================================================================
RCS file: /cvs/src/regress/lib/libpthread/fork/fork.c,v
retrieving revision 1.4
diff -u -p -r1.4 fork.c
--- regress/lib/libpthread/fork/fork.c  31 Jul 2003 21:48:04 -0000      1.4
+++ regress/lib/libpthread/fork/fork.c  15 Jul 2017 19:54:38 -0000
@@ -73,6 +73,7 @@ main(int argc, char *argv[])
        int flags;
        pthread_t sleeper_thread;
        void *result;
+       char name[32];
        int status;
        pid_t parent_pid;
        pid_t child_pid;
@@ -88,6 +89,8 @@ main(int argc, char *argv[])
 
        CHECKr(pthread_create(&sleeper_thread, NULL, sleeper, NULL));
        sleep(1);
+       pthread_get_name_np(sleeper_thread, name, sizeof(name));
+       ASSERT(strcmp(name, "slpr") == 0);
 
        printf("forking from pid %d\n", getpid());
 

Reply via email to