Patch Detail
Show a patch.
GET /api/1.1/patches/156/?format=api
{ "id": 156, "url": "https://patchwork.libcamera.org/api/1.1/patches/156/?format=api", "web_url": "https://patchwork.libcamera.org/patch/156/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20190106023328.10989-7-laurent.pinchart@ideasonboard.com>", "date": "2019-01-06T02:33:24", "name": "[libcamera-devel,07/11] libcamera: Add a poll-based event dispatcher", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "1ba97cbfac76bd76e88f84b6d152bcd393507f61", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/1.1/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/156/mbox/", "series": [ { "id": 53, "url": "https://patchwork.libcamera.org/api/1.1/series/53/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=53", "date": "2019-01-06T02:33:18", "name": "[libcamera-devel,01/11] libcamera: log: Add a LogFatal log level", "version": 1, "mbox": "https://patchwork.libcamera.org/series/53/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/156/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/156/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 25F6160B30\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 6 Jan 2019 03:32:29 +0100 (CET)", "from avalon.bb.dnainternet.fi\n\t(dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 89558513\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 6 Jan 2019 03:32:28 +0100 (CET)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1546741948;\n\tbh=py70DnK3sBvfhGAMM2t+fS8lbvYWXP2or1PGF+FJlyU=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=i6JCtQJcoNcGQ4U20PTk7cOFJc3eIbH0tStTwAMKOUJzI11HMNIUdly+uEulv3o4Q\n\tgfdu6Xw6OLqviIRsJgbBES5htfAY+LlAnT34O7AOJJxRLws+bTVNPvjKgmpIWwBB7h\n\tthHp22RNTDc0GnV9hv7hPeCgwxl3H7bOgZNuFtmU=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Sun, 6 Jan 2019 04:33:24 +0200", "Message-Id": "<20190106023328.10989-7-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.19.2", "In-Reply-To": "<20190106023328.10989-1-laurent.pinchart@ideasonboard.com>", "References": "<20190106023328.10989-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH 07/11] libcamera: Add a poll-based event\n\tdispatcher", "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": "Sun, 06 Jan 2019 02:32:29 -0000" }, "content": "Provide a poll-based event dispatcher implementation as convenience for\napplications that don't need a custom event loop. The poll-based\ndispatcher is automatically instantiated if the application doesn't\nprovide its own dispatcher.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/libcamera.h | 1 +\n src/libcamera/camera_manager.cpp | 19 +-\n src/libcamera/event_dispatcher_poll.cpp | 232 ++++++++++++++++++\n src/libcamera/include/event_dispatcher_poll.h | 50 ++++\n src/libcamera/meson.build | 2 +\n 5 files changed, 299 insertions(+), 5 deletions(-)\n create mode 100644 src/libcamera/event_dispatcher_poll.cpp\n create mode 100644 src/libcamera/include/event_dispatcher_poll.h", "diff": "diff --git a/include/libcamera/libcamera.h b/include/libcamera/libcamera.h\nindex 785babefa1c8..2dcaeda49812 100644\n--- a/include/libcamera/libcamera.h\n+++ b/include/libcamera/libcamera.h\n@@ -10,5 +10,6 @@\n #include <libcamera/camera.h>\n #include <libcamera/camera_manager.h>\n #include <libcamera/event_dispatcher.h>\n+#include <libcamera/event_dispatcher_poll.h>\n \n #endif /* __LIBCAMERA_LIBCAMERA_H__ */\ndiff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\nindex 0dbf31f47039..4475ab1f9db2 100644\n--- a/src/libcamera/camera_manager.cpp\n+++ b/src/libcamera/camera_manager.cpp\n@@ -9,6 +9,7 @@\n #include <libcamera/event_dispatcher.h>\n \n #include \"device_enumerator.h\"\n+#include \"event_dispatcher_poll.h\"\n #include \"log.h\"\n #include \"pipeline_handler.h\"\n \n@@ -188,9 +189,10 @@ CameraManager *CameraManager::instance()\n * \\param dispatcher Pointer to the event dispatcher\n *\n * libcamera requires an event dispatcher to integrate event notification and\n- * timers with the application event loop. Applications shall call this function\n- * once and only once before the camera manager is started with start() to set\n- * the event dispatcher.\n+ * timers with the application event loop. Applications that want to provide\n+ * their own event dispatcher shall call this function once and only once before\n+ * the camera manager is started with start(). If no event dispatcher is\n+ * provided, a default poll-based implementation will be used.\n *\n * The CameraManager takes ownership of the event dispatcher and will delete it\n * when the application terminates.\n@@ -208,11 +210,18 @@ void CameraManager::setEventDispatcher(EventDispatcher *dispatcher)\n /**\n * \\fn CameraManager::eventDispatcher()\n * \\brief Retrieve the event dispatcher\n- * \\return Pointer to the event dispatcher, or nullptr if no event dispatcher\n- * has been set\n+ *\n+ * This function retrieves the event dispatcher set with setEventDispatcher().\n+ * If no dispatcher has been set, a default poll-based implementation is created\n+ * and returned.\n+ *\n+ * \\return Pointer to the event dispatcher\n */\n EventDispatcher *CameraManager::eventDispatcher()\n {\n+\tif (!dispatcher_)\n+\t\tdispatcher_ = new EventDispatcherPoll();\n+\n \treturn dispatcher_;\n }\n \ndiff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp\nnew file mode 100644\nindex 000000000000..2c64c1446772\n--- /dev/null\n+++ b/src/libcamera/event_dispatcher_poll.cpp\n@@ -0,0 +1,232 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * event_dispatcher_poll.cpp - Poll-based event dispatcher\n+ */\n+\n+#include <algorithm>\n+#include <iomanip>\n+#include <poll.h>\n+#include <string.h>\n+\n+#include <libcamera/event_notifier.h>\n+#include <libcamera/timer.h>\n+\n+#include \"event_dispatcher_poll.h\"\n+#include \"log.h\"\n+\n+/**\n+ * \\file event_dispatcher_poll.h\n+ */\n+\n+namespace libcamera {\n+\n+static const char *notifierType(EventNotifier::Type type)\n+{\n+\tif (type == EventNotifier::Read)\n+\t\treturn \"read\";\n+\tif (type == EventNotifier::Write)\n+\t\treturn \"write\";\n+\tif (type == EventNotifier::Exception)\n+\t\treturn \"exception\";\n+\n+\treturn \"\";\n+}\n+\n+/**\n+ * \\class EventDispatcherPoll\n+ * \\brief A poll-based event dispatcher\n+ */\n+\n+EventDispatcherPoll::EventDispatcherPoll()\n+{\n+}\n+\n+EventDispatcherPoll::~EventDispatcherPoll()\n+{\n+}\n+\n+void EventDispatcherPoll::registerEventNotifier(EventNotifier *notifier)\n+{\n+\tEventNotifierSetPoll &set = notifiers_[notifier->fd()];\n+\tEventNotifier::Type type = notifier->type();\n+\n+\tif (set.notifiers[type] && set.notifiers[type] != notifier)\n+\t\tLOG(Warning) << \"Duplicate \" << notifierType(type)\n+\t\t\t << \" notifiers for fd \" << notifier->fd();\n+\n+\tset.notifiers[type] = notifier;\n+}\n+\n+void EventDispatcherPoll::unregisterEventNotifier(EventNotifier *notifier)\n+{\n+\tauto iter = notifiers_.find(notifier->fd());\n+\tif (iter == notifiers_.end())\n+\t\treturn;\n+\n+\tEventNotifierSetPoll &set = iter->second;\n+\tEventNotifier::Type type = notifier->type();\n+\n+\tif (!set.notifiers[type])\n+\t\treturn;\n+\n+\tif (set.notifiers[type] != notifier) {\n+\t\tLOG(Warning) << \"Duplicate \" << notifierType(type)\n+\t\t\t << \" notifiers for fd \" << notifier->fd();\n+\t\treturn;\n+\t}\n+\n+\tset.notifiers[type] = nullptr;\n+\tif (!set.notifiers[0] && !set.notifiers[1] && !set.notifiers[2])\n+\t\tnotifiers_.erase(iter);\n+}\n+\n+void EventDispatcherPoll::registerTimer(Timer *timer)\n+{\n+\tfor (auto iter = timers_.begin(); iter != timers_.end(); ++iter) {\n+\t\tif ((*iter)->deadline() > timer->deadline()) {\n+\t\t\ttimers_.insert(iter, timer);\n+\t\t\treturn;\n+\t\t}\n+\t}\n+\n+\ttimers_.push_back(timer);\n+}\n+\n+void EventDispatcherPoll::unregisterTimer(Timer *timer)\n+{\n+\tfor (auto iter = timers_.begin(); iter != timers_.end(); ++iter) {\n+\t\tif (*iter == timer) {\n+\t\t\ttimers_.erase(iter);\n+\t\t\treturn;\n+\t\t}\n+\n+\t\t/*\n+\t\t * As the timers list is ordered, we can stop as soon as we go\n+\t\t * past the deadline.\n+\t\t */\n+\t\tif ((*iter)->deadline() > timer->deadline())\n+\t\t\tbreak;\n+\t}\n+}\n+\n+void EventDispatcherPoll::processEvents()\n+{\n+\tint ret;\n+\n+\t/* Create the pollfd array. */\n+\tstd::vector<struct pollfd> pollfds;\n+\tpollfds.reserve(notifiers_.size());\n+\n+\tfor (auto notifier : notifiers_)\n+\t\tpollfds.push_back({ notifier.first, notifier.second.events(), 0 });\n+\n+\t/* Compute the timeout. */\n+\tTimer *nextTimer = !timers_.empty() ? timers_.front() : nullptr;\n+\tstruct timespec timeout;\n+\n+\tif (nextTimer) {\n+\t\tclock_gettime(CLOCK_MONOTONIC, &timeout);\n+\t\tuint64_t now = timeout.tv_sec * 1000000000ULL + timeout.tv_nsec;\n+\n+\t\tif (nextTimer->deadline() > now) {\n+\t\t\tuint64_t delta = nextTimer->deadline() - now;\n+\t\t\ttimeout.tv_sec = delta / 1000000000ULL;\n+\t\t\ttimeout.tv_nsec = delta % 1000000000ULL;\n+\t\t} else {\n+\t\t\ttimeout.tv_sec = 0;\n+\t\t\ttimeout.tv_nsec = 0;\n+\t\t}\n+\n+\t\tLOG(Debug) << \"timeout \" << timeout.tv_sec << \".\"\n+\t\t\t << std::setfill('0') << std::setw(9)\n+\t\t\t << timeout.tv_nsec;\n+\t}\n+\n+\t/* Wait for events and process notifers and timers. */\n+\tret = ppoll(pollfds.data(), pollfds.size(),\n+\t\t nextTimer ? &timeout : nullptr, nullptr);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(Warning) << \"poll() failed with \" << strerror(-ret);\n+\t} else if (ret > 0) {\n+\t\tprocessNotifiers(pollfds);\n+\t}\n+\n+\tprocessTimers();\n+}\n+\n+short EventDispatcherPoll::EventNotifierSetPoll::events() const\n+{\n+\tshort events = 0;\n+\n+\tif (notifiers[EventNotifier::Read])\n+\t\tevents |= POLLIN;\n+\tif (notifiers[EventNotifier::Write])\n+\t\tevents |= POLLOUT;\n+\tif (notifiers[EventNotifier::Exception])\n+\t\tevents |= POLLPRI;\n+\n+\treturn events;\n+}\n+\n+void EventDispatcherPoll::processNotifiers(std::vector<struct pollfd> &pollfds)\n+{\n+\tstatic const struct {\n+\t\tEventNotifier::Type type;\n+\t\tshort events;\n+\t} events[] = {\n+\t\t{ EventNotifier::Read, POLLIN },\n+\t\t{ EventNotifier::Write, POLLOUT },\n+\t\t{ EventNotifier::Exception, POLLPRI },\n+\t};\n+\n+\tfor (const struct pollfd &pfd : pollfds) {\n+\t\tauto iter = notifiers_.find(pfd.fd);\n+\t\tASSERT(iter == notifiers_.end());\n+\n+\t\tEventNotifierSetPoll &set = iter->second;\n+\n+\t\tfor (const auto &event : events) {\n+\t\t\tEventNotifier *notifier = set.notifiers[event.type];\n+\n+\t\t\tif (!notifier)\n+\t\t\t\tcontinue;\n+\n+\t\t\t/*\n+\t\t\t * If the file descriptor is invalid, disable the\n+\t\t\t * notifier immediately.\n+\t\t\t */\n+\t\t\tif (pfd.revents & POLLNVAL) {\n+\t\t\t\tLOG(Warning) << \"Disabling \" << notifierType(event.type)\n+\t\t\t\t\t << \" due to invalid file descriptor \"\n+\t\t\t\t\t << pfd.fd;\n+\t\t\t\tunregisterEventNotifier(notifier);\n+\t\t\t}\n+\n+\t\t\tif (pfd.revents & event.events)\n+\t\t\t\tnotifier->activated.emit(notifier);\n+\t\t}\n+\t}\n+}\n+\n+void EventDispatcherPoll::processTimers()\n+{\n+\tstruct timespec ts;\n+\tuint64_t now;\n+\tclock_gettime(CLOCK_MONOTONIC, &ts);\n+\tnow = ts.tv_sec * 1000000000ULL + ts.tv_nsec;\n+\n+\twhile (!timers_.empty()) {\n+\t\tTimer *timer = timers_.front();\n+\t\tif (timer->deadline() > now)\n+\t\t\tbreak;\n+\n+\t\ttimers_.pop_front();\n+\t\ttimer->stop();\n+\t\ttimer->timeout.emit(timer);\n+\t}\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/include/event_dispatcher_poll.h b/src/libcamera/include/event_dispatcher_poll.h\nnew file mode 100644\nindex 000000000000..600289e455e2\n--- /dev/null\n+++ b/src/libcamera/include/event_dispatcher_poll.h\n@@ -0,0 +1,50 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * event_loop.h - Camera object interface\n+ */\n+#ifndef __LIBCAMERA_EVENT_DISPATCHER_POLL_H__\n+#define __LIBCAMERA_EVENT_DISPATCHER_POLL_H__\n+\n+#include <libcamera/event_dispatcher.h>\n+\n+#include <list>\n+#include <map>\n+#include <vector>\n+\n+namespace libcamera {\n+\n+class EventNotifier;\n+class Timer;\n+\n+class EventDispatcherPoll final : public EventDispatcher\n+{\n+public:\n+\tEventDispatcherPoll();\n+\t~EventDispatcherPoll();\n+\n+\tvoid registerEventNotifier(EventNotifier *notifier);\n+\tvoid unregisterEventNotifier(EventNotifier *notifier);\n+\n+\tvoid registerTimer(Timer *timer);\n+\tvoid unregisterTimer(Timer *timer);\n+\n+\tvoid processEvents();\n+\n+private:\n+\tstruct EventNotifierSetPoll {\n+\t\tshort events() const;\n+\t\tEventNotifier *notifiers[3];\n+\t};\n+\n+\tstd::map<int, EventNotifierSetPoll> notifiers_;\n+\tstd::list<Timer *> timers_;\n+\n+\tvoid processNotifiers(std::vector<struct pollfd> &pollfds);\n+\tvoid processTimers();\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_EVENT_DISPATCHER_POLL_H__ */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 61fb93205b34..abf9a71d4172 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -3,6 +3,7 @@ libcamera_sources = files([\n 'camera_manager.cpp',\n 'device_enumerator.cpp',\n 'event_dispatcher.cpp',\n+ 'event_dispatcher_poll.cpp',\n 'event_notifier.cpp',\n 'log.cpp',\n 'media_device.cpp',\n@@ -14,6 +15,7 @@ libcamera_sources = files([\n \n libcamera_headers = files([\n 'include/device_enumerator.h',\n+ 'include/event_dispatcher_poll.h',\n 'include/log.h',\n 'include/media_device.h',\n 'include/media_object.h',\n", "prefixes": [ "libcamera-devel", "07/11" ] }