gstpad, gstobject: Add GMutexLocker helper

Add GST_OBJECT_AUTO_LOCK() and GST_PAD_STREAM_AUTO_LOCK() to simplify
g_autoptr(GMutexLocker) usage.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4300>
This commit is contained in:
Xavier Claessens 2023-04-05 08:48:36 -04:00 committed by GStreamer Marge Bot
parent f337d37d6d
commit 6a3b2001a3
3 changed files with 62 additions and 0 deletions

View file

@ -88,6 +88,34 @@ typedef enum
* It blocks until the lock can be obtained. * It blocks until the lock can be obtained.
*/ */
#define GST_OBJECT_LOCK(obj) g_mutex_lock(GST_OBJECT_GET_LOCK(obj)) #define GST_OBJECT_LOCK(obj) g_mutex_lock(GST_OBJECT_GET_LOCK(obj))
/**
* GST_OBJECT_AUTO_LOCK:
* @obj: a #GstObject to lock
* @var: a variable name to be declared
*
* Declare a #GMutexLocker variable with g_autoptr() and lock the object. The
* mutex will be unlocked automatically when leaving the scope.
*
* ``` c
* {
* GST_OBJECT_AUTO_LOCK (obj, locker);
*
* obj->stuff_with_lock();
* if (cond) {
* // No need to unlock
* return;
* }
*
* // Unlock before end of scope
* g_clear_pointer (&locker, g_mutex_locker_free);
* obj->stuff_without_lock();
* }
* ```
* Since: 1.24.0
*/
#define GST_OBJECT_AUTO_LOCK(obj, var) g_autoptr(GMutexLocker) G_GNUC_UNUSED var = g_mutex_locker_new(GST_OBJECT_GET_LOCK(obj))
/** /**
* GST_OBJECT_TRYLOCK: * GST_OBJECT_TRYLOCK:
* @obj: a #GstObject. * @obj: a #GstObject.

View file

@ -1249,6 +1249,34 @@ struct _GstPadClass {
* when buffers or serialized downstream events are pushed on a pad. * when buffers or serialized downstream events are pushed on a pad.
*/ */
#define GST_PAD_STREAM_LOCK(pad) g_rec_mutex_lock(GST_PAD_GET_STREAM_LOCK(pad)) #define GST_PAD_STREAM_LOCK(pad) g_rec_mutex_lock(GST_PAD_GET_STREAM_LOCK(pad))
/**
* GST_PAD_STREAM_AUTO_LOCK
* @pad: a #GstPad
* @var: a variable name to be declared
*
* Declare a #GRecMutexLocker variable with g_autoptr() and lock the pad. The
* recursive mutex will be unlocked automatically when leaving the scope.
*
* ``` c
* {
* GST_PAD_STREAM_AUTO_LOCK (pad, locker);
*
* gst_pad_push_event(pad, event1);
* if (cond) {
* // No need to unlock
* return;
* }
*
* // Unlock before end of scope
* g_clear_pointer (&locker, g_rec_mutex_locker_free);
* gst_pad_push_event(pad, event2);
* }
* ```
* Since: 1.24.0
*/
#define GST_PAD_STREAM_AUTO_LOCK(pad, var) g_autoptr(GRecMutexLocker) G_GNUC_UNUSED var = g_rec_mutex_locker_new(GST_PAD_GET_STREAM_LOCK(pad))
/** /**
* GST_PAD_STREAM_TRYLOCK: * GST_PAD_STREAM_TRYLOCK:
* @pad: a #GstPad * @pad: a #GstPad

View file

@ -284,12 +284,18 @@ GST_START_TEST (test_fake_object_name_threaded_right)
/* start looping and set/get name repeatedly */ /* start looping and set/get name repeatedly */
for (i = 0; i < 1000; ++i) { for (i = 0; i < 1000; ++i) {
#ifndef g_autoptr
GST_OBJECT_LOCK (object); GST_OBJECT_LOCK (object);
#else
GST_OBJECT_AUTO_LOCK (object, locker);
#endif
g_free (GST_OBJECT_NAME (object)); g_free (GST_OBJECT_NAME (object));
GST_OBJECT_NAME (object) = g_strdup ("main"); GST_OBJECT_NAME (object) = g_strdup ("main");
THREAD_SWITCH (); THREAD_SWITCH ();
name = g_strdup (GST_OBJECT_NAME (object)); name = g_strdup (GST_OBJECT_NAME (object));
#ifndef g_autoptr
GST_OBJECT_UNLOCK (object); GST_OBJECT_UNLOCK (object);
#endif
fail_unless (strcmp (name, "main") == 0, fail_unless (strcmp (name, "main") == 0,
"Name got changed while lock held during run %d", i); "Name got changed while lock held during run %d", i);