From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id D5A953858D32; Tue, 18 Jul 2023 20:22:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5A953858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689711727; bh=Oh5dJmFe+y/mniZ/MFH9xC1vbbAWQhlh6qU6VxocTMI=; h=From:To:Subject:Date:From; b=HOyIWqovfuPsbrPqD0e11xgXs4wgRoDPHD00QoYAgEvD5W6W35Ekl7VHo2o0AESIS RcovvbQViEnvzahRtjKmYKVHPA2UMJ/L0bXJKvHkkwIJCGDylMsLDCPge3Dsn9mPsx j0RkIh4fwWwaCBfVkBby9cQ+ULJ/fsbfUxr8eqjY= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: don't wait infinitely on a pthread cancel event X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 816e9d67be0780b1fb253ad8d3d3c7443b70fd4c X-Git-Newrev: a02144808c5bc4e9753bfeabf06313c6413e1cd3 Message-Id: <20230718202207.D5A953858D32@sourceware.org> Date: Tue, 18 Jul 2023 20:22:07 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Da02144808c5= bc4e9753bfeabf06313c6413e1cd3 commit a02144808c5bc4e9753bfeabf06313c6413e1cd3 Author: Corinna Vinschen AuthorDate: Mon Jul 17 18:02:04 2023 +0200 Commit: Corinna Vinschen CommitDate: Tue Jul 18 22:21:36 2023 +0200 Cygwin: don't wait infinitely on a pthread cancel event =20 Starting with commit 42faed412857 ("* thread.h (class pthread): Add bool member canceled."), pthread::testcancel waits infinitely on cancel_event after it checked if the canceled variable is set. However, this might introduce a deadlock, if the thread calling pthread_cancel is terminated after setting canceled to true, but before calling SetEvent on cancel_e= vent. =20 In fact, it's not at all necessary to wait infinitely. By definition, the thread is only canceled if cancel_event is set. The canceled variable is just a helper to speed up code. We can safely assume that the thread hasn't been canceled yet, if canceled is set, but cancel_eve= nt isn't. =20 Fixes: 42faed412857 ("* thread.h (class pthread): Add bool member cance= led.") Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/thread.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc index afddf42821ad..7bb4f9fc8341 100644 --- a/winsup/cygwin/thread.cc +++ b/winsup/cygwin/thread.cc @@ -962,12 +962,9 @@ pthread::testcancel () pthread_testcancel function a lot without adding the overhead of an OS call. Only if the thread is marked as canceled, we wait for cancel_event being really set, on the off-chance that pthread_cancel - gets interrupted before calling SetEvent. */ - if (canceled) - { - WaitForSingleObject (cancel_event, INFINITE); - cancel_self (); - } + gets interrupted or terminated before calling SetEvent. */ + if (canceled && IsEventSignalled (cancel_event)) + cancel_self (); } =20 /* Return cancel event handle if it exists *and* cancel is not disabled.