RFC: pthread pid accessor (BZ# 27880)

Florian Weimer fweimer@redhat.com
Tue Jun 1 17:54:29 GMT 2021


* Adhemerval Zanella:

> On 01/06/2021 14:36, Florian Weimer wrote:
>> * Adhemerval Zanella:
>> 
>>> I think we should return the TID while the thread is being executing
>>> either cancellation handler or c++ destructors during the unwind 
>>> phase. Once no more user defined callback are being executed we should
>>> not return the TID anymore.
>> 
>>>> Yes, I would prefer that.  Does Bionic return the TID from the
>>>> resettable TCB field, or a copy?
>>>
>>> Bionic seems to maintain a list of active threads, so it returns
>>> the active thread field directly.
>> 
>> My question was whether Bionic implements that exit behavior, or
>> something else.
>> 
>
> If I understood correctly bionic implementation will just dissociate
> the thread from the global thread list *after* it has been joined by
> pthread_join.  It basically does:
>
>   pthread_join
>     thread = find_in_global_list
>
>     old_state = THREAD_NOT_JOINED
>     while (!cas (thread->join_state, &old_state, THREAD_JOINED))
>
>     if (thread->tid != 0)
>       futex_wait (&thread_tid, 0)
>
>     remove_from_global_list (thread)
>
> And the pthread assessor is basically:
>
>   pthread_gettid
>     thread = find_in_global_list
>     return thread->tid

And thread->tid isn't the field that's auto-cleared by the kernel?  Hmm.

It's still unsafe to use the handle after the join because the address
could be reused and show up again in the global list.

Thanks,
Florian



More information about the Libc-alpha mailing list