From nobody Sat Dec 28 17:20:25 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YL8JV4bxcz5jfb1; Sat, 28 Dec 2024 17:20:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YL8JV3Gdvz4msP; Sat, 28 Dec 2024 17:20:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735406426; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PHdjr5s/c68XiE+TWM5vvLSi9h/zAVYs0o/TnXJnpno=; b=ewmEL/WSZlVkcnGPD3gkD8M3rboBKcIOa9UOBu+kLOIsMJEJImxvjSvCAJCWuv7W+rWatV 7PGukLYEXUKHcy9AmJmCIMPm9RY/wp7zYiT+0w0DEO0C3+xlzO/7Nl4RbJ8xNfY/itbYcd 87dgw+lblcSpYhRhhj7xDNsP6L6XXEXZep8w916xMc0VjO4oFp3Krnt0LknT1J4tIZBj2R UqHQYfOG4X49HkGPB5CaU3Rb389eHQ7Nx8Mj1dLqaAav00AS4kPY3MO+F44SSg4GzM7vho Pui3m9qRQ5Fs9BJ1eIKs2BryZ8O7S6mwbGvayNc+jxJo4Cj8CRRXJo+atECXWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735406426; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PHdjr5s/c68XiE+TWM5vvLSi9h/zAVYs0o/TnXJnpno=; b=fzboAGyvvd6M/ri54Z9K2FZ06/oAVNrGhJ6rH/dwp6oZE1SToYwxD09eQzwvLIOjgR8h8E XXjsE6Ttkhy9nnoTvMAwF3zSNc360Dokt2HeRypiKX5+If3bzXbnU/1NaqFaNq0ziA60rg NxISkp0XiwiacbvXOUGAk/HWVpafwpZ5sAm/QGG7TPlA7EWcdr88XWCENUiFZvs9E1a/yU SSPvlQJVNGse3KireeZSCr4TbD2nfcrWVqiDHO1lcUVN3lG0aXvQH1zEdfEaJ02YjFPeuZ 3AzmqILeFmPSU/ZngqPewBQvUxRpeBALbC3I2w0CHIXGQSKw2l7F4Zakb3SfOA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1735406426; a=rsa-sha256; cv=none; b=su4zOL7iIxzwq2GlZO6rZkItt6J83yQgvrmNUhok7R/whb9BKAkuXAyAEppBWP0g1q6fCC WrnDya3oJh/NCyY9etfh/Ihz5s1jz7bWTVKEG13GRcXvxjoSb7QOmC74ucbHcxkxcySdbP meP3mQvHGc5q0GFt7kih5OJ8ryorkPpwQyWycHwnF322qV+2Zi9kEXeyG1hSNFWO+nvA+F hUNXLVGwHAGBvDsNJxbYJ/wiXgjmHP91MH2rFG5IEz0fO77P5rmA3la8fF+snSF5xE8ye/ n1f6MkJrOTpUFci+fuAcClrn0hEq2CPg/1C4JuLOHSHt2whMFdGaPs9I82bQ4Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YL8JV05BCzZ48; Sat, 28 Dec 2024 17:20:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4BSHKPiE062036; Sat, 28 Dec 2024 17:20:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BSHKPNT062033; Sat, 28 Dec 2024 17:20:25 GMT (envelope-from git) Date: Sat, 28 Dec 2024 17:20:25 GMT Message-Id: <202412281720.4BSHKPNT062033@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 030f48f78f96 - main - pthread_setcancelstate(3): make it async-signal-safe List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 030f48f78f96e0cdb30c960e1a11e5ae01d0eee8 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=030f48f78f96e0cdb30c960e1a11e5ae01d0eee8 commit 030f48f78f96e0cdb30c960e1a11e5ae01d0eee8 Author: Konstantin Belousov AuthorDate: 2024-12-23 06:42:15 +0000 Commit: Konstantin Belousov CommitDate: 2024-12-28 17:01:50 +0000 pthread_setcancelstate(3): make it async-signal-safe by setting new cancel state and reading old cancel state from the curthread structure atomic. Note that this does not play well with async cancellation, since if cancellation is enabled from a signal handler and cancellation request is pending, the thread is cancelled immediately, calling user-defined destructors, which all must be async-signal-safe (but this is a general requirement for async cancellation anyway). Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200 --- lib/libthr/thread/thr_cancel.c | 12 ++++++------ share/man/man3/pthread_testcancel.3 | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/libthr/thread/thr_cancel.c b/lib/libthr/thread/thr_cancel.c index 7622e306f937..4189a2640d14 100644 --- a/lib/libthr/thread/thr_cancel.c +++ b/lib/libthr/thread/thr_cancel.c @@ -83,22 +83,22 @@ int _thr_setcancelstate(int state, int *oldstate) { struct pthread *curthread = _get_curthread(); - int oldval; + int oldval, val; - oldval = curthread->cancel_enable; switch (state) { case PTHREAD_CANCEL_DISABLE: - curthread->cancel_enable = 0; + val = 0; break; case PTHREAD_CANCEL_ENABLE: - curthread->cancel_enable = 1; - if (curthread->cancel_async) - testcancel(curthread); + val = 1; break; default: return (EINVAL); } + oldval = atomic_swap_int(&curthread->cancel_enable, val); + if (state == PTHREAD_CANCEL_ENABLE && curthread->cancel_async) + testcancel(curthread); if (oldstate != NULL) { *oldstate = oldval ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE; diff --git a/share/man/man3/pthread_testcancel.3 b/share/man/man3/pthread_testcancel.3 index 2d2bb06c48e2..c74cdcfe943b 100644 --- a/share/man/man3/pthread_testcancel.3 +++ b/share/man/man3/pthread_testcancel.3 @@ -34,6 +34,7 @@ are .Dv PTHREAD_CANCEL_ENABLE and .Dv PTHREAD_CANCEL_DISABLE . +The function is async-signal-safe. .Pp The .Fn pthread_setcanceltype @@ -248,6 +249,11 @@ function conforms to .St -p1003.1-96 . The standard allows implementations to make many more functions cancellation points. +.Pp +The +.Fn pthread_setcancelstate +function is async-signal-safe as required by +.St -p1003.1-2024 . .Sh AUTHORS This manual page was written by .An David Leonard Aq Mt d@openbsd.org