diff mbox series

[1/1] util/oslib: Returns real thread identifier on FreeBSD and NetBSD

Message ID CA+XhMqwMsmoYwNtkrvAEG_j_-8L=+PYcXSz--1Qg622szArkRQ@mail.gmail.com
State New
Headers show
Series [1/1] util/oslib: Returns real thread identifier on FreeBSD and NetBSD | expand

Commit Message

David CARLIER May 23, 2020, 7:23 a.m. UTC
Hi this is my first contribution hope it s useful . Regards.
From ca7fcd85e0453f7173ce73732905463bc259ee32 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Sat, 23 May 2020 08:17:51 +0100
Subject: [PATCH] util/oslib: returns real thread identifier on FreeBSD and
 NetBSD

Signed-off-by: David Carlier <devnexen@gmail.com>
---
 util/oslib-posix.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Thomas Huth May 26, 2020, 5:40 a.m. UTC | #1
On 23/05/2020 09.23, David CARLIER wrote:
> Hi this is my first contribution hope it s useful . Regards.

 Hi!

Thanks for your contribution. Some hints for getting your patch included:

- Please make sure to CC: the corresponding maintainers, otherwise your
patch might get lost in the high traffic of the mailing list. See the
MAINTAINERS file for more information.

- For simple patches like this one, it might also be helpful to CC:
qemu-trivial@nongnu.org so that the patch could get picked up via the
trivial queue

> From ca7fcd85e0453f7173ce73732905463bc259ee32 Mon Sep 17 00:00:00 2001
> From: David Carlier <devnexen@gmail.com>
> Date: Sat, 23 May 2020 08:17:51 +0100
> Subject: [PATCH] util/oslib: returns real thread identifier on FreeBSD and
>  NetBSD

The body of the mail should only contain a proper patch description, not
these head lines anymore, so that the patch can directly applied via
"git am". Please also add a proper description, e.g. saying what's the
effect of your patch. Is it just a cosmetic thing? Does it fix a real
bug that you've hit?

