gthread-posix: always use atomic pointer ops
authorRyan Lortie <desrt@desrt.ca>
Mon, 29 Oct 2012 09:13:40 +0000 (10:13 +0100)
committerRyan Lortie <desrt@desrt.ca>
Mon, 29 Oct 2012 15:18:10 +0000 (16:18 +0100)
On platforms where dependent loads can be reordered (alpha) and we have
exotic implementation of pthread_mutex_lock() it could be possible that
our implementation of g_mutex_lock() is unsafe.

Always use atomic operations to avoid this possibility.

https://bugzilla.gnome.org/show_bug.cgi?id=686191

glib/gthread-posix.c

index cb06b14..f42e32c 100644 (file)
@@ -116,7 +116,7 @@ g_mutex_impl_free (pthread_mutex_t *mutex)
 static pthread_mutex_t *
 g_mutex_get_impl (GMutex *mutex)
 {
-  pthread_mutex_t *impl = mutex->p;
+  pthread_mutex_t *impl = g_atomic_pointer_get (&mutex->p);
 
   if G_UNLIKELY (impl == NULL)
     {
@@ -285,7 +285,7 @@ g_rec_mutex_impl_free (pthread_mutex_t *mutex)
 static pthread_mutex_t *
 g_rec_mutex_get_impl (GRecMutex *rec_mutex)
 {
-  pthread_mutex_t *impl = rec_mutex->p;
+  pthread_mutex_t *impl = g_atomic_pointer_get (&rec_mutex->p);
 
   if G_UNLIKELY (impl == NULL)
     {
@@ -445,7 +445,7 @@ g_rw_lock_impl_free (pthread_rwlock_t *rwlock)
 static pthread_rwlock_t *
 g_rw_lock_get_impl (GRWLock *lock)
 {
-  pthread_rwlock_t *impl = lock->p;
+  pthread_rwlock_t *impl = g_atomic_pointer_get (&lock->p);
 
   if G_UNLIKELY (impl == NULL)
     {
@@ -662,7 +662,7 @@ g_cond_impl_free (pthread_cond_t *cond)
 static pthread_cond_t *
 g_cond_get_impl (GCond *cond)
 {
-  pthread_cond_t *impl = cond->p;
+  pthread_cond_t *impl = g_atomic_pointer_get (&cond->p);
 
   if G_UNLIKELY (impl == NULL)
     {
@@ -969,7 +969,7 @@ g_private_impl_free (pthread_key_t *key)
 static pthread_key_t *
 g_private_get_impl (GPrivate *key)
 {
-  pthread_key_t *impl = key->p;
+  pthread_key_t *impl = g_atomic_pointer_get (&key->p);
 
   if G_UNLIKELY (impl == NULL)
     {