[libcamera-devel,v4,3/6] py: Use UniqueFD
diff mbox series

Message ID 20220819111615.39814-4-tomi.valkeinen@ideasonboard.com
State Accepted
Commit f4933ee77f40b4861f2dd513b913065ac1bad96f
Headers show
Series
  • Python bindings: PyCameraManager and non-blocking eventfd
Related show

Commit Message

Tomi Valkeinen Aug. 19, 2022, 11:16 a.m. UTC
Use UniqueFD to automate the eventfd lifetime management.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/py/libcamera/py_camera_manager.cpp | 16 ++++------------
 src/py/libcamera/py_camera_manager.h   |  4 ++--
 2 files changed, 6 insertions(+), 14 deletions(-)

Patch
diff mbox series

diff --git a/src/py/libcamera/py_camera_manager.cpp b/src/py/libcamera/py_camera_manager.cpp
index 228fb212..18fdb623 100644
--- a/src/py/libcamera/py_camera_manager.cpp
+++ b/src/py/libcamera/py_camera_manager.cpp
@@ -29,25 +29,17 @@  PyCameraManager::PyCameraManager()
 		throw std::system_error(errno, std::generic_category(),
 					"Failed to create eventfd");
 
-	eventFd_ = fd;
+	eventFd_ = UniqueFD(fd);
 
 	int ret = cameraManager_->start();
-	if (ret) {
-		close(fd);
-		eventFd_ = -1;
+	if (ret)
 		throw std::system_error(-ret, std::generic_category(),
 					"Failed to start CameraManager");
-	}
 }
 
 PyCameraManager::~PyCameraManager()
 {
 	LOG(Python, Debug) << "~PyCameraManager()";
-
-	if (eventFd_ != -1) {
-		close(eventFd_);
-		eventFd_ = -1;
-	}
 }
 
 py::list PyCameraManager::cameras()
@@ -95,7 +87,7 @@  void PyCameraManager::writeFd()
 {
 	uint64_t v = 1;
 
-	size_t s = write(eventFd_, &v, 8);
+	size_t s = write(eventFd_.get(), &v, 8);
 	/*
 	 * We should never fail, and have no simple means to manage the error,
 	 * so let's log a fatal error.
@@ -108,7 +100,7 @@  void PyCameraManager::readFd()
 {
 	uint8_t buf[8];
 
-	if (read(eventFd_, buf, 8) != 8)
+	if (read(eventFd_.get(), buf, 8) != 8)
 		throw std::system_error(errno, std::generic_category());
 }
 
diff --git a/src/py/libcamera/py_camera_manager.h b/src/py/libcamera/py_camera_manager.h
index 9c15f814..710163e8 100644
--- a/src/py/libcamera/py_camera_manager.h
+++ b/src/py/libcamera/py_camera_manager.h
@@ -24,7 +24,7 @@  public:
 
 	static const std::string &version() { return CameraManager::version(); }
 
-	int eventFd() const { return eventFd_; }
+	int eventFd() const { return eventFd_.get(); }
 
 	std::vector<pybind11::object> getReadyRequests();
 
@@ -33,7 +33,7 @@  public:
 private:
 	std::unique_ptr<CameraManager> cameraManager_;
 
-	int eventFd_ = -1;
+	UniqueFD eventFd_;
 	std::mutex completedRequestsMutex_;
 	std::vector<Request *> completedRequests_;