From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.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 B8F8F2D061B for ; Mon, 16 Feb 2026 11:26:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771241176; cv=none; b=Y/xQvYg0VmrCrJZAQAaeUjOT+SJlYMkiBdmAPzKWqt20qwk0A+2iuSKDdnSvnxf1F2cnOLijiE06xjW8vDOMfpfhhaZZHnKSG6tIyGdLjIJSNEgL7C/RWQS1/yUDg3dBQLbF2XOGVbWqfvmZ9w47SeKQumUvWSIO5ajiORE+jA4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771241176; c=relaxed/simple; bh=2fY95UUg/tePenE+HYC6jvdp1NVukK9WIF2RG5D26kA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MSF8y3HCofBpuVRsFpbXNx4qAFQ2coq35jX9EhFiVuGit2lPzqRSFkwiG/aWpz5VzTa12RBXG1fk0Y8e666i7opd4udDxeuLtgfoUQjTcC9gFComCQbPQiEfJLRP9rk4TyEtlgFPB00HsQ8o+kPjY0xwXwDYxE0P4A8WX2jmW/4= 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=pvjO8jQP; arc=none smtp.client-ip=209.85.128.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="pvjO8jQP" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4803e8b6007so25371055e9.0 for ; Mon, 16 Feb 2026 03:26:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771241172; x=1771845972; 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=yGMbsyUuSVMTufVvZcwyil4vhH6NmOuJFN5COuJwEj4=; b=pvjO8jQPhHapmAASGv6VeT6JmSjAYCnEArLjuurgvdPUSVwbBRjoeSWywECcxAM0f5 DQNbR9T008LUfC0nA6F5Kml/aO3cEHRPN2+V2HjRet4Bu2ADslFPJ9NnZ4xcpdBA9JNX HTKkJaqsd3VM+koR+U24zZaSCTo3ScG8nG1u7a6EZ2VNXIccJXUOPMbdePC4dhebryBa SFdczlIYBz2kQC0RSxcznhbn3IAxS6a7J4XOZF/a1NE2R9LHq/9EAXXJySmPhEEwz8eB rnrGbbsh6WDAc2AUk62O0e8F5F2jE/0EMrBzcM/vlG7MRBS8sycLdA17fLzDvH5DihpO 5l9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771241172; x=1771845972; 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=yGMbsyUuSVMTufVvZcwyil4vhH6NmOuJFN5COuJwEj4=; b=em32kEEOOgjNO51tlXkabwMJvMW7TCk6/KsYPL7wULkPRcRvmwnb2zVXeMeErBPblO eiCBhkPWWoZ4jP/1GDZlI5keU5EyAYxpgU1SuFJ+4d8Ek2xZESvkHgJKSC/uY+sr0oZ0 buyF6jNBzu76SD0+XB7xNvakjEV6vgLKzHwT4ys2JIGid2hkk8RW/T8NzJoFGKN+Bh4y YEdmi/+MjcF0zBYIt1xf72TkqTaR7KhEFd1sCeqvqtgnIshyI//4Xun2mIG/ZnXJuwht 4dOCcYCtRygFOk4eRImt80DshehEGSWTN7PsDbDCeFvh4JRVqdDqcTy3qrwF1r8ybvtz lFKg== X-Forwarded-Encrypted: i=1; AJvYcCXi0VkoHLJnyVgRuwGk9o8aySYL8X9V45vcvMXU64xgp4RCUNoTHQ1nlGgKDj09JvaUm41dIDOcxTgbBB8=@vger.kernel.org X-Gm-Message-State: AOJu0YzPVFGIt7V0/MjmW3KkFikYUfYDyzoF0uRR7WNclAJp7w1DGMWD c1VVaXGr2OyDN5xvio3h945z7ELSc5JlFdrGWCQGULpbnK9+lBEirW7wmasGHdP18Pg3vtCH3XT 585iFAuDCiKd5rrFkog== X-Received: from wmbdn10.prod.google.com ([2002:a05:600c:654a:b0:483:6c98:3d2f]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e1d:b0:477:7ab8:aba with SMTP id 5b1f17b1804b1-48373a15f1bmr164805795e9.1.1771241172055; Mon, 16 Feb 2026 03:26:12 -0800 (PST) Date: Mon, 16 Feb 2026 11:26:11 +0000 In-Reply-To: <875x7xdjvr.fsf@t14s.mail-host-address-is-not-set> 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 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. I suppose we might be able to use include/linux/bit_spinlock.h here to avoid taking up any additional space. Alice [1]: https://lore.kernel.org/all/CAH5fLggY2Ei14nVJzLBEoR1Rut1GKU4SZX=+14tuRH1aSuQVTA@mail.gmail.com/