{"id":22658,"url":"https://patchwork.libcamera.org/api/1.1/patches/22658/?format=json","web_url":"https://patchwork.libcamera.org/patch/22658/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250128130308.509297-2-pobrn@protonmail.com>","date":"2025-01-28T13:03:14","name":"[RFC,v1] libcamera: thread: Fix event dispatcher creation","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"040e0393a2418f1bb9e0ab15c0ad1ad705b9b600","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/1.1/people/133/?format=json","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22658/mbox/","series":[{"id":4976,"url":"https://patchwork.libcamera.org/api/1.1/series/4976/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4976","date":"2025-01-28T13:03:14","name":"[RFC,v1] libcamera: thread: Fix event dispatcher creation","version":1,"mbox":"https://patchwork.libcamera.org/series/4976/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22658/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22658/checks/","tags":{},"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 C2B25C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 28 Jan 2025 13:03:20 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5B03368560;\n\tTue, 28 Jan 2025 14:03:20 +0100 (CET)","from mail-40133.protonmail.ch (mail-40133.protonmail.ch\n\t[185.70.40.133])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7220768557\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Jan 2025 14:03:17 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"nUnxPQvM\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1738069396; x=1738328596;\n\tbh=8le9BDiluVhvE6bS3IDOvjbE9lOeI2hp6AyJOkHeS5M=;\n\th=Date:To:From:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=nUnxPQvMKfrbWL87/Lso9Pk0OhWy3TrSdL8nxlqkYyQVEHCtwa8L6wx/Vj1hrp4KG\n\t9z1TelSn814ZJA6LJqctzhJA1dhtz1jBPvy7fviTtfKW1HSkzdYhoMfTilLuS+Q8EY\n\tlXMMmcBuXIblQSpQPkvjppT8IRzaal5zEFjrXzXEXf7WW4mtRNqb1YI0SpERiT7hT7\n\tUBbf5cU7JnkoTUvpHQbzYAEoD2QB19hhJvpJJbEegUSay9kT0iNZJRclI4ltaUYSk6\n\tB8VyEX24WM3SGC+yIZ3vIthd73wIIZPzd/1aGLLmh3QJSxNcRLBi2Jf0ICuIZL699C\n\tUiATNaXpFqFJw==","Date":"Tue, 28 Jan 2025 13:03:14 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Subject":"[RFC PATCH v1] libcamera: thread: Fix event dispatcher creation","Message-ID":"<20250128130308.509297-2-pobrn@protonmail.com>","In-Reply-To":"<20250128130308.509297-1-pobrn@protonmail.com>","References":"<20250128130308.509297-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"c8d93ac1df3e9d1c6871197173378f27b7fce573","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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>"},"content":"Use an appropriate compare-and-exchange atomic instruction\nto set the event dispatcher pointer. This ensures that there\nis only ever a single event dispatcher associated with a\ngiven thread.\n\nConsider e.g. the following series of events:\n\n1. thread 0 :: if (!data_->dispatcher_.load(std::memory_order_relaxed))\n               // reads nullptr and proceeds into the `if`'s block\n\n2. thread 1 :: if (!data_->dispatcher_.load(std::memory_order_relaxed))\n               // reads nullptr and proceeds into the `if`'s block\n\n3. thread 0 :: data_->dispatcher_.store(new EventDispatcherPoll(), std::memory_order_release);\n               // creates dispatcher and sets pointer\n\n4. thread 1 :: data_->dispatcher_.store(new EventDispatcherPoll(), std::memory_order_release);\n               // creates dispatcher and sets pointer\n\nAt the end, one of the event dispatchers is leaked, and\nfurthermore the `eventDispatcher()` calls might return\ndifferent values in the calling threads.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n---\n src/libcamera/base/thread.cpp | 13 +++++++++----\n 1 file changed, 9 insertions(+), 4 deletions(-)\n\n--\n2.48.1","diff":"diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\nindex de60567f6..93ec7b5a0 100644\n--- a/src/libcamera/base/thread.cpp\n+++ b/src/libcamera/base/thread.cpp\n@@ -518,11 +518,16 @@ pid_t Thread::currentId()\n  */\n EventDispatcher *Thread::eventDispatcher()\n {\n-\tif (!data_->dispatcher_.load(std::memory_order_relaxed))\n-\t\tdata_->dispatcher_.store(new EventDispatcherPoll(),\n-\t\t\t\t\t std::memory_order_release);\n+\tif (EventDispatcher *dispatcher = data_->dispatcher_.load(std::memory_order_acquire))\n+\t\treturn dispatcher;\n\n-\treturn data_->dispatcher_.load(std::memory_order_relaxed);\n+\tauto dispatcher = std::make_unique<EventDispatcherPoll>();\n+\tEventDispatcher *expected = nullptr;\n+\n+\tif (data_->dispatcher_.compare_exchange_strong(expected, dispatcher.get(), std::memory_order_acq_rel))\n+\t\treturn dispatcher.release();\n+\n+\treturn expected;\n }\n\n /**\n","prefixes":["RFC","v1"]}