All of lore.kernel.org
 help / color / mirror / Atom feed
From: David CARLIER <devnexen@gmail.com>
To: Li-Wen Hsu <lwhsu@freebsd.org>
Cc: "Thomas Huth" <thuth@redhat.com>, "Ed Maste" <emaste@freebsd.org>,
	pbonzini@readhat.com, qemu-trivial@nongnu.org,
	"Edward Tomasz Napierala" <trasz@freebsd.org>,
	qemu-devel <qemu-devel@nongnu.org>,
	"Kamil Rytarowski" <kamil@netbsd.org>,
	bauerchen@tencent.com,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>
Subject: Re: [PATCH 1/1] util/oslib: Returns real thread identifier on FreeBSD and NetBSD
Date: Fri, 5 Jun 2020 17:24:38 +0100	[thread overview]
Message-ID: <CA+XhMqyPuZu-MM4V8bqNy2Ed-8gchRnZnAr=fhUjCY7SGVj-Jw@mail.gmail.com> (raw)
In-Reply-To: <CAKBkRUxb6XiWhL7aHKDewXW+WUi5Q5wyKRYrcDuVtoo3_k0YwA@mail.gmail.com>

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
-- 
2.27.0

On Wed, 3 Jun 2020 at 16:07, Li-Wen Hsu <lwhsu@freebsd.org> wrote:
>
> 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
> > > >
> > >


      reply	other threads:[~2020-06-05 16:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-23  7:23 [PATCH 1/1] util/oslib: Returns real thread identifier on FreeBSD and NetBSD David CARLIER
2020-05-26  5:40 ` Thomas Huth
2020-05-26  7:29   ` David CARLIER
2020-05-26 13:15     ` Kamil Rytarowski
2020-06-03  5:12     ` Philippe Mathieu-Daudé
2020-06-03  6:14       ` David CARLIER
2020-06-03 15:07         ` Li-Wen Hsu
2020-06-05 16:24           ` David CARLIER [this message]

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to='CA+XhMqyPuZu-MM4V8bqNy2Ed-8gchRnZnAr=fhUjCY7SGVj-Jw@mail.gmail.com' \
    --to=devnexen@gmail.com \
    --cc=bauerchen@tencent.com \
    --cc=emaste@freebsd.org \
    --cc=kamil@netbsd.org \
    --cc=lwhsu@freebsd.org \
    --cc=pbonzini@readhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=thuth@redhat.com \
    --cc=trasz@freebsd.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.