[libcamera-devel,v2,02/10] libcamera: EventDispatcherPoll: Manage fd by ScopedFD
diff mbox series

Message ID 20210610075027.523672-3-hiroh@chromium.org
State Superseded
Headers show
Series
  • Introduce ScopedFD
Related show

Commit Message

Hirokazu Honda June 10, 2021, 7:50 a.m. UTC
Manages the event file descriptor owned by EventDispatcherPoll
by ScopedFD.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/internal/event_dispatcher_poll.h |  4 +++-
 src/libcamera/event_dispatcher_poll.cpp            | 11 +++++------
 2 files changed, 8 insertions(+), 7 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/event_dispatcher_poll.h b/include/libcamera/internal/event_dispatcher_poll.h
index 33de051d..a309bb7f 100644
--- a/include/libcamera/internal/event_dispatcher_poll.h
+++ b/include/libcamera/internal/event_dispatcher_poll.h
@@ -11,6 +11,8 @@ 
 #include <map>
 #include <vector>
 
+#include <libcamera/scoped_fd.h>
+
 #include "libcamera/internal/event_dispatcher.h"
 
 struct pollfd;
@@ -48,7 +50,7 @@  private:
 
 	std::map<int, EventNotifierSetPoll> notifiers_;
 	std::list<Timer *> timers_;
-	int eventfd_;
+	ScopedFD eventfd_;
 
 	bool processingEvents_;
 };
diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp
index 456c6def..65e46b92 100644
--- a/src/libcamera/event_dispatcher_poll.cpp
+++ b/src/libcamera/event_dispatcher_poll.cpp
@@ -54,14 +54,13 @@  EventDispatcherPoll::EventDispatcherPoll()
 	 * Create the event fd. Failures are fatal as we can't implement an
 	 * interruptible dispatcher without the fd.
 	 */
-	eventfd_ = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
-	if (eventfd_ < 0)
+	eventfd_ = ScopedFD(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
+	if (!eventfd_.isValid())
 		LOG(Event, Fatal) << "Unable to create eventfd";
 }
 
 EventDispatcherPoll::~EventDispatcherPoll()
 {
-	close(eventfd_);
 }
 
 void EventDispatcherPoll::registerEventNotifier(EventNotifier *notifier)
@@ -154,7 +153,7 @@  void EventDispatcherPoll::processEvents()
 	for (auto notifier : notifiers_)
 		pollfds.push_back({ notifier.first, notifier.second.events(), 0 });
 
-	pollfds.push_back({ eventfd_, POLLIN, 0 });
+	pollfds.push_back({ eventfd_.get(), POLLIN, 0 });
 
 	/* Wait for events and process notifiers and timers. */
 	do {
@@ -176,7 +175,7 @@  void EventDispatcherPoll::processEvents()
 void EventDispatcherPoll::interrupt()
 {
 	uint64_t value = 1;
-	ssize_t ret = write(eventfd_, &value, sizeof(value));
+	ssize_t ret = write(eventfd_.get(), &value, sizeof(value));
 	if (ret != sizeof(value)) {
 		if (ret < 0)
 			ret = -errno;
@@ -230,7 +229,7 @@  void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd)
 		return;
 
 	uint64_t value;
-	ssize_t ret = read(eventfd_, &value, sizeof(value));
+	ssize_t ret = read(eventfd_.get(), &value, sizeof(value));
 	if (ret != sizeof(value)) {
 		if (ret < 0)
 			ret = -errno;