From patchwork Wed Jun 24 00:21:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 1315771 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ens-lyon.org Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49s3jv65txz9sRN for ; Wed, 24 Jun 2020 10:21:55 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C06973951C87; Wed, 24 Jun 2020 00:21:50 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hera.aquilenet.fr (hera.aquilenet.fr [IPv6:2a0c:e300::1]) by sourceware.org (Postfix) with ESMTPS id 90E073840C3B for ; Wed, 24 Jun 2020 00:21:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 90E073840C3B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ens-lyon.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=samuel.thibault@ens-lyon.org Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id AB92A11044; Wed, 24 Jun 2020 02:21:47 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at aquilenet.fr Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7aGMTLPOEIcX; Wed, 24 Jun 2020 02:21:47 +0200 (CEST) Received: from function (lfbn-bor-1-797-11.w86-234.abo.wanadoo.fr [86.234.239.11]) by hera.aquilenet.fr (Postfix) with ESMTPSA id DB43411034; Wed, 24 Jun 2020 02:21:46 +0200 (CEST) Received: from samy by function with local (Exim 4.94) (envelope-from ) id 1jntAa-009NUd-7k; Wed, 24 Jun 2020 02:21:40 +0200 From: Samuel Thibault To: libc-alpha@sourceware.org Subject: [hurd, commited] htl: Fix case when sem_*wait is canceled while holding a token Date: Wed, 24 Jun 2020 02:21:40 +0200 Message-Id: <20200624002140.2235218-1-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: commit-hurd@gnu.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" * sysdeps/htl/sem-timedwait.c (struct cancel_ctx): Add cancel_wake field. (cancel_hook): When unblocking thread, set cancel_wake field to 1. (__sem_timedwait_internal): Set cancel_wake field to 0 by default. On cancellation exit, check whether we hold a token, to be put back. --- sysdeps/htl/sem-timedwait.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sysdeps/htl/sem-timedwait.c b/sysdeps/htl/sem-timedwait.c index 691fb7e5ee..fffdf36467 100644 --- a/sysdeps/htl/sem-timedwait.c +++ b/sysdeps/htl/sem-timedwait.c @@ -27,6 +27,7 @@ struct cancel_ctx { struct __pthread *wakeup; sem_t *sem; + int cancel_wake; }; static void @@ -42,7 +43,10 @@ cancel_hook (void *arg) If it was already unblocked, it's not queued any more. */ unblock = wakeup->prevp != NULL; if (unblock) - __pthread_dequeue (wakeup); + { + __pthread_dequeue (wakeup); + ctx->cancel_wake = 1; + } __pthread_spin_unlock (&sem->__lock); if (unblock) @@ -62,6 +66,7 @@ __sem_timedwait_internal (sem_t *restrict sem, struct cancel_ctx ctx; ctx.wakeup = self; ctx.sem = sem; + ctx.cancel_wake = 0; /* Test for a pending cancellation request, switch to deferred mode for safer resource handling, and prepare the hook to call in case we're @@ -159,7 +164,13 @@ out_locked: __pthread_mutex_unlock (&self->cancel_lock); if (cancelled) - __pthread_exit (PTHREAD_CANCELED); + { + if (ret == 0 && ctx.cancel_wake == 0) + /* We were cancelled while waking up with a token, put it back. */ + sem_post (sem); + + __pthread_exit (PTHREAD_CANCELED); + } return ret; }