gatomic: use pthread mutexes instead of GMutex

GStaticMutex makes use of atomic operations to initialise itself.  We
were using GStaticMutex from the emulated case of gatomic.  Use pthreads
directly to avoid unbounded recursion in this case.
This commit is contained in:
Ryan Lortie 2011-09-16 17:50:13 -04:00
parent 48494dd552
commit e2ae75ad4b

View File

@ -82,6 +82,17 @@
* perform the operations normally and then release the lock. * perform the operations normally and then release the lock.
**/ **/
/* NOTE CAREFULLY:
*
* This file is the lowest-level part of GLib.
*
* Other lowlevel parts of GLib (threads, slice allocator, g_malloc,
* messages, etc) call into these functions and macros to get work done.
*
* As such, these functions can not call back into any part of GLib
* without risking recursion.
*/
#ifdef G_ATOMIC_OP_USE_GCC_BUILTINS #ifdef G_ATOMIC_OP_USE_GCC_BUILTINS
#ifndef __GNUC__ #ifndef __GNUC__
@ -604,18 +615,24 @@ gsize
#else #else
#include "gthread.h" /* We are not permitted to call into any GLib functions from here, so we
* can not use GMutex.
*
* Fortunately, we already take care of the Windows case above, and all
* non-Windows platforms on which glib runs have pthreads. Use those.
*/
#include <pthread.h>
static GStaticMutex g_atomic_lock; static pthread_mutex_t g_atomic_lock = PTHREAD_MUTEX_INITIALIZER;
gint gint
(g_atomic_int_get) (volatile gint *atomic) (g_atomic_int_get) (volatile gint *atomic)
{ {
gint value; gint value;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
value = *atomic; value = *atomic;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return value; return value;
} }
@ -624,17 +641,17 @@ void
(g_atomic_int_set) (volatile gint *atomic, (g_atomic_int_set) (volatile gint *atomic,
gint value) gint value)
{ {
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
*atomic = value; *atomic = value;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
} }
void void
(g_atomic_int_inc) (volatile gint *atomic) (g_atomic_int_inc) (volatile gint *atomic)
{ {
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
(*atomic)++; (*atomic)++;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
} }
gboolean gboolean
@ -642,9 +659,9 @@ gboolean
{ {
gboolean is_zero; gboolean is_zero;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
is_zero = --(*atomic) == 0; is_zero = --(*atomic) == 0;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return is_zero; return is_zero;
} }
@ -656,12 +673,12 @@ gboolean
{ {
gboolean success; gboolean success;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
if ((success = (*atomic == oldval))) if ((success = (*atomic == oldval)))
*atomic = newval; *atomic = newval;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return success; return success;
} }
@ -672,10 +689,10 @@ gint
{ {
gint oldval; gint oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *atomic; oldval = *atomic;
*atomic = oldval + val; *atomic = oldval + val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -686,10 +703,10 @@ guint
{ {
guint oldval; guint oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *atomic; oldval = *atomic;
*atomic = oldval & val; *atomic = oldval & val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -700,10 +717,10 @@ guint
{ {
guint oldval; guint oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *atomic; oldval = *atomic;
*atomic = oldval | val; *atomic = oldval | val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -714,10 +731,10 @@ guint
{ {
guint oldval; guint oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *atomic; oldval = *atomic;
*atomic = oldval ^ val; *atomic = oldval ^ val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -729,9 +746,9 @@ gpointer
volatile gpointer *ptr = atomic; volatile gpointer *ptr = atomic;
gpointer value; gpointer value;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
value = *ptr; value = *ptr;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return value; return value;
} }
@ -742,9 +759,9 @@ void
{ {
volatile gpointer *ptr = atomic; volatile gpointer *ptr = atomic;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
*ptr = newval; *ptr = newval;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
} }
gboolean gboolean
@ -755,12 +772,12 @@ gboolean
volatile gpointer *ptr = atomic; volatile gpointer *ptr = atomic;
gboolean success; gboolean success;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
if ((success = (*ptr == oldval))) if ((success = (*ptr == oldval)))
*ptr = newval; *ptr = newval;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return success; return success;
} }
@ -772,10 +789,10 @@ gssize
volatile gssize *ptr = atomic; volatile gssize *ptr = atomic;
gssize oldval; gssize oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *ptr; oldval = *ptr;
*ptr = oldval + val; *ptr = oldval + val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -787,10 +804,10 @@ gsize
volatile gsize *ptr = atomic; volatile gsize *ptr = atomic;
gsize oldval; gsize oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *ptr; oldval = *ptr;
*ptr = oldval & val; *ptr = oldval & val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -802,10 +819,10 @@ gsize
volatile gsize *ptr = atomic; volatile gsize *ptr = atomic;
gsize oldval; gsize oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *ptr; oldval = *ptr;
*ptr = oldval | val; *ptr = oldval | val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }
@ -817,10 +834,10 @@ gsize
volatile gsize *ptr = atomic; volatile gsize *ptr = atomic;
gsize oldval; gsize oldval;
g_static_mutex_lock (&g_atomic_lock); pthread_mutex_lock (&g_atomic_lock);
oldval = *ptr; oldval = *ptr;
*ptr = oldval ^ val; *ptr = oldval ^ val;
g_static_mutex_unlock (&g_atomic_lock); pthread_mutex_unlock (&g_atomic_lock);
return oldval; return oldval;
} }