Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4525090rdb; Tue, 12 Dec 2023 01:51:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6Z3DSK/SYQqd1RJMTxsvrHqA0dD6kVtmgFDowBg36sHQd6Mu8CjBlx5RYWzBhrHI9gyKW X-Received: by 2002:a05:6358:7296:b0:170:17ea:f4e4 with SMTP id w22-20020a056358729600b0017017eaf4e4mr7102387rwf.49.1702374708267; Tue, 12 Dec 2023 01:51:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702374708; cv=none; d=google.com; s=arc-20160816; b=V8jJu6DloyHiYIW2SwmpOfvEN6SvLVfTeIxF/lEhQbgJw8LvSqhoWps3L8ts+gMvfJ Llh50VsJh3u9aVYUnlxNruNjmxBmgGa+unxYsC+kyGuxzjQYYTLWMnmsx6rXs8J7ZIPg ZJc2l3ny+XCmN8UITAn8BBucO0Ck3++lCrdE2RD/cb9dQrJr9WMARCjwJkgMAPZ0stnp +PNLag7b4gv81x2IUpGrH9X8ez8rHz6sCdQRVSJolsUihvij6UUaUw8kU1uGSq7kHQ8K aJP0IxF9SNG+tNsghEotlzwc9AbBgsYhVmnD5Symz56L4RwYt8flAIYikUKt+53td6RQ 3l3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ko5c0vwQVCFkqxt5MmDEPN+O3PpUovqfwlzC3CsXMJQ=; fh=Y/j3Xy31fqx3qjVbSbUd1KzYacOvx5EVPbyqrkdLByU=; b=tMQ7YEQkQSdw9+9IYXiMXc/lU6U806FSxHAIQG/1ZEXE9x+RHbliVRn34d0BN/I8A8 VyS6UHNP6Dsw8FDRwuIcLUKECYW0dj3DcU7AujZZBXKP2aqFjY4FjidGA4UDvON7u2ld VGPyQKbnOXq0jQxzlsMyBQmhkAWRuTpCYE1BD42rv35ShXpv2/86a8QSigaBl3rnEmF9 QbOfXm1gX41XQtXnP7vtdrXhzXDXp9QhceExoCvyqbD/z9YL6H8iEx2IydtbYnBoFHoE sLQV/BuJJyYUZzwbGCrx6Qlb2AwDm6Ry/voQqOPEMIKAdumCFvws5j5mjB2SxVO/Nsur UtqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uPD90YCX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id w3-20020a17090aad4300b0028a7de5f772si4103403pjv.119.2023.12.12.01.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 01:51:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uPD90YCX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 7C9808082702; Tue, 12 Dec 2023 01:51:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231322AbjLLJva (ORCPT + 99 others); Tue, 12 Dec 2023 04:51:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbjLLJv2 (ORCPT ); Tue, 12 Dec 2023 04:51:28 -0500 Received: from mail-vk1-xa29.google.com (mail-vk1-xa29.google.com [IPv6:2607:f8b0:4864:20::a29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91AAD2 for ; Tue, 12 Dec 2023 01:51:34 -0800 (PST) Received: by mail-vk1-xa29.google.com with SMTP id 71dfb90a1353d-4b328087918so1149233e0c.0 for ; Tue, 12 Dec 2023 01:51:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702374694; x=1702979494; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ko5c0vwQVCFkqxt5MmDEPN+O3PpUovqfwlzC3CsXMJQ=; b=uPD90YCXVz5exp9+Z2ehj6Ua2ncGLn+AXtor6zp0CgItEgQMlAO27YWxlwXdwaDBfq SGyV66zrp3atzyH8GhaPxD7azUlbJqrnaACm+Xmz/O/VDWOegE1LDRW72H3NZV5GfExu B2BU/o5InxGdLPGw+y2MJPYJDjmNFfpcaCnAcwvySk3Qge5xGYR00d1l7TN0ssTRwepU 4NfM4I2fpZak4b+wbIihuVODPvyehjZhBja2aGciEVeHFWx42GaAgVF1MYND68ugdPKn SOKPZtt2YfgLls+LpsOFzhxlKUVcPzi0xB+AksdeBzcPTMQ2LS8SVDLg0HJ2LesO+Z+d eBBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702374694; x=1702979494; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ko5c0vwQVCFkqxt5MmDEPN+O3PpUovqfwlzC3CsXMJQ=; b=mEX+5CrS87UYK9E8nBdNG5Y8Emeo9C7Yl/p6fhhDEpIeJW9hZFtuZnAnn1+V1Gxxod u+R1LsQ7LgNW0tNISS4U1AHeZtnWH+J3Y3rLoKWgF4IPgOvATQFsWeguW8iBsa3Cvdkk tTQ8hyc2BEnyLGAZhGY04jPbu1kXjqnziaQQfaVoUlwLtOFbVsf4sFbEoYwR1ZGQ+Kak D3VgulbXkeDlWsg2M7ll0xS5te5R0WrC9s5TOZmzQOTTDqoUmRSwph6wIxw6cp+YpI+w IoosbDnYD7+rvlZcW3XVXaz8dB0jvypmq/gYCAgnWGAtmJx6aY2vrBloPqRWdLb8fi8D s7lA== X-Gm-Message-State: AOJu0YwJcyMyobwm5vqIOpRYhZqpYB7UXsWtdoEozm7UQuYlF0Mleuoh RfeMXYYjmfaj/iEtPxlV9+euaig6kIDxsiZyg3iqPA== X-Received: by 2002:a05:6122:a08:b0:4b2:deeb:d9c5 with SMTP id 8-20020a0561220a0800b004b2deebd9c5mr4247201vkn.15.1702374693492; Tue, 12 Dec 2023 01:51:33 -0800 (PST) MIME-Version: 1.0 References: <20231206-rb-new-condvar-methods-v1-0-33a4cab7fdaa@google.com> <20231206-rb-new-condvar-methods-v1-2-33a4cab7fdaa@google.com> In-Reply-To: From: Alice Ryhl Date: Tue, 12 Dec 2023 10:51:22 +0100 Message-ID: Subject: Re: [PATCH 2/2] rust: sync: add `CondVar::wait_timeout` To: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 01:51:45 -0800 (PST) On Fri, Dec 8, 2023 at 8:04=E2=80=AFPM Benno Lossin wrote: > > On 12/6/23 11:09, Alice Ryhl wrote: > > + /// Atomically releases the given lock (whose ownership is proven = by the guard) and puts the > > + /// thread to sleep. It wakes up when notified by [`CondVar::notif= y_one`] or > > + /// [`CondVar::notify_all`], or when the thread receives a signal. > > + /// > > + /// Returns whether there is a signal pending. > > + fn wait_internal_timeout( > > + &self, > > + wait_state: u32, > > + guard: &mut Guard<'_, T, B>, > > + timeout: u64, > > + ) -> u64 > > + where > > + T: ?Sized, > > + B: Backend, > > + { > > + let wait =3D Opaque::::uninit(); > > + > > + // SAFETY: `wait` points to valid memory. > > + unsafe { bindings::init_wait(wait.get()) }; > > + > > + // SAFETY: Both `wait` and `wait_list` point to valid memory. > > + unsafe { > > + bindings::prepare_to_wait_exclusive(self.wait_list.get(), = wait.get(), wait_state as _) > > Does `.into()` work here? If for some reason the type here changes, we > probably want to know about it. I think we may be able to eliminate this cast by using c_int for the integer type. > > + }; > > + > > + // SAFETY: Switches to another thread. > > + let timeout =3D > > + guard.do_unlocked(|| unsafe { bindings::schedule_timeout(t= imeout as _) as _ }); > > Ditto. Here, we're casting u64->long and then long->u64. How about this? u64->long - Use timeout.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT), since MAX_SCHEDULE_TIMEOUT is LONG_MAX. long->u64 - This value is guaranteed to be less than the argument passed to schedule_timeout. Use .into() for infallible cast. Alice