diff options
author | Rich Felker <dalias@aerifal.cx> | 2014-07-05 23:29:55 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2014-07-05 23:29:55 -0400 |
commit | 83dc6eb087633abcf5608ad651d3b525ca2ec35e (patch) | |
tree | 1bed298fdfcf4167d63fed54d055a3c66af4f0f0 /src/thread | |
parent | 4c48501ee2a022a0dd207a2db4d346a00f9927a1 (diff) | |
download | musl-83dc6eb087633abcf5608ad651d3b525ca2ec35e.tar.gz musl-83dc6eb087633abcf5608ad651d3b525ca2ec35e.tar.bz2 musl-83dc6eb087633abcf5608ad651d3b525ca2ec35e.tar.xz musl-83dc6eb087633abcf5608ad651d3b525ca2ec35e.zip |
eliminate use of cached pid from thread structure
the main motivation for this change is to remove the assumption that
the tid of the main thread is also the pid of the process. (the value
returned by the set_tid_address syscall was used to fill both fields
despite it semantically being the tid.) this is historically and
presently true on linux and unlikely to change, but it conceivably
could be false on other systems that otherwise reproduce the linux
syscall api/abi.
only a few parts of the code were actually still using the cached pid.
in a couple places (aio and synccall) it was a minor optimization to
avoid a syscall. caching could be reintroduced, but lazily as part of
the public getpid function rather than at program startup, if it's
deemed important for performance later. in other places (cancellation
and pthread_kill) the pid was completely unnecessary; the tkill
syscall can be used instead of tgkill. this is actually a rather
subtle issue, since tgkill is supposedly a solution to race conditions
that can affect use of tkill. however, as documented in the commit
message for commit 7779dbd2663269b465951189b4f43e70839bc073, tgkill
does not actually solve this race; it just limits it to happening
within one process rather than between processes. we use a lock that
avoids the race in pthread_kill, and the use in the cancellation
signal handler is self-targeted and thus not subject to tid reuse
races, so both are safe regardless of which syscall (tgkill or tkill)
is used.
Diffstat (limited to 'src/thread')
-rw-r--r-- | src/thread/cancel_impl.c | 2 | ||||
-rw-r--r-- | src/thread/pthread_create.c | 1 | ||||
-rw-r--r-- | src/thread/pthread_kill.c | 2 | ||||
-rw-r--r-- | src/thread/synccall.c | 8 |
4 files changed, 5 insertions, 8 deletions
diff --git a/src/thread/cancel_impl.c b/src/thread/cancel_impl.c index 41cf2b8c..069b2796 100644 --- a/src/thread/cancel_impl.c +++ b/src/thread/cancel_impl.c @@ -52,7 +52,7 @@ static void cancel_handler(int sig, siginfo_t *si, void *ctx) __cancel(); } - __syscall(SYS_tgkill, self->pid, self->tid, SIGCANCEL); + __syscall(SYS_tkill, self->tid, SIGCANCEL); } void __testcancel() diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c index a7493c10..64151254 100644 --- a/src/thread/pthread_create.c +++ b/src/thread/pthread_create.c @@ -206,7 +206,6 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp new->map_size = size; new->stack = stack; new->stack_size = stack - stack_limit; - new->pid = self->pid; new->start = entry; new->start_arg = arg; new->self = new; diff --git a/src/thread/pthread_kill.c b/src/thread/pthread_kill.c index d9a5096a..acdb1ea6 100644 --- a/src/thread/pthread_kill.c +++ b/src/thread/pthread_kill.c @@ -4,7 +4,7 @@ int pthread_kill(pthread_t t, int sig) { int r; __lock(t->killlock); - r = t->dead ? ESRCH : -__syscall(SYS_tgkill, t->pid, t->tid, sig); + r = t->dead ? ESRCH : -__syscall(SYS_tkill, t->tid, sig); __unlock(t->killlock); return r; } diff --git a/src/thread/synccall.c b/src/thread/synccall.c index a21578dc..c4149904 100644 --- a/src/thread/synccall.c +++ b/src/thread/synccall.c @@ -1,5 +1,6 @@ #include "pthread_impl.h" #include <semaphore.h> +#include <unistd.h> static struct chain { struct chain *next; @@ -13,12 +14,11 @@ static sem_t chainlock, chaindone; static void handler(int sig, siginfo_t *si, void *ctx) { struct chain ch; - pthread_t self = __pthread_self(); int old_errno = errno; if (chainlen == libc.threads_minus_1) return; - sigqueue(self->pid, SIGSYNCCALL, (union sigval){0}); + sigqueue(getpid(), SIGSYNCCALL, (union sigval){0}); sem_init(&ch.sem, 0, 0); sem_init(&ch.sem2, 0, 0); @@ -39,7 +39,6 @@ static void handler(int sig, siginfo_t *si, void *ctx) void __synccall(void (*func)(void *), void *ctx) { - pthread_t self; struct sigaction sa; struct chain *next; sigset_t oldmask; @@ -65,8 +64,7 @@ void __synccall(void (*func)(void *), void *ctx) sigfillset(&sa.sa_mask); __libc_sigaction(SIGSYNCCALL, &sa, 0); - self = __pthread_self(); - sigqueue(self->pid, SIGSYNCCALL, (union sigval){0}); + sigqueue(getpid(), SIGSYNCCALL, (union sigval){0}); while (sem_wait(&chaindone)); sa.sa_flags = 0; |