From 1c7fcf832e03de8239a12897fb8b5767113e1b51 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 20 Dec 2012 14:45:18 +0100 Subject: [PATCH] omxrecmutex: Fix a small race condition when unlocking a non-recursive lock --- omx/gstomxrecmutex.c | 22 +++++++++++++++------- omx/gstomxrecmutex.h | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/omx/gstomxrecmutex.c b/omx/gstomxrecmutex.c index 35731fb..bc4c9cb 100644 --- a/omx/gstomxrecmutex.c +++ b/omx/gstomxrecmutex.c @@ -29,7 +29,7 @@ gst_omx_rec_mutex_init (GstOMXRecMutex * mutex) { g_mutex_init (&mutex->lock); g_mutex_init (&mutex->recursion_lock); - mutex->recursion_allowed = FALSE; + g_atomic_int_set (&mutex->recursion_allowed, FALSE); } void @@ -55,9 +55,13 @@ gst_omx_rec_mutex_unlock (GstOMXRecMutex * mutex) void gst_omx_rec_mutex_begin_recursion (GstOMXRecMutex * mutex) { + gboolean exchanged; + g_mutex_lock (&mutex->recursion_lock); - g_assert (mutex->recursion_allowed == FALSE); - mutex->recursion_allowed = TRUE; + exchanged = + g_atomic_int_compare_and_exchange (&mutex->recursion_allowed, FALSE, + TRUE); + g_assert (exchanged); g_mutex_unlock (&mutex->recursion_lock); } @@ -65,9 +69,13 @@ gst_omx_rec_mutex_begin_recursion (GstOMXRecMutex * mutex) void gst_omx_rec_mutex_end_recursion (GstOMXRecMutex * mutex) { + gboolean exchanged; + g_mutex_lock (&mutex->recursion_lock); - g_assert (mutex->recursion_allowed == TRUE); - mutex->recursion_allowed = FALSE; + exchanged = + g_atomic_int_compare_and_exchange (&mutex->recursion_allowed, TRUE, + FALSE); + g_assert (exchanged); g_mutex_unlock (&mutex->recursion_lock); } @@ -75,7 +83,7 @@ void gst_omx_rec_mutex_recursive_lock (GstOMXRecMutex * mutex) { g_mutex_lock (&mutex->recursion_lock); - if (!mutex->recursion_allowed) { + if (!g_atomic_int_get (&mutex->recursion_allowed)) { /* no recursion allowed, lock the proper mutex */ g_mutex_unlock (&mutex->recursion_lock); g_mutex_lock (&mutex->lock); @@ -89,7 +97,7 @@ gst_omx_rec_mutex_recursive_unlock (GstOMXRecMutex * mutex) * we hold at least one of the two locks and * either lock protects it from being changed. */ - if (mutex->recursion_allowed) { + if (g_atomic_int_get (&mutex->recursion_allowed)) { g_mutex_unlock (&mutex->recursion_lock); } else { g_mutex_unlock (&mutex->lock); diff --git a/omx/gstomxrecmutex.h b/omx/gstomxrecmutex.h index 79c49af..4a5a0ec 100644 --- a/omx/gstomxrecmutex.h +++ b/omx/gstomxrecmutex.h @@ -91,7 +91,7 @@ struct _GstOMXRecMutex { * the recursion_lock instead of the master lock. * This variable is protected by both locks. */ - volatile gboolean recursion_allowed; + volatile gint recursion_allowed; }; void gst_omx_rec_mutex_init (GstOMXRecMutex * mutex); -- 2.7.4