From nobody Wed Aug 02 08:12:06 2023 X-Original-To: freebsd-current@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 4RG4TZ16HCz4qPgZ for ; Wed, 2 Aug 2023 08:12:28 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (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 "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RG4TH72wjz3H00; Wed, 2 Aug 2023 08:12:19 +0000 (UTC) (envelope-from theraven@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690963940; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VRfpbFpba+aFanVU03R3qG5yUbt/f0sJViScGxYkRyA=; b=iE4Sv+UAmE14EUhY9VB/UJnDxkcO2PENs4KxVtHnt3QhzmNhXoVG5DOLZHJzccFgs+3Q7L Y2+oJTb1zzkTttHHK/tg+Pa3EZ38W2qix8fLyesbX+XIJ1P+G6kJQpQ0llIGSY9okst5vp gWRdMKJ/PMjZmmcymE9GIPYk0Ua+iB+dtC+oVOtSWfPxZRNui574HNSVVA8POMIfo0d0FO h+jX/iO8/CreH2Fcswwkrt0URJSgIuwXMRb+OFfnHdo5fUZ7uxoA7qlX/X5DhrLt/ig3iq QYNix+qPs9VhlC/c91A7jBKD24+bn3jfBhEqyKNb1m0yEzLEN6KyivZnTCxIWQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1690963940; a=rsa-sha256; cv=none; b=elbdiIcqV8ud5PWpePh/FM6Fgk7NQ5BxAje23Jek+qdJ5jhmhaneaeNtbiJD1UlK81CsN3 bZvyP8TyXOCJy1uJf37dolr0DAXyjI0vLd4O9mt9eeawG78Wrgmq+z6AoVJ+Z35C9tHTqB IFJwP1JB2B61WQP5CYkWGRxXKBvZLhG5FOk7h1rcDliUtz6H2G1VpZsDWyu4ZFLKWGskW+ JbNwEqrsbEnD8ONKDF2JShKYq77f/s1HuJ1v3f25cEt2M7p+QkzdFPP3hBUhSc5DWWXqjE KC+1afP/3JZUoDogkeUUFqsIJw/RlncJvueW81QDvvPjIu/uEaS4s30YRPQHCg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690963940; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VRfpbFpba+aFanVU03R3qG5yUbt/f0sJViScGxYkRyA=; b=Opn/0pF8pzkOdBLU0MxuelhpaObcZ18tcTbWfebFx+zGjJhvRnnxqTQFYmd7qLOtMyT5H+ SjzBjsvIOyGMbHFDGSA5+XQA6j/DWDL3W74z7SYRsTUFl92LCrQIWXFT/IdRa92eA/oIhP KonI501dymvLR39Nd5ytSofW4QSjJdiPrVgD0EkzTgBT1ti72g6tQHhnQTC0lHmzAgbr23 py+sRa1qjTOx1WF5pA/pq0jtpzIZe6uXkSlUc4thIKzu5C4omjpH9S2iUFIyqbB9F/qZt1 RmISY254SlBXaq/AfFqMKwu9R66o/mM+4vWjO0P0rdiSDAhgO5eZWfBM0WzYbQ== Received: from smtp.theravensnest.org (smtp.theravensnest.org [45.77.103.195]) (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) (Authenticated sender: theraven) by smtp.freebsd.org (Postfix) with ESMTPSA id 4RG4TH3Y8Rz17ym; Wed, 2 Aug 2023 08:12:19 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from smtpclient.apple (host86-141-212-56.range86-141.btcentralplus.com [86.141.212.56]) by smtp.theravensnest.org (Postfix) with ESMTPSA id DF572889C; Wed, 2 Aug 2023 09:12:17 +0100 (BST) Content-Type: text/plain; charset=utf-8 List-Id: Discussions about the use of FreeBSD-current List-Archive: https://lists.freebsd.org/archives/freebsd-current List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-current@freebsd.org Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) Subject: Re: Review of patch that uses "volatile sig_atomic_t" From: David Chisnall In-Reply-To: Date: Wed, 2 Aug 2023 09:12:06 +0100 Cc: FreeBSD CURRENT Content-Transfer-Encoding: quoted-printable Message-Id: <9A5C0082-2FFD-4B2B-9381-7ACD9EB7C4DF@FreeBSD.org> References: To: Rick Macklem X-Mailer: Apple Mail (2.3731.600.7) On 2 Aug 2023, at 00:33, Rick Macklem wrote: >=20 > Just trying to understand what you are suggesting... > 1 - Declare the variable _Atomic(int) OR atomic_int (is there a = preference) and > not volatile. Either is fine (the latter is a typedef for the former). I am not a = huge fan of the typedefs, some people like them, as far as I can tell = it=E2=80=99s purely personal preference. > 2 - Is there a need for signal_atomic_fence(memory_order_acquire); = before the > assignment of the variable in the signal handler. (This exists in > one place in > the source tree (bin/dd/misc,c), although for this example, > neither volatile nor > _Atomic() are used for the variable's declaration. You don=E2=80=99t need a fence if you use an atomic variable. The fence = prevents the compiler reordering things across it, using atomic = operations also prevents this. You might be able to use a fence and not = use an atomic but I=E2=80=99d have to reread the spec very carefully to = convince myself that this didn=E2=80=99t trigger undefined behaviour. > 3 - Is there any need for other atomic_XXX() calls where the variable = is used > outside of the signal handler? No. By default, _Atomic variables use sequentially consistent = semantics. You need to use the `atomic_` functions only for explicit = memory orderings, which you might want to do for optimisation (very = unlikely in this case). Reading it outside the signal handler is the = equivalent of doing `atomic_load` with a sequentially consistent memory = order. This is a stronger guarantee than you need, but it=E2=80=99s = unlikely to cause performance problems if you=E2=80=99re doing more than = a few dozen instructions worth of work between checks. > In general, it is looking like FreeBSD needs to have a standard way of = dealing > with this and there will be assorted places that need to be fixed? If we used a language that let you build abstractions, that would be = easy (I have a C++ class that provides a static epoch counter that=E2=80=99= s incremented in a signal handler and a local copy for each instance, so = you can check if the signal handler has fired since it was last used. = It=E2=80=99s trivial to reuse in C++ projects but C doesn=E2=80=99t give = you tools for doing this. David