omxrecmutex: Fix a small race condition when unlocking a non-recursive lock

This commit is contained in:
Sebastian Dröge 2012-12-20 14:45:18 +01:00
parent 1d01c0ebf0
commit 1c7fcf832e
2 changed files with 16 additions and 8 deletions

View file

@ -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);

View file

@ -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);