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 1r1dt9-00CZAE-IL for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Nov 2023 02:38:23 +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 1r1dt8-006LIs-0Q for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Nov 2023 02:38:22 +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 1r1dt7-006LIk-Mp for pgsql-hackers@lists.postgresql.org; Sat, 11 Nov 2023 02:38:21 +0000 Received: from mail-io1-xd33.google.com ([2607:f8b0:4864:20::d33]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r1dt4-005pXf-OB for pgsql-hackers@postgresql.org; Sat, 11 Nov 2023 02:38:20 +0000 Received: by mail-io1-xd33.google.com with SMTP id ca18e2360f4ac-7a93b7fedb8so93057539f.3 for ; Fri, 10 Nov 2023 18:38:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699670296; x=1700275096; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MHnVli/aLu5geKt7Qu9t8aSt2olfhI5XxxCSETTfnR0=; b=GEq2DzRMhBDVOG/NxXu02VMCNK6Dvu/Iwkw3buzxu/uFt2IA9C1yuCbh8M1O14iI2E Yt5FYtLKvmbjTmmyGUoS/UHupiRZS8AVwk5ULZdq0ac/BPNJTVM1prAG/2pqNaoy3gNe uKgDjwKH0JFQ+f+WKZa7bIjsKbdHXle6MzBJb6AappqiazWNvc7ooL40c0/MEKJf4McL JAFYGgPDXIVv5+b0HvuDYM9Ss4UKb4H+rhC21nFtbbtyWhJORR7RN+L42mFWgnGgK09d G2KHxUpY3OamB92QJ1r1J8Fz2W2xp6VFGXvKpKlExnf9WIvuyB4zIf6xv54VH/TTihrc fDEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699670296; x=1700275096; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MHnVli/aLu5geKt7Qu9t8aSt2olfhI5XxxCSETTfnR0=; b=Im26L42CvwgGvCrhVsj/K+QmW9YeUKBUd+u6xuW1jARtFBADCEJSbG1+jagYdGskbq p0wFh+2FHkWtyDk0+KUfAbLTxbJ+aHr/g24TvFv27HY5Iax8bkyHkPdxq18cF03HGQl2 I9RdhD9Rw115dW2SEcJZuPgQO8N/+ZuPZo2YbexgVQ3rG1ptaQgSGCBI+9RttXPNojxM /RtfuJYZL+37q5N/0Z4gdl/CwDNBWcckUv+qEek3E6vuPsHio3186jhN27Mp/lGRzGUg ooCX8W1keX+0Tt2bB3ujg0N2n1fqG9GJ7WO7ATzXEnn0OQ8NzI87cDrS/HW/B3RNoRII krdQ== X-Gm-Message-State: AOJu0YwOURdJ6hNzUanBhh2lLmjwjNN0MhziZRCOIp+nfWiK4k6OHXbO Z7/QauPhv695D6qyqIxXfc7IWU0bvFc= X-Google-Smtp-Source: AGHT+IHhiL/J/m8hfcTRIGHuoNmphWwHJtYnJNI7iSkBynTA9rJPi5pgS2GO1TgtnErRJY9LjvmwBA== X-Received: by 2002:a05:6602:2ac4:b0:792:8230:c175 with SMTP id m4-20020a0566022ac400b007928230c175mr1471330iov.6.1699670295955; Fri, 10 Nov 2023 18:38:15 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id f5-20020a056638118500b00463fb834b8csm199816jas.151.2023.11.10.18.38.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 18:38:15 -0800 (PST) Date: Fri, 10 Nov 2023 20:38:13 -0600 From: Nathan Bossart To: Andres Freund Cc: pgsql-hackers@postgresql.org Subject: Re: locked reads for atomics Message-ID: <20231111023813.GB1408159@nathanxps13> References: <20231110205128.GB1315705@nathanxps13> <20231110231150.fjm77gup2i7xu6hc@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231110231150.fjm77gup2i7xu6hc@alap3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Nov 10, 2023 at 03:11:50PM -0800, Andres Freund wrote: > On 2023-11-10 14:51:28 -0600, Nathan Bossart wrote: >> + * This read is guaranteed to read the current value, > > It doesn't guarantee that *at all*. What it guarantees is solely that the > current CPU won't be doing something that could lead to reading an outdated > value. To actually ensure the value is up2date, the modifying side also needs > to have used a form of barrier (in the form of fetch_add, compare_exchange, > etc or an explicit barrier). Okay, I think I was missing that this doesn't work along with pg_atomic_write_u32() because that doesn't have any barrier semantics (probably because the spinlock version does). IIUC you'd want to use pg_atomic_exchange_u32() to write the value instead, which seems to really just be another compare/exchange under the hood. Speaking of the spinlock implementation of pg_atomic_write_u32(), I've been staring at this comment for a while and can't make sense of it: void pg_atomic_write_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val) { /* * One might think that an unlocked write doesn't need to acquire the * spinlock, but one would be wrong. Even an unlocked write has to cause a * concurrent pg_atomic_compare_exchange_u32() (et al) to fail. */ SpinLockAcquire((slock_t *) &ptr->sema); ptr->value = val; SpinLockRelease((slock_t *) &ptr->sema); } It refers to "unlocked writes," but this isn't pg_atomic_unlocked_write_u32_impl(). The original thread for this comment [0] doesn't offer any hints, either. Does "unlocked" mean something different here, such as "write without any barrier semantics?" [0] https://postgr.es/m/14947.1475690465%40sss.pgh.pa.us -- Nathan Bossart Amazon Web Services: https://aws.amazon.com