{"id":347,"url":"https://patchwork.libcamera.org/api/patches/347/?format=json","web_url":"https://patchwork.libcamera.org/patch/347/","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":"<20190123085923.12524-4-laurent.pinchart@ideasonboard.com>","date":"2019-01-23T08:59:22","name":"[libcamera-devel,3/4] libcamera: event_dispatcher: Add interrupt() function","commit_ref":"4d470eb37fe31cc9899ee36e81134b35b02a36f3","pull_url":null,"state":"accepted","archived":false,"hash":"bf49af4b69cc004d57fbd51bf632916d27f29852","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/347/mbox/","series":[{"id":120,"url":"https://patchwork.libcamera.org/api/series/120/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=120","date":"2019-01-23T08:59:19","name":"Event dispatcher enhancements","version":1,"mbox":"https://patchwork.libcamera.org/series/120/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/347/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/347/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 C811F60C9C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 23 Jan 2019 09:59:29 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5824623E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 23 Jan 2019 09:59:29 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1548233969;\n\tbh=jGwEGD2Js39ZX+865l7YI72a5zJN+1GNZuOluD2WQac=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=jlwf+XRUlAImna14Y53dQFK8YvhZNkq1sPPWFDvQSOqQM0tVQnw+AoUiiqxp3yV4y\n\tRfc1v6KhELT3pfRWZX9xZZDQ5p240zEFfG3ov/ZfaMmA+0p/MzHLQxAsy0WEMibyA8\n\tv6jHbynKp95y6EDcgPhGklPH4UiwuwUZY+ot2tug=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed, 23 Jan 2019 10:59:22 +0200","Message-Id":"<20190123085923.12524-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.19.2","In-Reply-To":"<20190123085923.12524-1-laurent.pinchart@ideasonboard.com>","References":"<20190123085923.12524-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 3/4] libcamera: event_dispatcher: Add\n\tinterrupt() function","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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":"Wed, 23 Jan 2019 08:59:30 -0000"},"content":"The new interrupt() function allows interrupting in-progress blocking\nprocessEvents() calls. This is useful to stop running event loops.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/event_dispatcher.h          |  2 ++\n src/libcamera/event_dispatcher.cpp            | 10 ++++++\n src/libcamera/event_dispatcher_poll.cpp       | 32 ++++++++++++++++++-\n src/libcamera/include/event_dispatcher_poll.h |  3 ++\n 4 files changed, 46 insertions(+), 1 deletion(-)","diff":"diff --git a/include/libcamera/event_dispatcher.h b/include/libcamera/event_dispatcher.h\nindex c20518c6866d..cb06bf208e12 100644\n--- a/include/libcamera/event_dispatcher.h\n+++ b/include/libcamera/event_dispatcher.h\n@@ -26,6 +26,8 @@ public:\n \tvirtual void unregisterTimer(Timer *timer) = 0;\n \n \tvirtual void processEvents() = 0;\n+\n+\tvirtual void interrupt() = 0;\n };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/event_dispatcher.cpp b/src/libcamera/event_dispatcher.cpp\nindex f7c40734095e..b82c59c3f5dc 100644\n--- a/src/libcamera/event_dispatcher.cpp\n+++ b/src/libcamera/event_dispatcher.cpp\n@@ -104,4 +104,14 @@ EventDispatcher::~EventDispatcher()\n  * it before returning.\n  */\n \n+/**\n+ * \\fn EventDispatcher::interrupt()\n+ * \\brief Interrupt any running processEvents() call as soon as possible\n+ *\n+ * Calling this function interrupts any blocking processEvents() call in\n+ * progress. The processEvents() function will return as soon as possible,\n+ * after processing pending timers and events. If processEvents() isn't in\n+ * progress, it will be interrupted immediately the next time it gets called.\n+ */\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp\nindex 6e0609c34ddc..a2674ab31135 100644\n--- a/src/libcamera/event_dispatcher_poll.cpp\n+++ b/src/libcamera/event_dispatcher_poll.cpp\n@@ -8,7 +8,10 @@\n #include <algorithm>\n #include <iomanip>\n #include <poll.h>\n+#include <stdint.h>\n #include <string.h>\n+#include <sys/eventfd.h>\n+#include <unistd.h>\n \n #include <libcamera/event_notifier.h>\n #include <libcamera/timer.h>\n@@ -43,10 +46,18 @@ static const char *notifierType(EventNotifier::Type type)\n \n EventDispatcherPoll::EventDispatcherPoll()\n {\n+\t/*\n+\t * Create the event fd. Failures are fatal as we can't implement an\n+\t * interruptible dispatcher without the fd.\n+\t */\n+\teventfd_ = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);\n+\tif (eventfd_ < 0)\n+\t\tLOG(Event, Fatal) << \"Unable to create eventfd\";\n }\n \n EventDispatcherPoll::~EventDispatcherPoll()\n {\n+\tclose(eventfd_);\n }\n \n void EventDispatcherPoll::registerEventNotifier(EventNotifier *notifier)\n@@ -123,11 +134,13 @@ void EventDispatcherPoll::processEvents()\n \n \t/* Create the pollfd array. */\n \tstd::vector<struct pollfd> pollfds;\n-\tpollfds.reserve(notifiers_.size());\n+\tpollfds.reserve(notifiers_.size() + 1);\n \n \tfor (auto notifier : notifiers_)\n \t\tpollfds.push_back({ notifier.first, notifier.second.events(), 0 });\n \n+\tpollfds.push_back({ eventfd_, POLLIN, 0 });\n+\n \t/* Wait for events and process notifiers and timers. */\n \tdo {\n \t\tret = poll(&pollfds);\n@@ -137,12 +150,20 @@ void EventDispatcherPoll::processEvents()\n \t\tret = -errno;\n \t\tLOG(Event, Warning) << \"poll() failed with \" << strerror(-ret);\n \t} else if (ret > 0) {\n+\t\tprocessInterrupt(pollfds.back());\n+\t\tpollfds.pop_back();\n \t\tprocessNotifiers(pollfds);\n \t}\n \n \tprocessTimers();\n }\n \n+void EventDispatcherPoll::interrupt()\n+{\n+\tuint64_t value = 1;\n+\twrite(eventfd_, &value, sizeof(value));\n+}\n+\n short EventDispatcherPoll::EventNotifierSetPoll::events() const\n {\n \tshort events = 0;\n@@ -186,6 +207,15 @@ int EventDispatcherPoll::poll(std::vector<struct pollfd> *pollfds)\n \t\t     nextTimer ? &timeout : nullptr, nullptr);\n }\n \n+void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd)\n+{\n+\tif (!pfd.revents & POLLIN)\n+\t\treturn;\n+\n+\tuint64_t value;\n+\tread(eventfd_, &value, sizeof(value));\n+}\n+\n void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pollfds)\n {\n \tstatic const struct {\ndiff --git a/src/libcamera/include/event_dispatcher_poll.h b/src/libcamera/include/event_dispatcher_poll.h\nindex ac3efde082b4..1c0066c24dc8 100644\n--- a/src/libcamera/include/event_dispatcher_poll.h\n+++ b/src/libcamera/include/event_dispatcher_poll.h\n@@ -31,6 +31,7 @@ public:\n \tvoid unregisterTimer(Timer *timer);\n \n \tvoid processEvents();\n+\tvoid interrupt();\n \n private:\n \tstruct EventNotifierSetPoll {\n@@ -40,8 +41,10 @@ private:\n \n \tstd::map<int, EventNotifierSetPoll> notifiers_;\n \tstd::list<Timer *> timers_;\n+\tint eventfd_;\n \n \tint poll(std::vector<struct pollfd> *pollfds);\n+\tvoid processInterrupt(const struct pollfd &pfd);\n \tvoid processNotifiers(const std::vector<struct pollfd> &pollfds);\n \tvoid processTimers();\n };\n","prefixes":["libcamera-devel","3/4"]}