{"id":2111,"url":"https://patchwork.libcamera.org/api/1.1/patches/2111/?format=json","web_url":"https://patchwork.libcamera.org/patch/2111/","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":"<20191006053226.8976-7-laurent.pinchart@ideasonboard.com>","date":"2019-10-06T05:32:23","name":"[libcamera-devel,6/9] libcamera: timer: Forbid starting or stopping timer from another thread","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d841c588e3799286404bd75d412be4cf010ec95f","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2111/mbox/","series":[{"id":522,"url":"https://patchwork.libcamera.org/api/1.1/series/522/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=522","date":"2019-10-06T05:32:17","name":"Timer fixes and enhancements","version":1,"mbox":"https://patchwork.libcamera.org/series/522/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2111/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2111/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["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 3A4C66196E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Oct 2019 07:32:39 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(modemcable151.96-160-184.mc.videotron.ca [184.160.96.151])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 99EE2DD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Oct 2019 07:32:38 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1570339958;\n\tbh=KVVAKqGdxut9JHBGlZQRoDo4n+8wG0LViVGNO9UV+18=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=NkKnb1+DROYRB0bLbKiVRc2sTKGCve3noWzz63H2jQTX51Nss5nlFUm3rPfO45SWF\n\t9RLDATMFWw8teWcXmeSshXs/C2yZRnQRk0NqQAEwTBmb6+YgEOXQTGK1uzkW3Sb97o\n\td7PabWwCURQWBmE3x+WTeHkxB05qF5N4X53JExe8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun,  6 Oct 2019 08:32:23 +0300","Message-Id":"<20191006053226.8976-7-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20191006053226.8976-1-laurent.pinchart@ideasonboard.com>","References":"<20191006053226.8976-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 6/9] libcamera: timer: Forbid starting or\n\tstopping timer from another thread","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>","X-List-Received-Date":"Sun, 06 Oct 2019 05:32:39 -0000"},"content":"Starting or stopping a timer from a different thread than the one it\nbelongs to is inherently racy. Disallow it.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/timer.cpp | 24 +++++++++++++++++++++---\n 1 file changed, 21 insertions(+), 3 deletions(-)","diff":"diff --git a/src/libcamera/timer.cpp b/src/libcamera/timer.cpp\nindex 5d4e52713e6e..8749d66c8662 100644\n--- a/src/libcamera/timer.cpp\n+++ b/src/libcamera/timer.cpp\n@@ -36,6 +36,11 @@ LOG_DEFINE_CATEGORY(Timer)\n  * Once started the timer will run until it times out. It can be stopped with\n  * stop(), and once it times out or is stopped, can be started again with\n  * start().\n+ *\n+ * Timers run in the thread they belong to, and thus emit the \\a ref timeout\n+ * signal from that thread. To avoid race conditions they must not be started\n+ * or stopped from a different thread, attempts to do so will be rejected and\n+ * logged, and may cause undefined behaviour.\n  */\n \n /**\n@@ -57,17 +62,24 @@ Timer::~Timer()\n  * \\brief Start or restart the timer with a timeout of \\a msec\n  * \\param[in] msec The timer duration in milliseconds\n  *\n- * If the timer is already running it will be stopped and restarted.\n+ * This method shall be called from the thread the timer is associated with. If\n+ * the timer is already running it will be stopped and restarted.\n  */\n \n /**\n  * \\brief Start or restart the timer with a timeout of \\a duration\n  * \\param[in] duration The timer duration in milliseconds\n  *\n- * If the timer is already running it will be stopped and restarted.\n+ * This method shall be called from the thread the timer is associated with. If\n+ * the timer is already running it will be stopped and restarted.\n  */\n void Timer::start(std::chrono::milliseconds duration)\n {\n+\tif (Thread::current() != thread()) {\n+\t\tLOG(Timer, Error) << \"Timer can't be started from another thread\";\n+\t\treturn;\n+\t}\n+\n \tdeadline_ = utils::clock::now() + duration;\n \n \tLOG(Timer, Debug)\n@@ -87,13 +99,19 @@ void Timer::start(std::chrono::milliseconds duration)\n  * After this function returns the timer is guaranteed not to emit the\n  * \\ref timeout signal.\n  *\n- * If the timer is not running this function performs no operation.\n+ * This method shall be called from the thread the timer is associated with. If\n+ * the timer is not running this function performs no operation.\n  */\n void Timer::stop()\n {\n \tif (!isRunning())\n \t\treturn;\n \n+\tif (Thread::current() != thread()) {\n+\t\tLOG(Timer, Error) << \"Timer can't be stopped from another thread\";\n+\t\treturn;\n+\t}\n+\n \tunregisterTimer();\n }\n \n","prefixes":["libcamera-devel","6/9"]}