improve pthread_exit synchronization with functions targeting tid

if the last thread exited via pthread_exit, the logic that marked it
dead did not account for the possibility of it targeting itself via
atexit handlers. for example, an atexit handler calling
pthread_kill(pthread_self(), SIGKILL) would return success
(previously, ESRCH) rather than causing termination via the signal.

move the release of killlock after the determination is made whether
the exiting thread is the last thread. in the case where it's not,
move the release all the way to the end of the function. this way we
can clear the tid rather than spending storage on a dedicated
dead-flag. clearing the tid is also preferable in that it hardens
against inadvertent use of the value after the thread has terminated
but before it is joined.
This commit is contained in:
Rich Felker
2018-05-05 11:09:51 -04:00
parent 4df4216351
commit 526e64f54d
6 changed files with 18 additions and 17 deletions
+2 -2
View File
@@ -4,8 +4,8 @@ int pthread_kill(pthread_t t, int sig)
{
int r;
LOCK(t->killlock);
r = t->dead ? (sig+0U >= _NSIG ? EINVAL : 0)
: -__syscall(SYS_tkill, t->tid, sig);
r = t->tid ? -__syscall(SYS_tkill, t->tid, sig)
: (sig+0U >= _NSIG ? EINVAL : 0);
UNLOCK(t->killlock);
return r;
}