[{"id":32989,"web_url":"https://patchwork.libcamera.org/comment/32989/","msgid":"<20250109195014.GE6159@pendragon.ideasonboard.com>","date":"2025-01-09T19:50:14","subject":"Re: [PATCH v2] Thread: Fix setThreadAffinity race condition in start","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Harvey,\n\nThank you for the patch.\n\nOn Tue, Jan 07, 2025 at 02:41:37PM +0000, Harvey Yang wrote:\n> Previously we call Thread::setThreadAffinityInternal in\n> Thread::startThread. The purpose was to avoid the main workload being\n> run on incorrect CPUs. This leads to a race condition of setting\n> `Thread::thread_` in `Thread::start()` and accessing\n> `Thread::setThreadAffinityInternal` though.\n> \n> This patch moves the call after the construction of std::thread to avoid\n> the race condition. The downside is that the first tasks, if any, upon\n> starting a thread might be run on incorrect CPUs.\n> \n> Fixes: 4d9db06d6690 (\"libcamera: add method to set thread affinity\")\n> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/libcamera/base/thread.cpp | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n> \n> diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\n> index f6322fe3108b..319bfda9ae98 100644\n> --- a/src/libcamera/base/thread.cpp\n> +++ b/src/libcamera/base/thread.cpp\n> @@ -257,6 +257,8 @@ void Thread::start()\n>  \tdata_->exit_.store(false, std::memory_order_relaxed);\n>  \n>  \tthread_ = std::thread(&Thread::startThread, this);\n> +\n> +\tsetThreadAffinityInternal();\n>  }\n>  \n>  void Thread::startThread()\n> @@ -284,8 +286,6 @@ void Thread::startThread()\n>  \tdata_->tid_ = syscall(SYS_gettid);\n>  \tcurrentThreadData = data_;\n>  \n> -\tsetThreadAffinityInternal();\n> -\n>  \trun();\n>  }\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 11618C32EA\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  9 Jan 2025 19:50:20 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1FC70684EA;\n\tThu,  9 Jan 2025 20:50:19 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 84D8A61880\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  9 Jan 2025 20:50:17 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7B68E9FC;\n\tThu,  9 Jan 2025 20:49:23 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"SQGV0VOB\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1736452163;\n\tbh=FbfYJDxOFIuswzA9bQtcpBCWXZgqRsmjKRZHjqRcvJc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=SQGV0VOB8cGo1pJFfaH6uxdVXfX0z64uFV09IVXYzPPEUQgwMWvgv8Pv/k3XlzfH5\n\tLhSjeCFugk14Z6L5I8UF4avsA7D9fwd289Jznqu8ATFjIY8rav0Nmxku8jbgRFo5Qe\n\tpRuoImFcdUhn1vkRNM8tH/TbvF5XksSdpgI8X6q8=","Date":"Thu, 9 Jan 2025 21:50:14 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Harvey Yang <chenghaoyang@chromium.org>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2] Thread: Fix setThreadAffinity race condition in start","Message-ID":"<20250109195014.GE6159@pendragon.ideasonboard.com>","References":"<20250107144141.3416094-1-chenghaoyang@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20250107144141.3416094-1-chenghaoyang@chromium.org>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]