Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp419407rwe; Fri, 14 Apr 2023 05:05:14 -0700 (PDT) X-Google-Smtp-Source: AKy350af3R6TMH8IkBbUoQXv+bCf3haX8HJBYX4I2OFpVyfmdCyxwfts9gl3ZOsg2vwFt4rRoYsc X-Received: by 2002:a0d:d709:0:b0:54f:85a9:91c5 with SMTP id z9-20020a0dd709000000b0054f85a991c5mr5602467ywd.24.1681473914070; Fri, 14 Apr 2023 05:05:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681473914; cv=none; d=google.com; s=arc-20160816; b=EDNO8Sxc3gNVcZh6q9WxlMjBLueAM++xqqxsW3gmqVDz81MNYaX8EjyHeOE+mu+O+1 Ns/DKQuA6pnygrK5KlUuJWwClLp+et99Xcosho412+HQSWUHElcraqSlZLk4l67P0dXv pZDQamqPK+Vdg4jfyM19GxtreHV7xRypjRjcalAZj2fRsIp7x8XMinZDvzWNUsq9Zsto AFpSUYotpd2UUsdsk2NBHwGpE/Wi0v6EP6HJmuH9p8qYcXFS19L+0Fm6xf51e+W8c51h bOFTHE8I8mPrprOFnb8KoOIS1HZonR32v1aolMXsu85zw09tpsTnf7+CG8w08D4gWI9G jfmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:feedback-id:dkim-signature:dkim-signature; bh=2I4MzMrmxZMH1ZdqLmrFqtevk4RMUqxEQ3bkyYHe2wU=; b=ZjbsoxQXvlyMjDhDePhDOL/H+mLDqboYUdOwGD7Zk0OJcP8hljBB32YGHi3p5NH4Kw 7iizO32lErWu4qElgTJWV2k2P4PTvOB4p7bhw5WnZ1pGByDrr4LW8ZvGe48i/CZayiwP XFULz3PDr2ezlfGuukYAwzlHHoF1qU+02R0MTAJHxvgpU/GFeGZc6drwiUMAcdt2jqak X3KazHffWmWgFY65kzqbPh3pxs/zPIBkj86i3xGqe7Dsy5EdyvLq7HsGzoy9xUOMDp7j asT6YCMWDWe4N+gVJ/wR+vEFNrYsM6yzpGE5VcbCHOMrbUqkByW/M7JzttyhxBtQ26SP IdnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm3 header.b=Vyq2vmJc; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=gOvywEzY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ryhl.io Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e8-20020a0561020dc800b0042c5b58b131si993752vst.138.2023.04.14.05.04.58; Fri, 14 Apr 2023 05:05:14 -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=@ryhl.io header.s=fm3 header.b=Vyq2vmJc; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=gOvywEzY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ryhl.io Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230152AbjDNLzF (ORCPT + 99 others); Fri, 14 Apr 2023 07:55:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229752AbjDNLzE (ORCPT ); Fri, 14 Apr 2023 07:55:04 -0400 Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F71530FA; Fri, 14 Apr 2023 04:55:02 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6CF745C0060; Fri, 14 Apr 2023 07:55:01 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 14 Apr 2023 07:55:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ryhl.io; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm3; t= 1681473301; x=1681559701; bh=2I4MzMrmxZMH1ZdqLmrFqtevk4RMUqxEQ3b kyYHe2wU=; b=Vyq2vmJcTpxKjTN6uQWHwHuka1/yjJcUNUecMDbRCmDy0AIPpgn RGo4jiyr2+uEW5m8kXyStOHZQqsHw9cwg/PWmrNjbLNizzj3ZkhPmqkviD8kedK+ XcMyFdgOZLOY79y+/QOtpbsmaCQzLx3Czs2Bc4/8oByFhWrW7JvA6sE76+DzzXhA wgHOPqjXLQYW/6fnA6tlJW8ghq1DvhyjzeBmgwWla1wD2j2SGqjphzj+RG/exwB/ DqjJ8pWCbzA98skxarxEcguhJ/nzMSQbXgJBVnrKcJTW0dVpUZWlg6J4ldjjQ+6L sIF0vauVFJha4FCVd/JpPY4mygnADnOdy9g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1681473301; x=1681559701; bh=2I4MzMrmxZMH1ZdqLmrFqtevk4RMUqxEQ3b kyYHe2wU=; b=gOvywEzYW2kLxtKU6vBtR33APK/e2IMy7UOz0mzGhej4EC2LqG5 E5RsPuNE1irskwS2RxdYxNcIYLwprWksj4GRXaxmt9z7qxxPYh+wRcva3vmMOamU t3XepIOpY6ZNbx8UgVnM6YB502BYhp6UL3wBSkYXNfGsuGnqVx90K7+1FzcwlPF2 KPI+x/AVo9ivMivjKrfO6p96juiaLbo2uhu4g6TxNskFFPXYxuEfbQHdDGYS8wfk YwV8qTcW49oMT6R854QzNgz3wSXnzjTVdt0r36SwN9IzO6mXKu7qrqSnPRvHabRp Y/lorbdtkQnqZMpau6FVGVyCLnIHFa6Q0ng== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdeltddggeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeetlhhi tggvucfthihhlhcuoegrlhhitggvsehrhihhlhdrihhoqeenucggtffrrghtthgvrhhnpe ehudduvdetkedvkedtudeludfgfffhudegjeeguedvvedtteevjeehheeiffefgeenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhitggvse hrhihhlhdrihho X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 14 Apr 2023 07:54:57 -0400 (EDT) Message-ID: <433dcb57-e5d6-bb2b-5c5a-74768cccecab@ryhl.io> Date: Fri, 14 Apr 2023 13:55:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH v4 12/13] rust: sync: introduce `CondVar` To: Wedson Almeida Filho , rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Martin Rodriguez Reboredo References: <20230411054543.21278-1-wedsonaf@gmail.com> <20230411054543.21278-12-wedsonaf@gmail.com> Content-Language: en-US From: Alice Ryhl In-Reply-To: <20230411054543.21278-12-wedsonaf@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 4/11/23 07:45, Wedson Almeida Filho wrote: > From: Wedson Almeida Filho > > This is the traditional condition variable or monitor synchronisation > primitive. It is implemented with C's `wait_queue_head_t`. > > It allows users to release a lock and go to sleep while guaranteeing > that notifications won't be missed. This is achieved by enqueuing a wait > entry before releasing the lock. > > Cc: Peter Zijlstra > Cc: Ingo Molnar > Cc: Will Deacon > Cc: Waiman Long > Reviewed-by: Martin Rodriguez Reboredo > Signed-off-by: Wedson Almeida Filho Reviewed-by: Alice Ryhl I have a few methods below that the binder driver will need on the condvar. I'll let it be up to you whether you wish to include them in this patch. Otherwise, we will add them when we start upstreaming binder. > + /// 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); > + crate::current!().signal_pending() > + } The binder driver will need a `wait_timeout` method. > + /// 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); > + } Android binder will also need a `notify_sync` method. It could be implemented like this: /// Calls the kernel function to notify one thread synchronously. pub fn notify_sync(&self) { // SAFETY: `wait_list` points to valid memory. unsafe { bindings::__wake_up_sync(self.wait_list.get(), bindings::TASK_NORMAL) }; }