From patchwork Tue Sep 4 20:45:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 145967 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp4125406ljw; Tue, 4 Sep 2018 13:47:22 -0700 (PDT) X-Google-Smtp-Source: ANB0VdY0za7bZBHfEVySwpMh5P7/dTWO16SuxrHNF+L95aIi6pDS0+7OM8sGQwCWlU9sumoklIY8 X-Received: by 2002:a62:6a01:: with SMTP id f1-v6mr4559983pfc.156.1536094042854; Tue, 04 Sep 2018 13:47:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536094042; cv=none; d=google.com; s=arc-20160816; b=Ar9kAKNnr5bHi4/F/wdRhPqOUvXdB1GtZgTmmTny1L4zMQ8vllOBOHs9OFvccC3wIL hVhiPjq25AGROwfj+GaHKL2bI4WUYkULg37J4WK+RzilxMWUm+bdsrh2c8NpLcZkBiV0 V2LjYnhf1eDA1zKpS9EU4eKsefcapFXGqWviW1NuaRiTNc6ac03EvQ5YBp/r9qqRB4Rv TMetKnN5EaHnGkp6wGgKi1RrF2MNXQ/u7ELRubofQ6du9AW6K6RYWf3TUWazc5Gd2/8A c+3EuiVcthp1N/FqEr7CuKEMebegWtv6wtZaVJL/G3mNdEf425uuenvHAfBvHbpYWExq H8Ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature:arc-authentication-results; bh=Jg5FMyulyTLN+Fb7rXtkjunYEzz3j7CB2Il4MgzQaMU=; b=v7aeYY4AoYYhUg7SYXudojof85/9iepUpXDS6x0wGZPfwT0ew/qr5FfpSrHVUs/uWu 2GeCxpwwNO2awGWk3hbd2uhHtR7lg5Nb29Fq8nolTNBw120kzI4FfQcN21NOdrRqB6Dw Iweb3dGUmU0wUToNZmOCaYg18tDCmj6Ng2fGAML0h0gq0FLPQyZvUXn3ItNiNkdQtzCU ps1InJ+nVgCrvYbMdPdTo/fVecIjusk1pUbHy+WhaYwCwi7JHreo0AAofd6cZ9tHRw59 shk6YfkQq1/ZTWMzMcjnG4RvMIFoJ8gll3Xh/DscCCshq1xPUiMESYALT3CWexRK8a0m 2DKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=SZn8qnIb; dkim=pass header.i=@linaro.org header.s=google header.b=XDxIDugF; spf=pass (google.com: domain of libc-alpha-return-95674-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-95674-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id u13-v6si22506390pgg.263.2018.09.04.13.47.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Sep 2018 13:47:22 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-95674-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=SZn8qnIb; dkim=pass header.i=@linaro.org header.s=google header.b=XDxIDugF; spf=pass (google.com: domain of libc-alpha-return-95674-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-95674-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=xwntBMxu0dIIw+AR4bAizak1e41T+NR 2Tq/khhDUtuCJmGW1tJnk+6BxSQ9DAr9F4b8LjlhingTWqS+LP9bFCzdZD5g8dYL 5TShlxuSJMgMNR+FKcfE9JvGcV5+VCilja4TQqzLCEsmM9IkKyEIylRqSJTaNolF TZkaEC9D1OAk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=2NRTH0NcSYTfGBxlGCA2fj3XZ6E=; b=SZn8q nIbOMyRMkokUEiur5Dbbh+aIlj7yLiDSqUQHZe8CMocJjyiEqXrGo80sIwgbxxB8 dEj0Uw98S35jF5UzdfN7GWaEc+OmmYtqSS9AQsGYsiG8duman6WP9bhrpxTSO5HY nGPPOGkxZpi8KwDiI6z/Mriz8KvcPfCA3uwMfw= Received: (qmail 35220 invoked by alias); 4 Sep 2018 20:46:16 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 35112 invoked by uid 89); 4 Sep 2018 20:46:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SEM_URI, SEM_URIRED, SPF_PASS autolearn=ham version=3.3.2 spammy=acquire, HX-Received:sk:b1-v6mr, wakeup X-HELO: mail-qt0-f176.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=Jg5FMyulyTLN+Fb7rXtkjunYEzz3j7CB2Il4MgzQaMU=; b=XDxIDugF0AJh3kQ0ZGDUus8CRDxkCKLR1l/r+hJFabZ0LbbZolGuGa/e78AW4Zuxjl pcBUxkUoo0l9NmkGOtEEv9ZA+kDEihWZXHC6fKrhrzM0NzqIlIkqbQgXcM4eDKEdqr1B DsjGR8zfwMcT+z3t9j+Z7Q6GNFdu4ueUsWJF0= Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 6/7] nptl: Cleanup cancellation macros Date: Tue, 4 Sep 2018 17:45:52 -0300 Message-Id: <20180904204553.6971-7-adhemerval.zanella@linaro.org> In-Reply-To: <20180904204553.6971-1-adhemerval.zanella@linaro.org> References: <20180904204553.6971-1-adhemerval.zanella@linaro.org> This patch wraps all uses of *_{enable,disable}_asynccancel and and *_CANCEL_{ASYNC,RESET} in either already provided macros (lll_futex_timed_wait_cancel) or creates new ones if the functionality is not provided (SYSCALL_CANCEL_NCS, lll_futex_wait_cancel, and lll_futex_timed_wait_cancel). Also for some generic implementations, the direct call of the macros are removed since the underlying symbols are suppose to provide cancellation support. This is a priliminary patch intended to simplify the work required for BZ#12683 fix. It is a refactor change, no semantic changes are expected. Checked on x86_64-linux-gnu and i686-linux-gnu. * nptl/pthread_join_common.c (__pthread_timedjoin_ex): Use lll_wait_tid with timeout. * nptl/sem_wait.c (__old_sem_wait): Use lll_futex_wait_cancel. * sysdeps/nptl/aio_misc.h (AIO_MISC_WAIT): Use futex_reltimed_wait_cancelable for cancelabla mode. * sysdeps/nptl/gai_misc.h (GAI_MISC_WAIT): Likewise. * sysdeps/posix/open64.c (__libc_open64): Do not call cancelation macros. * sysdeps/posix/sigwait.c (__sigwait): Likewise. * sysdeps/posix/waitid.c (__sigwait): Likewise. * sysdeps/unix/sysdep.h (__SYSCALL_CANCEL_CALL, SYSCALL_CANCEL_NCS): New macro. * sysdeps/nptl/lowlevellock.h (lll_wait_tid): Add timeout argument. (lll_timedwait_tid): Remove macro. * sysdeps/unix/sysv/linux/i386/lowlevellock.h (lll_wait_tid): Likewise. (lll_timedwait_tid): Likewise. * sysdeps/unix/sysv/linux/sparc/lowlevellock.h (lll_wait_tid): Likewise. (lll_timedwait_tid): Likewise. * sysdeps/unix/sysv/linux/x86_64/lowlevellock.h (lll_wait_tid): Likewise. (lll_timedwait_tid): Likewise. * sysdeps/unix/sysv/linux/clock_nanosleep.c (__clock_nanosleep): Use INTERNAL_SYSCALL_CANCEL. * sysdeps/unix/sysv/linux/futex-internal.h (futex_reltimed_wait_cancelable): Use LIBC_CANCEL_{ASYNC,RESET} instead of __pthread_{enable,disable}_asynccancel. * sysdeps/unix/sysv/linux/lowlevellock-futex.h (lll_futex_wait_cancel): New macro. --- ChangeLog | 31 ++++++++++++++ nptl/pthread_join_common.c | 9 +--- nptl/sem_wait.c | 8 +--- sysdeps/nptl/aio_misc.h | 15 +++---- sysdeps/nptl/gai_misc.h | 15 +++---- sysdeps/nptl/lowlevellock.h | 40 ++++++++---------- sysdeps/posix/open64.c | 12 +----- sysdeps/posix/sigwait.c | 12 +----- sysdeps/posix/waitid.c | 12 +----- sysdeps/unix/sysv/linux/clock_nanosleep.c | 23 ++++------ sysdeps/unix/sysv/linux/futex-internal.h | 4 +- sysdeps/unix/sysv/linux/i386/lowlevellock.h | 42 +++++++++---------- sysdeps/unix/sysv/linux/lowlevellock-futex.h | 11 +++++ sysdeps/unix/sysv/linux/sparc/lowlevellock.h | 39 ++++++++--------- sysdeps/unix/sysv/linux/x86_64/lowlevellock.h | 40 +++++++++--------- 15 files changed, 148 insertions(+), 165 deletions(-) -- 2.17.1 diff --git a/nptl/pthread_join_common.c b/nptl/pthread_join_common.c index 702fcd1545..63d693210d 100644 --- a/nptl/pthread_join_common.c +++ b/nptl/pthread_join_common.c @@ -81,14 +81,7 @@ __pthread_timedjoin_ex (pthread_t threadid, void **thread_return, un-wait-ed for again. */ pthread_cleanup_push (cleanup, &pd->joinid); - int oldtype = CANCEL_ASYNC (); - - if (abstime != NULL) - result = lll_timedwait_tid (pd->tid, abstime); - else - lll_wait_tid (pd->tid); - - CANCEL_RESET (oldtype); + result = lll_wait_tid (pd->tid, abstime); pthread_cleanup_pop (0); } diff --git a/nptl/sem_wait.c b/nptl/sem_wait.c index e7d910613f..e057b05dd6 100644 --- a/nptl/sem_wait.c +++ b/nptl/sem_wait.c @@ -56,14 +56,8 @@ __old_sem_wait (sem_t *sem) if (atomic_decrement_if_positive (futex) > 0) return 0; - /* Enable asynchronous cancellation. Required by the standard. */ - int oldtype = __pthread_enable_asynccancel (); - /* Always assume the semaphore is shared. */ - err = lll_futex_wait (futex, 0, LLL_SHARED); - - /* Disable asynchronous cancellation. */ - __pthread_disable_asynccancel (oldtype); + err = lll_futex_wait_cancel (futex, 0, LLL_SHARED); } while (err == 0 || err == -EWOULDBLOCK); diff --git a/sysdeps/nptl/aio_misc.h b/sysdeps/nptl/aio_misc.h index 206d8e193d..70f1e057b5 100644 --- a/sysdeps/nptl/aio_misc.h +++ b/sysdeps/nptl/aio_misc.h @@ -41,15 +41,15 @@ { \ pthread_mutex_unlock (&__aio_requests_mutex); \ \ - int oldtype; \ - if (cancel) \ - oldtype = LIBC_CANCEL_ASYNC (); \ - \ int status; \ do \ { \ - status = futex_reltimed_wait ((unsigned int *) futexaddr, oldval, \ - timeout, FUTEX_PRIVATE); \ + if (cancel) \ + status = futex_reltimed_wait_cancelable ( \ + (unsigned int *) futexaddr, oldval, timeout, FUTEX_PRIVATE); \ + else \ + status = futex_reltimed_wait ((unsigned int *) futexaddr, \ + oldval, timeout, FUTEX_PRIVATE); \ if (status != EAGAIN) \ break; \ \ @@ -57,9 +57,6 @@ } \ while (oldval != 0); \ \ - if (cancel) \ - LIBC_CANCEL_RESET (oldtype); \ - \ if (status == EINTR) \ result = EINTR; \ else if (status == ETIMEDOUT) \ diff --git a/sysdeps/nptl/gai_misc.h b/sysdeps/nptl/gai_misc.h index 815e6c0dc6..831b43a479 100644 --- a/sysdeps/nptl/gai_misc.h +++ b/sysdeps/nptl/gai_misc.h @@ -42,15 +42,15 @@ { \ pthread_mutex_unlock (&__gai_requests_mutex); \ \ - int oldtype; \ - if (cancel) \ - oldtype = LIBC_CANCEL_ASYNC (); \ - \ int status; \ do \ { \ - status = futex_reltimed_wait ((unsigned int *) futexaddr, oldval, \ - timeout, FUTEX_PRIVATE); \ + if (cancel) \ + status = futex_reltimed_wait_cancelable ( \ + (unsigned int *) futexaddr, oldval, timeout, FUTEX_PRIVATE); \ + else \ + status = futex_reltimed_wait ((unsigned int *) futexaddr, \ + oldval, timeout, FUTEX_PRIVATE); \ if (status != EAGAIN) \ break; \ \ @@ -58,9 +58,6 @@ } \ while (oldval != 0); \ \ - if (cancel) \ - LIBC_CANCEL_RESET (oldtype); \ - \ if (status == EINTR) \ result = EINTR; \ else if (status == ETIMEDOUT) \ diff --git a/sysdeps/nptl/lowlevellock.h b/sysdeps/nptl/lowlevellock.h index bfbda99940..d7837be5d4 100644 --- a/sysdeps/nptl/lowlevellock.h +++ b/sysdeps/nptl/lowlevellock.h @@ -175,33 +175,29 @@ extern int __lll_timedlock_wait (int *futex, const struct timespec *, #define LLL_LOCK_INITIALIZER (0) #define LLL_LOCK_INITIALIZER_LOCKED (1) +extern int __lll_timedwait_tid (int *, const struct timespec *) + attribute_hidden; /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex wake-up when the clone terminates. The memory location contains the thread ID while the clone is running and is reset to zero by the kernel afterwards. The kernel up to version 3.16.3 does not use the private futex - operations for futex wake-up when the clone terminates. */ -#define lll_wait_tid(tid) \ - do { \ - __typeof (tid) __tid; \ - /* We need acquire MO here so that we synchronize \ - with the kernel's store to 0 when the clone \ - terminates. (see above) */ \ - while ((__tid = atomic_load_acquire (&(tid))) != 0) \ - lll_futex_wait (&(tid), __tid, LLL_SHARED); \ - } while (0) - -extern int __lll_timedwait_tid (int *, const struct timespec *) - attribute_hidden; - -/* As lll_wait_tid, but with a timeout. If the timeout occurs then return - ETIMEDOUT. If ABSTIME is invalid, return EINVAL. */ -#define lll_timedwait_tid(tid, abstime) \ - ({ \ - int __res = 0; \ - if ((tid) != 0) \ - __res = __lll_timedwait_tid (&(tid), (abstime)); \ - __res; \ + operations for futex wake-up when the clone terminates. + If ABSTIME is not NULL, is used a timeout for futex call. If the timeout + occurs then return ETIMEOUT, if ABSTIME is invalid, return EINVAL. + The futex operation are issues with cancellable versions. */ +#define lll_wait_tid(tid, abstime) \ + ({ \ + int __res = 0; \ + __typeof (tid) __tid; \ + if (abstime != NULL) \ + __res = __lll_timedwait_tid (&(tid), (abstime)); \ + else \ + /* We need acquire MO here so that we synchronize with the \ + kernel's store to 0 when the clone terminates. (see above) */ \ + while ((__tid = atomic_load_acquire (&(tid))) != 0) \ + lll_futex_wait_cancel (&(tid), __tid, LLL_SHARED); \ + __res; \ }) diff --git a/sysdeps/posix/open64.c b/sysdeps/posix/open64.c index c4209c8cdb..e4ec5d1876 100644 --- a/sysdeps/posix/open64.c +++ b/sysdeps/posix/open64.c @@ -34,16 +34,8 @@ __libc_open64 (const char *file, int oflag, ...) va_end (arg); } - if (SINGLE_THREAD_P) - return __libc_open (file, oflag | O_LARGEFILE, mode); - - int oldtype = LIBC_CANCEL_ASYNC (); - - int result = __libc_open (file, oflag | O_LARGEFILE, mode); - - LIBC_CANCEL_RESET (oldtype); - - return result; + /* __libc_open should be a cancellation point. */ + return __libc_open (file, oflag | O_LARGEFILE, mode); } weak_alias (__libc_open64, __open64) libc_hidden_weak (__open64) diff --git a/sysdeps/posix/sigwait.c b/sysdeps/posix/sigwait.c index 4ff9d847d4..acc82e1965 100644 --- a/sysdeps/posix/sigwait.c +++ b/sysdeps/posix/sigwait.c @@ -85,16 +85,8 @@ do_sigwait (const sigset_t *set, int *sig) int __sigwait (const sigset_t *set, int *sig) { - if (SINGLE_THREAD_P) - return do_sigwait (set, sig); - - int oldtype = LIBC_CANCEL_ASYNC (); - - int result = do_sigwait (set, sig); - - LIBC_CANCEL_RESET (oldtype); - - return result; + /* __sigsuspend should be a cancellation point. */ + return do_waitid (idtype, id, infop, options); } libc_hidden_def (__sigwait) weak_alias (__sigwait, sigwait) diff --git a/sysdeps/posix/waitid.c b/sysdeps/posix/waitid.c index 3207c742c2..98f35d078e 100644 --- a/sysdeps/posix/waitid.c +++ b/sysdeps/posix/waitid.c @@ -151,16 +151,8 @@ OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) int __waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options) { - if (SINGLE_THREAD_P) - return do_waitid (idtype, id, infop, options); - - int oldtype = LIBC_CANCEL_ASYNC (); - - int result = do_waitid (idtype, id, infop, options); - - LIBC_CANCEL_RESET (oldtype); - - return result; + /* __waitpid should be a cancellation point. */ + return do_waitid (idtype, id, infop, options); } weak_alias (__waitid, waitid) strong_alias (__waitid, __libc_waitid) diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c index 93d5d6ef12..31229bbd85 100644 --- a/sysdeps/unix/sysv/linux/clock_nanosleep.c +++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c @@ -28,27 +28,18 @@ int __clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req, struct timespec *rem) { - INTERNAL_SYSCALL_DECL (err); - int r; - if (clock_id == CLOCK_THREAD_CPUTIME_ID) return EINVAL; if (clock_id == CLOCK_PROCESS_CPUTIME_ID) clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED); - if (SINGLE_THREAD_P) - r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, rem); - else - { - int oldstate = LIBC_CANCEL_ASYNC (); - - r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, - rem); - - LIBC_CANCEL_RESET (oldstate); - } - + /* If the call is interrupted by a signal handler or encounters an error, + it returns a positive value similar to errno. */ + INTERNAL_SYSCALL_DECL (err); + int r = INTERNAL_SYSCALL_CANCEL (clock_nanosleep, err, clock_id, flags, + req, rem); return (INTERNAL_SYSCALL_ERROR_P (r, err) - ? INTERNAL_SYSCALL_ERRNO (r, err) : 0); + ? INTERNAL_SYSCALL_ERRNO (r, err) : 0); + } weak_alias (__clock_nanosleep, clock_nanosleep) diff --git a/sysdeps/unix/sysv/linux/futex-internal.h b/sysdeps/unix/sysv/linux/futex-internal.h index 96a07b05b9..d36235a925 100644 --- a/sysdeps/unix/sysv/linux/futex-internal.h +++ b/sysdeps/unix/sysv/linux/futex-internal.h @@ -138,9 +138,9 @@ futex_reltimed_wait_cancelable (unsigned int *futex_word, const struct timespec *reltime, int private) { int oldtype; - oldtype = __pthread_enable_asynccancel (); + oldtype = LIBC_CANCEL_ASYNC (); int err = lll_futex_timed_wait (futex_word, expected, reltime, private); - __pthread_disable_asynccancel (oldtype); + LIBC_CANCEL_RESET (oldtype); switch (err) { case 0: diff --git a/sysdeps/unix/sysv/linux/i386/lowlevellock.h b/sysdeps/unix/sysv/linux/i386/lowlevellock.h index 38fbc2556f..c0b938bd9f 100644 --- a/sysdeps/unix/sysv/linux/i386/lowlevellock.h +++ b/sysdeps/unix/sysv/linux/i386/lowlevellock.h @@ -221,32 +221,30 @@ extern int __lll_timedlock_elision (int *futex, short *adapt_count, #define lll_islocked(futex) \ (futex != LLL_LOCK_INITIALIZER) +extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime) + __attribute__ ((regparm (2))) attribute_hidden; + /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex wake-up when the clone terminates. The memory location contains the thread ID while the clone is running and is reset to zero by the kernel afterwards. The kernel up to version 3.16.3 does not use the private futex - operations for futex wake-up when the clone terminates. */ -#define lll_wait_tid(tid) \ - do { \ - __typeof (tid) __tid; \ - while ((__tid = (tid)) != 0) \ - lll_futex_wait (&(tid), __tid, LLL_SHARED);\ - } while (0) - -extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime) - __attribute__ ((regparm (2))) attribute_hidden; - -/* As lll_wait_tid, but with a timeout. If the timeout occurs then return - ETIMEDOUT. If ABSTIME is invalid, return EINVAL. - XXX Note that this differs from the generic version in that we do the - error checking here and not in __lll_timedwait_tid. */ -#define lll_timedwait_tid(tid, abstime) \ - ({ \ - int __result = 0; \ - if ((tid) != 0) \ - __result = __lll_timedwait_tid (&(tid), (abstime)); \ - __result; }) - + operations for futex wake-up when the clone terminates. + If ABSTIME is not NULL, is used a timeout for futex call. If the timeout + occurs then return ETIMEOUT, if ABSTIME is invalid, return EINVAL. + The futex operation are issues with cancellable versions. */ +#define lll_wait_tid(tid, abstime) \ + ({ \ + int __res = 0; \ + __typeof (tid) __tid; \ + if (abstime != NULL) \ + __res = __lll_timedwait_tid (&(tid), (abstime)); \ + else \ + /* We need acquire MO here so that we synchronize with the \ + kernel's store to 0 when the clone terminates. (see above) */ \ + while ((__tid = atomic_load_acquire (&(tid))) != 0) \ + lll_futex_wait_cancel (&(tid), __tid, LLL_SHARED); \ + __res; \ + }) extern int __lll_lock_elision (int *futex, short *adapt_count, int private) attribute_hidden; diff --git a/sysdeps/unix/sysv/linux/lowlevellock-futex.h b/sysdeps/unix/sysv/linux/lowlevellock-futex.h index fc834ed16e..71420fea89 100644 --- a/sysdeps/unix/sysv/linux/lowlevellock-futex.h +++ b/sysdeps/unix/sysv/linux/lowlevellock-futex.h @@ -125,6 +125,17 @@ private), \ nr_wake, nr_move, mutex, val) + +/* Cancellable futex macros. */ +#define lll_futex_wait_cancel(futexp, val, private) \ + ({ \ + int __oldtype = CANCEL_ASYNC (); \ + long int __err = lll_futex_wait (futexp, val, LLL_SHARED); \ + CANCEL_RESET (__oldtype); \ + __err; \ + }) + + #endif /* !__ASSEMBLER__ */ #endif /* lowlevellock-futex.h */ diff --git a/sysdeps/unix/sysv/linux/sparc/lowlevellock.h b/sysdeps/unix/sysv/linux/sparc/lowlevellock.h index e2c0b2abaa..28f368dbe7 100644 --- a/sysdeps/unix/sysv/linux/sparc/lowlevellock.h +++ b/sysdeps/unix/sysv/linux/sparc/lowlevellock.h @@ -108,28 +108,29 @@ __lll_timedlock (int *futex, const struct timespec *abstime, int private) #define LLL_LOCK_INITIALIZER (0) #define LLL_LOCK_INITIALIZER_LOCKED (1) -/* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex - wakeup when the clone terminates. The memory location contains the - thread ID while the clone is running and is reset to zero - afterwards. */ -#define lll_wait_tid(tid) \ - do \ - { \ - __typeof (tid) __tid; \ - while ((__tid = (tid)) != 0) \ - lll_futex_wait (&(tid), __tid, LLL_SHARED); \ - } \ - while (0) - extern int __lll_timedwait_tid (int *, const struct timespec *) attribute_hidden; -#define lll_timedwait_tid(tid, abstime) \ - ({ \ - int __res = 0; \ - if ((tid) != 0) \ - __res = __lll_timedwait_tid (&(tid), (abstime)); \ - __res; \ +/* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex + wake-up when the clone terminates. The memory location contains the + thread ID while the clone is running and is reset to zero by the kernel + afterwards. The kernel up to version 3.16.3 does not use the private futex + operations for futex wake-up when the clone terminates. + If ABSTIME is not NULL, is used a timeout for futex call. If the timeout + occurs then return ETIMEOUT, if ABSTIME is invalid, return EINVAL. + The futex operation are issues with cancellable versions. */ +#define lll_wait_tid(tid, abstime) \ + ({ \ + int __res = 0; \ + __typeof (tid) __tid; \ + if (abstime != NULL) \ + __res = __lll_timedwait_tid (&(tid), (abstime)); \ + else \ + /* We need acquire MO here so that we synchronize with the \ + kernel's store to 0 when the clone terminates. (see above) */ \ + while ((__tid = atomic_load_acquire (&(tid))) != 0) \ + lll_futex_wait_cancel (&(tid), __tid, LLL_SHARED); \ + __res; \ }) #endif /* lowlevellock.h */ diff --git a/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h b/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h index eedb6fc990..3c5860d4cf 100644 --- a/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h +++ b/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h @@ -224,32 +224,30 @@ extern int __lll_timedlock_elision (int *futex, short *adapt_count, #define lll_islocked(futex) \ (futex != LLL_LOCK_INITIALIZER) +extern int __lll_timedwait_tid (int *, const struct timespec *) + attribute_hidden; /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex wake-up when the clone terminates. The memory location contains the thread ID while the clone is running and is reset to zero by the kernel afterwards. The kernel up to version 3.16.3 does not use the private futex - operations for futex wake-up when the clone terminates. */ -#define lll_wait_tid(tid) \ - do { \ - __typeof (tid) __tid; \ - while ((__tid = (tid)) != 0) \ - lll_futex_wait (&(tid), __tid, LLL_SHARED);\ - } while (0) - -extern int __lll_timedwait_tid (int *, const struct timespec *) - attribute_hidden; - -/* As lll_wait_tid, but with a timeout. If the timeout occurs then return - ETIMEDOUT. If ABSTIME is invalid, return EINVAL. - XXX Note that this differs from the generic version in that we do the - error checking here and not in __lll_timedwait_tid. */ -#define lll_timedwait_tid(tid, abstime) \ - ({ \ - int __result = 0; \ - if ((tid) != 0) \ - __result = __lll_timedwait_tid (&(tid), (abstime)); \ - __result; }) + operations for futex wake-up when the clone terminates. + If ABSTIME is not NULL, is used a timeout for futex call. If the timeout + occurs then return ETIMEOUT, if ABSTIME is invalid, return EINVAL. + The futex operation are issues with cancellable versions. */ +#define lll_wait_tid(tid, abstime) \ + ({ \ + int __res = 0; \ + __typeof (tid) __tid; \ + if (abstime != NULL) \ + __res = __lll_timedwait_tid (&(tid), (abstime)); \ + else \ + /* We need acquire MO here so that we synchronize with the \ + kernel's store to 0 when the clone terminates. (see above) */ \ + while ((__tid = atomic_load_acquire (&(tid))) != 0) \ + lll_futex_wait_cancel (&(tid), __tid, LLL_SHARED); \ + __res; \ + }) extern int __lll_lock_elision (int *futex, short *adapt_count, int private) attribute_hidden;