pthread_exit: don't __vm_wait under thread list lock
authorRich Felker <dalias@aerifal.cx>
Thu, 19 Nov 2020 21:09:16 +0000 (16:09 -0500)
committerRich Felker <dalias@aerifal.cx>
Thu, 19 Nov 2020 21:09:16 +0000 (16:09 -0500)
commitd26e0774a59bb7245b205bc8e7d8b35cc2037095
tree1f9921e83c473b5b1c595c99c20801d7df4d4564
parent167390f05564e0a4d3fcb4329377fd7743267560
pthread_exit: don't __vm_wait under thread list lock

the __vm_wait operation can delay forward progress arbitrarily long if
a thread holding the lock is interrupted by a signal. in a worst case
this can deadlock. any critical section holding the thread list lock
must respect lock ordering contracts and must not take any lock which
is not AS-safe.

to fix, move the determination of thread joinable/detached state to
take place before the killlock and thread list lock are taken. this
requires reverting the atomic state transition if we determine that
the exiting thread is the last thread and must call exit, but that's
easy to do since it's a single-threaded context with application
signals blocked.
src/thread/pthread_create.c