public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Trevor Gross <tmgross@umich.edu>
To: Florian Weimer <fweimer@redhat.com>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: Trevor Gross via Libc-help <libc-help@sourceware.org>,
	jistone@redhat.com,  "Carlos O'Donell" <carlos@redhat.com>
Subject: Re: RFC: A way to get a TID from a pthread_t handler
Date: Fri, 12 Jan 2024 02:55:07 -0500	[thread overview]
Message-ID: <CALNs47snTUueRALjTPyWLdj-iX4+7mx0SR11k4PYEkguk0OsUA@mail.gmail.com> (raw)
In-Reply-To: <87a5pbv9na.fsf@oldenburg.str.redhat.com>

On Thu, Jan 11, 2024 at 4:32 PM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Trevor Gross via Libc-help:
>
> > It seems like there isn't a good way to get the thread ID from a
> > pthread_t. If you are within the thread then you can call gettid(),
> > but there is no public API to get a spawned thread's TID after calling
> > pthread_create().
> >
> > This value is stored in the pthread, so a new public function could
> > just be a basic getter:
> >
> >     pid_t pthread_gettid(pthread_t threadid)
> >     {
> >       struct pthread *pd = (struct pthread *) threadid;
> >       return pd->tid;
> >     }
> >
> > Would an addition like this likely get accepted? Or is there anything
> > overlooked about an easier way to recover the spawned thread's TID?
>
> We should probably make a copy of the TID inside pthread_create and
> return that from pthread_gettid, instead of pd->tid that might be
> cleared by the kernel upon thread exit.  This way, pthread_gettid
> remains valid until pthread_join is called.
>
> Thanks,
> Florian
>

As in, store a second pid_t value within pthread? This sounds doable,
I think that would sidestep a lot of the problems listed in [1]. Maybe
it could also be used other places after a INVALID_TD_P check, such as
at [2], to still return something somewhat valid if the check returns
a false positive.

Adding Adhemerval based on the thread, did you ever prototype an
implementation for this? (pthread_gettid_np, [1])

- Trevor

[1]: https://inbox.sourceware.org/libc-alpha/6d79a213-0df2-be8e-3596-e010f366a34f@linaro.org/
[2]: https://github.com/bminor/glibc/blob/520b1df08de68a3de328b65a25b86300a7ddf512/nptl/pthread_getcpuclockid.c#L38

  reply	other threads:[~2024-01-12  7:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  6:10 Trevor Gross
2024-01-11 18:23 ` Carlos O'Donell
2024-01-12  7:29   ` Trevor Gross
2024-01-11 21:32 ` Florian Weimer
2024-01-12  7:55   ` Trevor Gross [this message]
2024-01-15 19:47     ` Adhemerval Zanella Netto
2024-01-15 19:54       ` Florian Weimer

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=CALNs47snTUueRALjTPyWLdj-iX4+7mx0SR11k4PYEkguk0OsUA@mail.gmail.com \
    --to=tmgross@umich.edu \
    --cc=adhemerval.zanella@linaro.org \
    --cc=carlos@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=jistone@redhat.com \
    --cc=libc-help@sourceware.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 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).