{"id":22674,"url":"https://patchwork.libcamera.org/api/patches/22674/?format=json","web_url":"https://patchwork.libcamera.org/patch/22674/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20250130115001.1129305-8-pobrn@protonmail.com>","date":"2025-01-30T11:50:36","name":"[RFC,v3,07/21] apps: common: event_loop: Make it possible to exit with exception","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"f6759f1d3ef9a5b9f4a2359fcdad42e7f2ec62d3","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/people/133/?format=json","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22674/mbox/","series":[{"id":4978,"url":"https://patchwork.libcamera.org/api/series/4978/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4978","date":"2025-01-30T11:50:08","name":"apps: lc-compliance: Multi-stream tests","version":3,"mbox":"https://patchwork.libcamera.org/series/4978/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22674/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22674/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 B549ABDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 Jan 2025 11:51:35 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2B4446857F;\n\tThu, 30 Jan 2025 12:51:35 +0100 (CET)","from mail-10631.protonmail.ch (mail-10631.protonmail.ch\n\t[79.135.106.31])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EE3476857E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Jan 2025 12:51:31 +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=\"GVWVCYaN\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1738237891; x=1738497091;\n\tbh=NUhrDH+R6HSzEFO8Hof78/dy7krBWAL7vNTi4zrPXrY=;\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=GVWVCYaN5/iPLofg/GD/TfzViRuAgtpgZfts7Vg+5W0vDg1GwkZ20lHg55hRxa/68\n\tDqkkvixdkJNtnTIuCZFdXfAtAzcGOp26sX++NxlG5tPMw1PTxnAaZMRQe+BF1+s6Cp\n\tC4AGE47TO4qP098cFBYqogV4KX3bNSUYFClulo/g33+ZS+QVJdaZjSIIdZs2hHk/Dq\n\t2Mn/qtc9cGMSabLjF7dINOnT0/rLJS8hQpLUbTAY9XsxolBe9t01ClcJlpEYpjAZ9B\n\tlKRx6DjAjiqZZ7kxoBR/MhGNkhR33r76ppYcljrcr1zGWAQbQztKkroXl/9chOM+mU\n\tOwoXu0vkxbHTA==","Date":"Thu, 30 Jan 2025 11:50:36 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Subject":"[RFC PATCH v3 07/21] apps: common: event_loop: Make it possible to\n\texit with exception","Message-ID":"<20250130115001.1129305-8-pobrn@protonmail.com>","In-Reply-To":"<20250130115001.1129305-1-pobrn@protonmail.com>","References":"<20250130115001.1129305-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"1d14dc4b50c39baa5b13ef346ff4914946cd24f8","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":"Instead of taking a single integer in `EventLoop:exit()`, change it\nso that an `std::exception_ptr` can also be passed. And in `EventLoop::exec()`\nrethrow the stored exception (if any).\n\nFurthermore, catch exceptions from deferred calls, and stop the event\nloop with the caught exception.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n---\n src/apps/common/event_loop.cpp | 35 +++++++++++++++++++++++++++++-----\n src/apps/common/event_loop.h   |  6 ++++--\n 2 files changed, 34 insertions(+), 7 deletions(-)","diff":"diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp\nindex 0d7a4a024..13c2d6057 100644\n--- a/src/apps/common/event_loop.cpp\n+++ b/src/apps/common/event_loop.cpp\n@@ -12,6 +12,7 @@\n #include <event2/thread.h>\n #include <algorithm>\n #include <iostream>\n+#include <utility>\n \n EventLoop *EventLoop::instance_ = nullptr;\n \n@@ -38,7 +39,14 @@ EventLoop::EventLoop()\n \t\t\t\tself->calls_.pop_front();\n \t\t\t}\n \n-\t\t\tcall();\n+\t\t\ttry {\n+\t\t\t\tcall();\n+\t\t\t}\n+\t\t\tcatch (...) {\n+\t\t\t\t::event_active(self->callsTrigger_, 0, 0);\n+\t\t\t\tself->exit(std::current_exception());\n+\t\t\t\tbreak;\n+\t\t\t}\n \t\t}\n \t}, this);\n \tassert(callsTrigger_);\n@@ -63,14 +71,31 @@ EventLoop *EventLoop::instance()\n \n int EventLoop::exec()\n {\n-\texitCode_ = -1;\n+\t{\n+\t\tstd::lock_guard locker(lock_);\n+\t\tresult_ = 0;\n+\t}\n+\n \tevent_base_loop(base_, EVLOOP_NO_EXIT_ON_EMPTY);\n-\treturn exitCode_;\n+\n+\tauto result = [&] {\n+\t\tstd::lock_guard locker(lock_);\n+\t\treturn std::exchange(result_, 0);\n+\t}();\n+\n+\tif (auto *exc = std::get_if<std::exception_ptr>(&result))\n+\t\tstd::rethrow_exception(std::move(*exc));\n+\n+\treturn std::get<int>(result);\n }\n \n-void EventLoop::exit(int code)\n+void EventLoop::exit(std::variant<int, std::exception_ptr> result)\n {\n-\texitCode_ = code;\n+\t{\n+\t\tstd::lock_guard locker(lock_);\n+\t\tresult_ = std::move(result);\n+\t}\n+\n \tevent_base_loopbreak(base_);\n }\n \ndiff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h\nindex 6d7d0497a..380c3483a 100644\n--- a/src/apps/common/event_loop.h\n+++ b/src/apps/common/event_loop.h\n@@ -9,11 +9,13 @@\n \n #include <chrono>\n #include <deque>\n+#include <exception>\n #include <functional>\n #include <list>\n #include <memory>\n #include <mutex>\n #include <optional>\n+#include <variant>\n \n #include <libcamera/base/class.h>\n \n@@ -35,7 +37,7 @@ public:\n \tstatic EventLoop *instance();\n \n \tint exec();\n-\tvoid exit(int code = 0);\n+\tvoid exit(std::variant<int, std::exception_ptr> result = 0);\n \n \tvoid callLater(std::function<void()> &&func, std::optional<std::uintptr_t> cookie = {});\n \tvoid cancelLater(std::optional<std::uintptr_t> cookie = {});\n@@ -63,7 +65,7 @@ private:\n \tstatic EventLoop *instance_;\n \n \tstruct event_base *base_;\n-\tint exitCode_;\n+\tstd::variant<int, std::exception_ptr> result_ = 0;\n \n \tstd::deque<std::pair<std::function<void()>, std::optional<std::uintptr_t>>> calls_;\n \t::event *callsTrigger_ = nullptr;\n","prefixes":["RFC","v3","07/21"]}