Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2090376rwl; Thu, 30 Mar 2023 06:02:51 -0700 (PDT) X-Google-Smtp-Source: AKy350bw1sP2YjATkVO9ZfArNlRKcy90l2JVzxHYaFGiAXLfSOGEs3+DUvDUsPTV6tAWFIjk+HH1 X-Received: by 2002:a62:aa15:0:b0:627:e878:1dbd with SMTP id e21-20020a62aa15000000b00627e8781dbdmr2499675pff.3.1680181371363; Thu, 30 Mar 2023 06:02:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680181371; cv=none; d=google.com; s=arc-20160816; b=uFA+dFftlkWR2d+LSng3RRB3FPVQJTx0c6aBp34/5pRsljJr6b4NO9+10QNG/MYO/6 +HLW7RveBbx/zUKKSIjvVLJwxxd76zKXa1IGYe5GWDI83XC9HkoUf0k7PQ4xdKa2aAUS s7FrPQ5Kr+uNYraSVw3O8EvK2I8htcPzSw01YYyQXPuP3iIksEFUo5VU10Kp3qKBd4iE TxfCQnS3CRE6SXQbG+oeBTStXou8+xdnKCx2kUoyAdHC7Cr8esisVhz/8cd+9kdpbApt WaTqYnQt0Q8oUfekVtbNgKXsmy5+T530FwSHFgZ1FFeqoEdxYvLOboU45BUa3zBGmujU CcLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=UL7W2tzjfyF4OgCjneaPuyZJqekRRwSCQhOyLLp43Jk=; b=s4o4jOUH/WY7bIvKAVIF1Gse910ELqXFszLRCHLjaF6VBl7JcW03ayeiLIHfvzaSup RsdYCmF7M/MPWAj1SsZ1ZrRKhrNgPRMu93th8Pr4CYb2Bwkok9MpeJCkfZ+dSGTyYW0A phk653tcfspuZ6Uy2ywtx36mVA2TJNWiP8YEKEHKhabmkznD22dsZt7AP+7JvrsJ5uKs n4hYcrPoMZw+ErY2S2Z0w7FdqXFvNHYRJbCznSBSKAp6gf95hhxaBtc7xVXdC6//uyAo JxxFxBXqWQbnXO+wK1B/CwXT1vWY56YtlfkoQ6fkw/ounh45suw029LJsB1z3rYCP8Kt g+MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=nNmNcxwT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c140-20020a624e92000000b0062a52a1c935si16556964pfb.131.2023.03.30.06.02.39; Thu, 30 Mar 2023 06:02:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=nNmNcxwT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231718AbjC3M7v (ORCPT + 99 others); Thu, 30 Mar 2023 08:59:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231721AbjC3M7o (ORCPT ); Thu, 30 Mar 2023 08:59:44 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79B6E9EF5; Thu, 30 Mar 2023 05:59:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=UL7W2tzjfyF4OgCjneaPuyZJqekRRwSCQhOyLLp43Jk=; b=nNmNcxwTdfBZooOAXeHgpqbzac YmZZ8wDDt2nH3z0jCIVkowr8x23tD6YxwwOAk6rAj8pTshUdDULUJITs7DiMEnPIaQNNw3xdsb/qx vSsgqu0QhfwhfmbKxgxIXjHuHfFU5wmgILJMSy7WPBjDxoisImaE5nTo6nCecH1MlxmDov5Gf6gqS +v1s0zvqKVRA0nacCNdep+UhUke0JaJ65D8q2wX+C4CNd/qsv56zEftf/ztcO05U10c5QYSgWmK03 ygAc9exCCi/hEYF8wFxKH4oREB4Ggofzi4H4gbVTTvy+peteD+OVwOOjHTHWplCF33JVEeE1dtUYT PM2lo0Pg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1phrsG-00AQFb-3c; Thu, 30 Mar 2023 12:59:28 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 7BC07300134; Thu, 30 Mar 2023 14:59:27 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 618F320134244; Thu, 30 Mar 2023 14:59:27 +0200 (CEST) Date: Thu, 30 Mar 2023 14:59:27 +0200 From: Peter Zijlstra To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Ingo Molnar , Will Deacon , Waiman Long Subject: Re: [PATCH 12/13] rust: sync: introduce `CondVar` Message-ID: <20230330125927.GD124812@hirez.programming.kicks-ass.net> References: <20230330043954.562237-1-wedsonaf@gmail.com> <20230330043954.562237-12-wedsonaf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230330043954.562237-12-wedsonaf@gmail.com> X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 30, 2023 at 01:39:53AM -0300, Wedson Almeida Filho wrote: > +impl CondVar { > + /// Constructs a new condvar initialiser. > + #[allow(clippy::new_ret_no_self)] > + pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit { > + pin_init!(Self { > + _pin: PhantomPinned, > + // SAFETY: `__init_waitqueue_head` initialises the waitqueue head, and both `name` and > + // `key` have static lifetimes so they live indefinitely. > + wait_list <- unsafe { > + Opaque::ffi_init2( > + bindings::__init_waitqueue_head, > + name.as_char_ptr(), > + key.as_ptr(), > + ) > + }, > + }) > + } > + > + fn wait_internal(&self, wait_state: u32, guard: &mut Guard<'_, T, B>) { > + let wait = 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 _) > + }; I can't read this rust gunk, but where is the condition test gone? Also, where is the loop gone to? > + > + // SAFETY: No arguments, switches to another thread. > + guard.do_unlocked(|| unsafe { bindings::schedule() }); > + > + // SAFETY: Both `wait` and `wait_list` point to valid memory. > + unsafe { bindings::finish_wait(self.wait_list.get(), wait.get()) }; > + } > + > + /// Releases the lock and waits for a notification in interruptible mode. > + /// > + /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the > + /// thread to sleep, reacquiring the lock on wake up. It wakes up when notified by > + /// [`CondVar::notify_one`] or [`CondVar::notify_all`], or when the thread receives a signal. > + /// It may also wake up spuriously. > + /// > + /// Returns whether there is a signal pending. > + #[must_use = "wait returns if a signal is pending, so the caller must check the return value"] > + pub fn wait(&self, guard: &mut Guard<'_, T, B>) -> bool { > + self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard); > + Task::current().signal_pending() > + } > + > + /// Releases the lock and waits for a notification in uninterruptible mode. > + /// > + /// Similar to [`CondVar::wait`], except that the wait is not interruptible. That is, the > + /// thread won't wake up due to signals. It may, however, wake up supirously. > + pub fn wait_uninterruptible(&self, guard: &mut Guard<'_, T, B>) { > + self.wait_internal(bindings::TASK_UNINTERRUPTIBLE, guard) > + } > + > + /// Calls the kernel function to notify the appropriate number of threads with the given flags. > + fn notify(&self, count: i32, flags: u32) { > + // SAFETY: `wait_list` points to valid memory. > + unsafe { > + bindings::__wake_up( > + self.wait_list.get(), > + bindings::TASK_NORMAL, > + count, > + flags as _, > + ) > + }; > + } > + > + /// Wakes a single waiter up, if any. > + /// > + /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost > + /// completely (as opposed to automatically waking up the next waiter). > + pub fn notify_one(&self) { > + self.notify(1, 0); > + } > + > + /// Wakes all waiters up, if any. > + /// > + /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost > + /// completely (as opposed to automatically waking up the next waiter). > + pub fn notify_all(&self) { > + self.notify(0, 0); > + } > +}