From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3E2A2BE644 for ; Mon, 16 Feb 2026 11:35:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771241731; cv=none; b=WKYU6QbOrcQP6Z9CnCKerD4rpgwiw40CPQXwRKD2ikIBx5a0Pdni/bBTYmwKa7FU6YWYAhCAu/O2sQ6bMlgISuMGu2s32Bbs86CKz43bG/1Tj1jQe6w3en0ku1u6PJ3AYuZoiVO41zoNkbFVeIgeRcMf8zyTf+yQdf8VmqqTBrs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771241731; c=relaxed/simple; bh=uE/mjFeTnHKZN0Len2sn8q0P9apJrQkgfX6J6zKwBhE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=icrmaMmrj4mRLD3RfsTsD/1ryQRXbLyoVPxVUWcGFC3XzMza8HGF/+Ty09f0uVamY2OOwBNli0b3Fi8zbqj995/Gb86PGNDp4IlgwJuhXpI9rhkEsVmoJ0Qvu+hUlvPYYpFS+9lXHW0r+tWIKhhsiushkkUVKguLRDq067np754= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1cxEb3vt; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1cxEb3vt" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-4363333c102so2032370f8f.1 for ; Mon, 16 Feb 2026 03:35:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771241729; x=1771846529; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=F67modHH25Y5O5ELxZwmixMWUpgeSd637tYygFtD4MM=; b=1cxEb3vtKBRj65FbFD4eB+c7UBBTJ9s5B92qlbyU/Dlr9O4s6chQDfOqOSC5xu9dMK 0zyEY+VNUI8Qn84SD/ALZW0lyMjrX7K+MbPpa/rGmldFysxZVcATFo5BMn+5GYkJ42ew rBU+npX/E3DGrUMGRl6vBH8WDRGS3QsxqJB9TkRZ3F0QppjHWHVjilWIzLUZG6Cm0ipY aZAQ9qCBwZ8w+0L0ywL8CyjLlbtGXOEIiWr4haYIo5r3mMS4V1lbyatRXmqBaQNg8P0i UZjHJWFVuTkov03QU1sFp5r3iIjR+mOhZoR9DuWv9vkh8pFobJDZzu2r2vgYcZs1ouPE kDog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771241729; x=1771846529; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=F67modHH25Y5O5ELxZwmixMWUpgeSd637tYygFtD4MM=; b=g/lQ3U06VPYDa9G65UBXp7mWTIQDKm2N88kMsvIv5xch7Hw5xGxG1Pt3wUN1+X6Djo inans1WQSHOxZxAw5pjdW2FBlW+NPJL5BWr83f4hBXqdZzMtPg9cFLaCSZoWwIIOm0bI 9BMbXRPCE5bIRNjrLRL+g760HTmtTqEabGgwUHtiRiMjg0XgX7vP7jXlBFlGQfEGMoVx jmpLJqKEA5/Ao9B6xMoUoq1gZbPKm3l7K6DSXK0fvXAnkI4lg/3Q4H16YFclHS3daJNQ 1nKfUSraByGRsji41Gi7xZeyjjb+UOkG32yreuoBwfzGX46mOHYR2v/HsTlTLOgf7iND NcqA== X-Forwarded-Encrypted: i=1; AJvYcCXy/R1WQbOhO/WHIw3nAHLcFbtj/V1Nr4EuaePMMONW376dg/6E5/Zz9RoPEeur+/gft18UZS+PaPFpoKs=@vger.kernel.org X-Gm-Message-State: AOJu0YzkgAhwYAyoAzO5RtpHzbqT6eCOFf4h22ioAXY2K3zTuimGSd7T d+05Ui9Wy8vN3wH8Df9mYUG8JWvBPTX6YV29R4bBRFXNQ9uMTp/fezoSoG5HrLU22HeaJn0ya8G EpYzqQXPzNO5EpplBDw== X-Received: from wmph41.prod.google.com ([2002:a05:600c:49a9:b0:483:509d:729b]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8486:b0:47a:7fdd:2906 with SMTP id 5b1f17b1804b1-48373a1b533mr161837985e9.12.1771241728861; Mon, 16 Feb 2026 03:35:28 -0800 (PST) Date: Mon, 16 Feb 2026 11:35:27 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260215-set-once-lazy-v1-1-6f5bd2efda11@kernel.org> <4EYpvxiPLRBBb2fUI9n8ZrVkve50KAvXwj7oFqoYhtpIuY_U7eVtiwnjA-ZR8n7jCwoJL67EI1ex2_DmBs5UMg==@protonmail.internalid> <875x7xdjvr.fsf@t14s.mail-host-address-is-not-set> Message-ID: Subject: Re: [PATCH] rust: sync: add lazy initialization methods to SetOnce From: Alice Ryhl To: Andreas Hindborg Cc: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Trevor Gross , Danilo Krummrich , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Mon, Feb 16, 2026 at 11:26:11AM +0000, Alice Ryhl wrote: > On Mon, Feb 16, 2026 at 12:10:16PM +0100, Andreas Hindborg wrote: > > "Alice Ryhl" writes: > > > > > On Sun, Feb 15, 2026 at 09:27:17PM +0100, Andreas Hindborg wrote: > > >> Add methods to get a reference to the contained value or populate the > > >> SetOnce if empty. The new `as_ref_or_populate` method accepts a value > > >> directly, while `as_ref_or_populate_with` accepts a fallible closure, > > >> allowing for lazy initialization that may fail. Both methods spin-wait > > >> if another thread is concurrently initializing the container. > > >> > > >> Also add `populate_with` which takes a fallible closure and serves as > > >> the implementation basis for the other populate methods. > > >> > > >> Signed-off-by: Andreas Hindborg > > > > > >> + /// Get a reference to the contained object, or populate the [`SetOnce`] > > >> + /// with the value returned by `callable` and return a reference to that > > >> + /// object. > > >> + pub fn as_ref_or_populate_with(&self, callable: impl FnOnce() -> Result) -> Result<&T> { > > >> + if !self.populate_with(callable)? { > > >> + while self.init.load(Acquire) != 2 { > > >> + core::hint::spin_loop(); > > >> + } > > > > > > We should not be implementing our own spinlocks. > > > > That is a great proverb. I'd be happy to receive a suggestion on an > > alternate approach for this particular context. > > You can add a spinlock to SetOnce. Like I mentioned previously [1], > support for waiting will require the addition of extra fields. By the way, back then I suggested renaming it from OnceLock to SetOnce because you did not support waiting for the value to be populated, and you said you didn't need that. If you add that feature, then we should rename it back to OnceLock, or create a new type OnceLock for users who need that additional feature. Alice