> Signed-off-by: David Carlier <devnexen@gmail.com>
> ---
>  util/oslib-posix.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 062236a1ab..4d28dfd8f5 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -48,11 +48,13 @@
>  #ifdef __FreeBSD__
>  #include <sys/sysctl.h>
>  #include <sys/user.h>
> +#include <sys/thr.h>
>  #include <libutil.h>
>  #endif
> 
>  #ifdef __NetBSD__
>  #include <sys/sysctl.h>
> +#include <lwp.h>
>  #endif
> 
>  #include "qemu/mmap-alloc.h"
> @@ -84,6 +86,13 @@ int qemu_get_thread_id(void)
>  {
>  #if defined(__linux__)
>      return syscall(SYS_gettid);
> +#elif defined(__FreeBSD__)
> +    // thread id is up to INT_MAX

QEMU coding style only used /* ... */ comments, see the CODING_STYLE.rst
file.

> +    long tid;
> +    thr_self(&tid);
> +    return (int)tid;
> +#elif defined(__NetBSD__)
> +    return _lwp_self();
>  #else
>      return getpid();
>  #endif
> 

 HTH,
  Thomas
David CARLIER May 26, 2020, 7:29 a.m. UTC | #2
From 792fbcd9114f43bd80fd1ef5b25cd9935a536f9f Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Tue, 26 May 2020 08:25:26 +0100
Subject: [PATCH] util/oslib: Returns the real thread identifier on FreeBSD and
 NetBSD

getpid is good enough in a mono thread context, however
 thr_self/_lwp_self reflects the real current thread identifier
 from a given process.
---
 util/oslib-posix.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 062236a1ab..916f1be224 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -48,11 +48,13 @@
 #ifdef __FreeBSD__
 #include <sys/sysctl.h>
 #include <sys/user.h>
+#include <sys/thr.h>
 #include <libutil.h>
 #endif

 #ifdef __NetBSD__
 #include <sys/sysctl.h>
+#include <lwp.h>
 #endif

 #include "qemu/mmap-alloc.h"
@@ -84,6 +86,13 @@ int qemu_get_thread_id(void)
 {
 #if defined(__linux__)
     return syscall(SYS_gettid);
+#elif defined(__FreeBSD__)
+    /* thread id is up to INT_MAX */
+    long tid;
+    thr_self(&tid);
+    return (int)tid;
+#elif defined(__NetBSD__)
+    return _lwp_self();
 #else
     return getpid();
 #endif
Kamil Rytarowski May 26, 2020, 1:15 p.m. UTC | #3
Reviewed-by: Kamil Rytarowski <kamil@netbsd.org>

On 26.05.2020 09:29, David CARLIER wrote:
> From 792fbcd9114f43bd80fd1ef5b25cd9935a536f9f Mon Sep 17 00:00:00 2001
> From: David Carlier <devnexen@gmail.com>
> Date: Tue, 26 May 2020 08:25:26 +0100
> Subject: [PATCH] util/oslib: Returns the real thread identifier on FreeBSD and
>  NetBSD
> 
> getpid is good enough in a mono thread context, however
>  thr_self/_lwp_self reflects the real current thread identifier
>  from a given process.
> ---
>  util/oslib-posix.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 062236a1ab..916f1be224 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -48,11 +48,13 @@
>  #ifdef __FreeBSD__
>  #include <sys/sysctl.h>
>  #include <sys/user.h>
> +#include <sys/thr.h>
>  #include <libutil.h>
>  #endif
> 
>  #ifdef __NetBSD__
>  #include <sys/sysctl.h>
> +#include <lwp.h>
>  #endif
> 
>  #include "qemu/mmap-alloc.h"
> @@ -84,6 +86,13 @@ int qemu_get_thread_id(void)
>  {
>  #if defined(__linux__)
>      return syscall(SYS_gettid);
> +#elif defined(__FreeBSD__)
> +    /* thread id is up to INT_MAX */
> +    long tid;
> +    thr_self(&tid);
> +    return (int)tid;
> +#elif defined(__NetBSD__)
> +    return _lwp_self();
>  #else
>      return getpid();
>  #endif
>
Philippe Mathieu-Daudé June 3, 2020, 5:12 a.m. UTC | #4
ping?

On 5/26/20 9:29 AM, David CARLIER wrote:
> From 792fbcd9114f43bd80fd1ef5b25cd9935a536f9f Mon Sep 17 00:00:00 2001
> From: David Carlier <devnexen@gmail.com>
> Date: Tue, 26 May 2020 08:25:26 +0100
> Subject: [PATCH] util/oslib: Returns the real thread identifier on FreeBSD and
>  NetBSD
> 
> getpid is good enough in a mono thread context, however
>  thr_self/_lwp_self reflects the real current thread identifier
>  from a given process.
> ---
>  util/oslib-posix.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 062236a1ab..916f1be224 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -48,11 +48,13 @@
>  #ifdef __FreeBSD__
>  #include <sys/sysctl.h>
>  #include <sys/user.h>
> +#include <sys/thr.h>
>  #include <libutil.h>
>  #endif
> 
>  #ifdef __NetBSD__
>  #include <sys/sysctl.h>
> +#include <lwp.h>
>  #endif
> 
>  #include "qemu/mmap-alloc.h"
> @@ -84,6 +86,13 @@ int qemu_get_thread_id(void)
>  {
>  #if defined(__linux__)
>      return syscall(SYS_gettid);
> +#elif defined(__FreeBSD__)
> +    /* thread id is up to INT_MAX */
> +    long tid;
> +    thr_self(&tid);
> +    return (int)tid;
> +#elif defined(__NetBSD__)
> +    return _lwp_self();
>  #else
>      return getpid();
>  #endif
>
David CARLIER June 3, 2020, 6:14 a.m. UTC | #5
Sorry it landed in the spam.

It does make things more accurate, thus a bit more than cosmetic, as
stated in the commit message, thr_self/_lwp_self represents the
current thread id in multi thread context.

For OpenBSD it is syscall(SYS_getthrid) I believe
https://man.openbsd.org/getthrid.2

On Wed, 3 Jun 2020 at 06:12, Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
>
> ping?
>
> On 5/26/20 9:29 AM, David CARLIER wrote:
> > From 792fbcd9114f43bd80fd1ef5b25cd9935a536f9f Mon Sep 17 00:00:00 2001
> > From: David Carlier <devnexen@gmail.com>
> > Date: Tue, 26 May 2020 08:25:26 +0100
> > Subject: [PATCH] util/oslib: Returns the real thread identifier on FreeBSD and
> >  NetBSD
> >
> > getpid is good enough in a mono thread context, however
> >  thr_self/_lwp_self reflects the real current thread identifier
> >  from a given process.
> > ---
> >  util/oslib-posix.c | 9 +++++++++
> >  1 file changed, 9 insertions(+)
> >
> > diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> > index 062236a1ab..916f1be224 100644
> > --- a/util/oslib-posix.c
> > +++ b/util/oslib-posix.c
> > @@ -48,11 +48,13 @@
> >  #ifdef __FreeBSD__
> >  #include <sys/sysctl.h>
> >  #include <sys/user.h>
> > +#include <sys/thr.h>
> >  #include <libutil.h>
> >  #endif
> >
> >  #ifdef __NetBSD__
> >  #include <sys/sysctl.h>
> > +#include <lwp.h>
> >  #endif
> >
> >  #include "qemu/mmap-alloc.h"
> > @@ -84,6 +86,13 @@ int qemu_get_thread_id(void)
> >  {
> >  #if defined(__linux__)
> >      return syscall(SYS_gettid);
> > +#elif defined(__FreeBSD__)
> > +    /* thread id is up to INT_MAX */
> > +    long tid;
> > +    thr_self(&tid);
> > +    return (int)tid;
> > +#elif defined(__NetBSD__)
> > +    return _lwp_self();
> >  #else
> >      return getpid();
> >  #endif
> >
>
Li-Wen Hsu June 3, 2020, 3:07 p.m. UTC | #6
Sorry that I am not familiar with this part so I asked others to help
review (the FreeBSD related code).  It's good and please append:

Reviewed-by: Edward Tomasz Napierala <trasz@FreeBSD.org>

Thanks!

On Wed, Jun 3, 2020 at 2:14 PM David CARLIER <devnexen@gmail.com> wrote:
>
> Sorry it landed in the spam.
>
> It does make things more accurate, thus a bit more than cosmetic, as
> stated in the commit message, thr_self/_lwp_self represents the
> current thread id in multi thread context.
>
> For OpenBSD it is syscall(SYS_getthrid) I believe
> https://man.openbsd.org/getthrid.2
>
> On Wed, 3 Jun 2020 at 06:12, Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
> >
> > ping?
> >
> > On 5/26/20 9:29 AM, David CARLIER wrote:
> > > From 792fbcd9114f43bd80fd1ef5b25cd9935a536f9f Mon Sep 17 00:00:00 2001
> > > From: David Carlier <devnexen@gmail.com>
> > > Date: Tue, 26 May 2020 08:25:26 +0100
> > > Subject: [PATCH] util/oslib: Returns the real thread identifier on FreeBSD and
> > >  NetBSD
> > >
> > > getpid is good enough in a mono thread context, however
> > >  thr_self/_lwp_self reflects the real current thread identifier
> > >  from a given process.
> > > ---
> > >  util/oslib-posix.c | 9 +++++++++
> > >  1 file changed, 9 insertions(+)
> > >
> > > diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> > > index 062236a1ab..916f1be224 100644
> > > --- a/util/oslib-posix.c
> > > +++ b/util/oslib-posix.c
> > > @@ -48,11 +48,13 @@
> > >  #ifdef __FreeBSD__
> > >  #include <sys/sysctl.h>
> > >  #include <sys/user.h>
> > > +#include <sys/thr.h>
> > >  #include <libutil.h>
> > >  #endif
> > >
> > >  #ifdef __NetBSD__
> > >  #include <sys/sysctl.h>
> > > +#include <lwp.h>
> > >  #endif
> > >
> > >  #include "qemu/mmap-alloc.h"
> > > @@ -84,6 +86,13 @@ int qemu_get_thread_id(void)
> > >  {
> > >  #if defined(__linux__)
> > >      return syscall(SYS_gettid);
> > > +#elif defined(__FreeBSD__)
> > > +    /* thread id is up to INT_MAX */
> > > +    long tid;
> > > +    thr_self(&tid);
> > > +    return (int)tid;
> > > +#elif defined(__NetBSD__)
> > > +    return _lwp_self();
> > >  #else
> > >      return getpid();
> > >  #endif
> > >
> >
David CARLIER June 5, 2020, 4:24 p.m. UTC | #7
From 5f0b74a75906a9a043ce4874e7f09dedcad7e6e7 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Fri, 5 Jun 2020 17:18:25 +0100
Subject: [PATCH] util/oslib: Returns real thread identifier on FreeBSD,
 OpenBSD and NetBSD

getpid is good enough in a mono thread context,
however thr_self/getthrid/_lwp_self reflects
the real current thread identifier.

Reviewed-By: Kamil Rytarowski <kamil@netbsd.org>
Reviewed-By: Edward Tomasz Napierala <trasz@FreeBSD.org>
Signed-off-by: David Carlier <devnexen@gmail.com>
---
 util/oslib-posix.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 062236a1ab..7864a7768c 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -48,11 +48,17 @@
 #ifdef __FreeBSD__
 #include <sys/sysctl.h>
 #include <sys/user.h>
+#include <sys/thr.h>
 #include <libutil.h>
 #endif

+#ifdef __OpenBSD__
+#include <unistd.h>
+#endif
+
 #ifdef __NetBSD__
 #include <sys/sysctl.h>
+#include <lwp.h>
 #endif

 #include "qemu/mmap-alloc.h"
@@ -84,6 +90,15 @@ int qemu_get_thread_id(void)
 {
 #if defined(__linux__)
     return syscall(SYS_gettid);
+#elif defined(__FreeBSD__)
+    /* thread id is up to INT_MAX */
+    long tid;
+    thr_self(&tid);
+    return (int)tid;
+#elif defined(__OpenBSD__)
+    return getthrid();
+#elif defined(__NetBSD__)
+    return _lwp_self();
 #else
     return getpid();
 #endif
diff mbox series

Patch

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 062236a1ab..4d28dfd8f5 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -48,11 +48,13 @@ 
 #ifdef __FreeBSD__
 #include <sys/sysctl.h>
 #include <sys/user.h>
+#include <sys/thr.h>
 #include <libutil.h>
 #endif

 #ifdef __NetBSD__
 #include <sys/sysctl.h>
+#include <lwp.h>
 #endif

 #include "qemu/mmap-alloc.h"
@@ -84,6 +86,13 @@  int qemu_get_thread_id(void)
 {
 #if defined(__linux__)
     return syscall(SYS_gettid);
+#elif defined(__FreeBSD__)
+    // thread id is up to INT_MAX
+    long tid;
+    thr_self(&tid);
+    return (int)tid;
+#elif defined(__NetBSD__)
+    return _lwp_self();
 #else
     return getpid();
 #endif