Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqvz7-00CIwJ-8z for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Mar 2025 15:21:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tqvz5-002UC6-8u for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Mar 2025 15:21:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqvz4-002U5i-6m for pgsql-hackers@lists.postgresql.org; Sat, 08 Mar 2025 15:21:02 +0000 Received: from fhigh-b2-smtp.messagingengine.com ([202.12.124.153]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tqvyz-001hTq-2V for pgsql-hackers@postgresql.org; Sat, 08 Mar 2025 15:21:01 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.stl.internal (Postfix) with ESMTP id 97CB225401DA; Sat, 8 Mar 2025 10:20:56 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Sat, 08 Mar 2025 10:20:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1741447256; x=1741533656; bh=oHUc+UvLGMrcMfyQ8/Gk3mVPm5142Lj9FlnxRjoxid8=; b= ISo87/o9l0YdTmqEBL+mMVOn7pn1bUyFYUz9Zf+kA2lQknzyv1yJKDkSgdX1+XcX E5rqcqi1qkiafM0PGNcPPHm8ofJeEP0IiyGxp2nxbW9qMUvsoa7xB2SAMww68FID b85JgCNsN3u6oCbPCVhJFszDpgn2LbprZ5qp3NXiUaUIUagVQzNEejFFurBRSUtB G8kiXig43BTZwOdLY/vll9LQ7oHQynGwFxrmm38uXepKYIiiIdSvkacFsv6D18j/ nrmdcFSXKd85QxxaNYpzMLIbIrxpYnftHwqk8EzYGrZ0giiIk7mTirT1tYeFgVp0 mvTvGFwtOBMhWkBhLVUjDA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1741447256; x= 1741533656; bh=oHUc+UvLGMrcMfyQ8/Gk3mVPm5142Lj9FlnxRjoxid8=; b=C zb07LAErNHXVvbQa1wIA9wJHCPhoZWG9SA/L5wVkVHD/YcBpTjkqhDuhOOF0lD6A xyj8PKOkYdr2fU7W19P46BUvzLOD8u+YtvGVVmd5aFHL8FKT+J5pcUgc9RgUPsYA 1AgIqBy0YcoOWM7FIUBdKs3uncoSzJTRdSz1SEFfxhIA80k98e7hk+xoKxk91nI0 FEJF6qrLMmOMLRy23CbfHUtl0GFYGSkA1m/RIOBX1Yd3OUTIetnVie+5RQzzp0gp c4+WgqIRqVC330DvW+AW3fnrvagEl7yirLVdLIH+8P1l/nNPa+6B2HJvpkTtcIMI KcRd5bvpbODcSCvE8Bvrg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduudefkeelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggugfgjsehtkefstddt tdejnecuhfhrohhmpeetnhgurhgvshcuhfhrvghunhguuceorghnughrvghssegrnhgrrh griigvlhdruggvqeenucggtffrrghtthgvrhhnpedtleelvdfgjedvffeiueekfeeuleff hfegfffhgfffkeevueehieehhfeigffhvdenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpegrnhgurhgvshesrghnrghrrgiivghlrdguvgdpnhgs pghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheprggvkhhorh hothhkohhvsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghr shesphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 8 Mar 2025 10:20:55 -0500 (EST) Date: Sat, 8 Mar 2025 10:20:55 -0500 From: Andres Freund To: Alexander Korotkov Cc: pgsql-hackers Subject: Re: pg_atomic_compare_exchange_*() and memory barriers Message-ID: References: <6ybtypq2v3kvskiqj7izl2rmfrcluilsmbobtpylcnp7moa7vq@2q3cplokvcza> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2025-03-08 17:06:38 +0200, Alexander Korotkov wrote: > On Sat, Mar 8, 2025 at 3:41 PM Andres Freund wrote: > > > > On 2025-03-08 08:02:41 -0500, Andres Freund wrote: > > > From the C/C++ standard atomics model it doesn't make sense to say that a > > > failed CAS has release semantics, as there simply isn't a write that could be > > > ordered! What their barriers guarantee is ordering between multiple memory > > > operation, you can't order multiple writes if you don't have multiple > > > writes... The synchronization in the C/C++ model is only established between > > > accesses of the same variable and there's no write in the case of a failed > > > CAS, so there's nothing that could establish a release-acquire ordering. > > > > > > Unfortunately that model doesn't mesh well with barriers that aren't attached > > > to read/modify operations. Which is what we ended up with... > > > > Adding a full barrier to failed CAS would be a rather large overhead, > > undesirable in just about any sane algorithm. As a consequence, I think what > > we ought to do is to redefine the barrier semantics to only imply an acquire > > barrier in case CAS fails. > > Thank you, I'm good with this solution. Can I leave this on you? I'm > not feeling myself strong to word this correctly. Not in the next ~four weeks. If you ping me afterwards, I can give it a go. FWIW, I am fairly certain that any non-toy algorithm that requires a full memory barrier instead of just an acquire in case of a CAS failure is chock full of concurrency bugs. Greetings, Andres Freund