[PATCH 2/2] manual: Document __libc_single_threaded
Florian Weimer
fweimer@redhat.com
Fri May 22 17:40:19 GMT 2020
* Rich Felker:
>> Our discussion focused on the problem that observing a thread count of 1
>> in pthread_join does not necessarily mean that it is safe to assume at
>> this point that the process is single-threaded, in glibc's
>> implementation that uses a simple __nptl_nthreads counter decremented on
>> the thread itself. This does not cause a low-level data race directly,
>> but is potentially still incorrect (I'm not quite sure yet).
>
> pthread_join necessarily has an acquire barrier (this is a fundamental
> requirement of the interface contract; join is acquiring the results
> of the thread) so under some weak assumptions on unsynchronized memory
> access (e.g. non-tearing, not seeing a value that wasn't stored
> sometime between the last and next acquire barriers on the observer's
> side) I think observing it from pthread_join is safe.
Because of the meaning of the variable, it is *completely* safe if there
are no detached threads, without any further assumptions.
With detached threads an pthread_join observing a thread count of 1 (as
decreased during thread exit), the validity of setting
__libc_single_threaded depends on whether the kernel offers something
that causes a memory write on thread exit. I know of at least two such
facilities: the TID variable and robust mutexes. Therefore, I'm
inclined that further such facilities could be added by the kernel, in
ways not observable to glibc, and we better make sure that we have full
synchronization with the thread exit if we implemented the pthread_join
optimization for __libc_single_threaded *today*, although it might not
actually be needed.
> I don't think leaf (at least the gcc attribute leaf) is the actual
> issue here
Yes, please see the follow-up.
Thanks,
Florian
More information about the Libc-alpha
mailing list