From patchwork Tue Jan 7 14:41:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 22456 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 309EEC32DC for ; Tue, 7 Jan 2025 14:41:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2BD9B684E2; Tue, 7 Jan 2025 15:41:50 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="lkdjYUMc"; dkim-atps=neutral Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 59D2B684CD for ; Tue, 7 Jan 2025 15:41:48 +0100 (CET) Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-21649a7bcdcso222336925ad.1 for ; Tue, 07 Jan 2025 06:41:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1736260906; x=1736865706; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lEJs8QbGX1h8G/RP+rIHsVRX37JJ+I5a9diiDBxG02E=; b=lkdjYUMcmOu5G+lpRGvfbC7kyEkx1ZrtAaJI5/wLcVNKCFI6Xaq5O85rq3JF34+Buf UZNoP7sHIv+850oQiCozvTc8iu6bcwHBy/N7PTPMz7VMxdDCk3XaGZMnURQ6Mvn1OFtD r/FqCCFi5PRR4cQsH/uZmKJ10mejyYj3v11c8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736260906; x=1736865706; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lEJs8QbGX1h8G/RP+rIHsVRX37JJ+I5a9diiDBxG02E=; b=uNw2UIA+d8SLoBJyy88H8BwpRBd1N9VafK31fpiKb4OgRK7Xtg/tCcQ9XY4BuL2t7+ C5hHIjt/AaPD50gWHgnCCWi1zbLQY8MkO67Jv4u25J1hAVgc4qMNgcfgxhIJPhxKzLly xxUky/dgWVgSLQzE4zoNIfy2CWbrnDYTvK0wJXP9CmLH5aDGqCQJxSWr0UKgKEuWKOvz mMZjI8Imwubddw7LRMFedzoHQNpw5dCNpgcQNCQ9nvEJeOP2iBsBTWckblxZ8B/Y8YXu 5Evvoy31rZ0MIbtvJtR+sqXkRZmdGxlp6B625veSHonkT/s7bt1lgPZRrNj26VouX0jA 9c4w== X-Gm-Message-State: AOJu0Yys3vQDeEC0TZ8coGKAzZW6nsGsuz3ROxb2rQ7p+pjwZlhJ08Ie X+eVFcgS6O4NVi0KLQ8IULu/s6sLCmsIywxyLZhjA3B5Ll8RI+xKDn7HiFWGVSnAj8l7/M4PADU = X-Gm-Gg: ASbGncsckBmOjhbH8k32OBvdEKqi1O/j3krtJoF6BVSoxM/OSfMWpKHfgAaJGPhaTdC ehOuE+fuIZ0bPSo2BDpEMj+FmG9U2HCJMA60crWKpJwIxCAAL2sH+mWaZV3+AE0fy4SnQJd/6Oz lFy0EeyCxdIH/taklQFN/ieS2R+loL489ye1hiYBhxK1QTbtZ6ddHkxhX+jC+Oh6/DDEc1CX0tM wA47xXNTxPY73zyGbQo82ddNZvLKt6rvJL2iOQ92bF2Qij8lBD4F2xd/W+mK8WV8DkF7v5pE/cA N2Jxv2ynflETWBThNs4A1EbbCs9xbiw/BCr6XFmYr7MMkDlKygZ3AJLQ6sLFduE= X-Google-Smtp-Source: AGHT+IEao42DQFq+Br6SSZ2i+cLJegtZROqMJQVXEGVWuQf4D+Uo1vV/Qcpq43O41o6oUUZhWZFeSg== X-Received: by 2002:a17:902:e5cc:b0:215:577b:ab77 with SMTP id d9443c01a7336-219e70bf185mr889076745ad.39.1736260906575; Tue, 07 Jan 2025 06:41:46 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (21.160.199.104.bc.googleusercontent.com. [104.199.160.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc96e867sm313355965ad.71.2025.01.07.06.41.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 06:41:46 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang Subject: [PATCH v2] Thread: Fix setThreadAffinity race condition in start Date: Tue, 7 Jan 2025 14:41:37 +0000 Message-ID: <20250107144141.3416094-1-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Previously we call Thread::setThreadAffinityInternal in Thread::startThread. The purpose was to avoid the main workload being run on incorrect CPUs. This leads to a race condition of setting `Thread::thread_` in `Thread::start()` and accessing `Thread::setThreadAffinityInternal` though. This patch moves the call after the construction of std::thread to avoid the race condition. The downside is that the first tasks, if any, upon starting a thread might be run on incorrect CPUs. Fixes: 4d9db06d6690 ("libcamera: add method to set thread affinity") Signed-off-by: Harvey Yang Reviewed-by: Laurent Pinchart --- src/libcamera/base/thread.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp index f6322fe3108b..319bfda9ae98 100644 --- a/src/libcamera/base/thread.cpp +++ b/src/libcamera/base/thread.cpp @@ -257,6 +257,8 @@ void Thread::start() data_->exit_.store(false, std::memory_order_relaxed); thread_ = std::thread(&Thread::startThread, this); + + setThreadAffinityInternal(); } void Thread::startThread() @@ -284,8 +286,6 @@ void Thread::startThread() data_->tid_ = syscall(SYS_gettid); currentThreadData = data_; - setThreadAffinityInternal(); - run(); }