From patchwork Thu Feb 27 20:03:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Dufresne X-Patchwork-Id: 2886 Return-Path: Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B845762708 for ; Thu, 27 Feb 2020 21:04:19 +0100 (CET) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: nicolas) with ESMTPSA id 3A1342949DF From: Nicolas Dufresne To: libcamera-devel@lists.libcamera.org Date: Thu, 27 Feb 2020 15:03:44 -0500 Message-Id: <20200227200407.490616-5-nicolas.dufresne@collabora.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200227200407.490616-1-nicolas.dufresne@collabora.com> References: <20200227200407.490616-1-nicolas.dufresne@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 04/27] gst: utils: Add simple scoped lockers for GMutex and GRectMutex X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Feb 2020 20:04:21 -0000 While GLib has locker implementation already using g_autoptr(), recursive mutext locker was onmly introduced in recent GLib. Implement a simple locker for GMutex and GRectMutex in order to allow making locking simplier and safer. Signed-off-by: Nicolas Dufresne Reviewed-by: Laurent Pinchart --- src/gstreamer/gstlibcamera-utils.h | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/gstreamer/gstlibcamera-utils.h b/src/gstreamer/gstlibcamera-utils.h index 33160b8..737ca63 100644 --- a/src/gstreamer/gstlibcamera-utils.h +++ b/src/gstreamer/gstlibcamera-utils.h @@ -15,4 +15,55 @@ GstCaps *gst_libcamera_stream_formats_to_caps(const libcamera::StreamFormats &formats); +/** + * \class GLibLocker + * \brief A simple scoped mutex locker for GMutex + */ +class GLibLocker +{ +public: + GLibLocker(GMutex *mutex) + : mutex_(mutex) + { + g_mutex_lock(mutex_); + } + + GLibLocker(GstObject *object) + : mutex_(GST_OBJECT_GET_LOCK(object)) + { + g_mutex_lock(mutex_); + } + + ~GLibLocker() + { + g_mutex_unlock(mutex_); + } + +private: + GMutex *mutex_; +}; + +/** + * \class GLibRecLocker + * \brief A simple scoped mutex locker for GRecMutex + */ +class GLibRecLocker +{ +public: + GLibRecLocker(GRecMutex *mutex) + : mutex_(mutex) + { + g_rec_mutex_lock(mutex_); + } + + ~GLibRecLocker() + { + g_rec_mutex_unlock(mutex_); + } + +private: + GRecMutex *mutex_; +}; + + #endif /* __GST_LIBCAMERA_UTILS_H_ */