From c5634df6d3ffe2da013a246880b183140b8f1260 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Sun, 2 Oct 2011 20:59:15 -0400 Subject: [PATCH] locks: change the ABI just a bit Add a little bit more room in the ABI for our synchronisation primatives since we're going to need it when we add native implementations on Linux. Also: rename the pointer field and add /*< private >*/ annotations. --- glib/deprecated/gthread.h | 2 +- glib/gthread-posix.c | 42 +++++++++++++++++++++--------------------- glib/gthread-win32.c | 15 +++++++-------- glib/gthread.h | 20 ++++++++++++++------ 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/glib/deprecated/gthread.h b/glib/deprecated/gthread.h index 8e013a8..00e92df 100644 --- a/glib/deprecated/gthread.h +++ b/glib/deprecated/gthread.h @@ -123,7 +123,7 @@ typedef GMutex * GStaticMutex; #define g_static_mutex_get_mutex g_static_mutex_get_mutex_impl #else /* G_OS_WIN32 */ typedef struct { - struct _GMutex *unused; + GMutex *unused; GMutex mutex; } GStaticMutex; #define G_STATIC_MUTEX_INIT { NULL, { NULL } } diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c index dfb2b91..c47fc0d 100644 --- a/glib/gthread-posix.c +++ b/glib/gthread-posix.c @@ -116,14 +116,14 @@ g_mutex_impl_free (pthread_mutex_t *mutex) static pthread_mutex_t * g_mutex_get_impl (GMutex *mutex) { - pthread_mutex_t *impl = mutex->impl; + pthread_mutex_t *impl = mutex->p; if G_UNLIKELY (impl == NULL) { impl = g_mutex_impl_new (); - if (!g_atomic_pointer_compare_and_exchange (&mutex->impl, NULL, impl)) + if (!g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl)) g_mutex_impl_free (impl); - impl = mutex->impl; + impl = mutex->p; } return impl; @@ -164,7 +164,7 @@ g_mutex_get_impl (GMutex *mutex) void g_mutex_init (GMutex *mutex) { - mutex->impl = g_mutex_impl_new (); + mutex->p = g_mutex_impl_new (); } /** @@ -184,7 +184,7 @@ g_mutex_init (GMutex *mutex) void g_mutex_clear (GMutex *mutex) { - g_mutex_impl_free (mutex->impl); + g_mutex_impl_free (mutex->p); } /** @@ -293,14 +293,14 @@ 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->impl; + pthread_mutex_t *impl = rec_mutex->p; if G_UNLIKELY (impl == NULL) { impl = g_rec_mutex_impl_new (); - if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->impl, NULL, impl)) + if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl)) g_rec_mutex_impl_free (impl); - impl = rec_mutex->impl; + impl = rec_mutex->p; } return impl; @@ -342,7 +342,7 @@ g_rec_mutex_get_impl (GRecMutex *rec_mutex) void g_rec_mutex_init (GRecMutex *rec_mutex) { - rec_mutex->impl = g_rec_mutex_impl_new (); + rec_mutex->p = g_rec_mutex_impl_new (); } /** @@ -363,7 +363,7 @@ g_rec_mutex_init (GRecMutex *rec_mutex) void g_rec_mutex_clear (GRecMutex *rec_mutex) { - g_rec_mutex_impl_free (rec_mutex->impl); + g_rec_mutex_impl_free (rec_mutex->p); } /** @@ -401,7 +401,7 @@ g_rec_mutex_lock (GRecMutex *mutex) void g_rec_mutex_unlock (GRecMutex *rec_mutex) { - pthread_mutex_unlock (rec_mutex->impl); + pthread_mutex_unlock (rec_mutex->p); } /** @@ -453,14 +453,14 @@ 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->impl; + pthread_rwlock_t *impl = lock->p; if G_UNLIKELY (impl == NULL) { impl = g_rw_lock_impl_new (); - if (!g_atomic_pointer_compare_and_exchange (&lock->impl, NULL, impl)) + if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl)) g_rw_lock_impl_free (impl); - impl = lock->impl; + impl = lock->p; } return impl; @@ -500,7 +500,7 @@ g_rw_lock_get_impl (GRWLock *lock) void g_rw_lock_init (GRWLock *rw_lock) { - rw_lock->impl = g_rw_lock_impl_new (); + rw_lock->p = g_rw_lock_impl_new (); } /** @@ -517,7 +517,7 @@ g_rw_lock_init (GRWLock *rw_lock) void g_rw_lock_clear (GRWLock *rw_lock) { - g_rw_lock_impl_free (rw_lock->impl); + g_rw_lock_impl_free (rw_lock->p); } /** @@ -659,14 +659,14 @@ g_cond_impl_free (pthread_cond_t *cond) static pthread_cond_t * g_cond_get_impl (GCond *cond) { - pthread_cond_t *impl = cond->impl; + pthread_cond_t *impl = cond->p; if G_UNLIKELY (impl == NULL) { impl = g_cond_impl_new (); - if (!g_atomic_pointer_compare_and_exchange (&cond->impl, NULL, impl)) + if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl)) g_cond_impl_free (impl); - impl = cond->impl; + impl = cond->p; } return impl; @@ -694,7 +694,7 @@ g_cond_get_impl (GCond *cond) void g_cond_init (GCond *cond) { - cond->impl = g_cond_impl_new (); + cond->p = g_cond_impl_new (); } /** @@ -714,7 +714,7 @@ g_cond_init (GCond *cond) void g_cond_clear (GCond *cond) { - g_cond_impl_free (cond->impl); + g_cond_impl_free (cond->p); } /** diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c index 5d6d867..efd0a70 100644 --- a/glib/gthread-win32.c +++ b/glib/gthread-win32.c @@ -173,14 +173,14 @@ g_rec_mutex_impl_free (CRITICAL_SECTION *cs) static CRITICAL_SECTION * g_rec_mutex_get_impl (GRecMutex *mutex) { - CRITICAL_SECTION *impl = mutex->impl; + CRITICAL_SECTION *impl = mutex->p; - if G_UNLIKELY (mutex->impl == NULL) + if G_UNLIKELY (mutex->p == NULL) { impl = g_rec_mutex_impl_new (); - if (InterlockedCompareExchangePointer (&mutex->impl, impl, NULL) != NULL) + if (InterlockedCompareExchangePointer (&mutex->p, impl, NULL) != NULL) g_rec_mutex_impl_free (impl); - impl = mutex->impl; + impl = mutex->p; } return impl; @@ -189,14 +189,13 @@ g_rec_mutex_get_impl (GRecMutex *mutex) void g_rec_mutex_init (GRecMutex *mutex) { - mutex->impl = g_rec_mutex_impl_new (); + mutex->p = g_rec_mutex_impl_new (); } void g_rec_mutex_clear (GRecMutex *mutex) { - if (mutex->impl) - g_rec_mutex_impl_free (mutex->impl); + g_rec_mutex_impl_free (mutex->p); } void @@ -208,7 +207,7 @@ g_rec_mutex_lock (GRecMutex *mutex) void g_rec_mutex_unlock (GRecMutex *mutex) { - LeaveCriticalSection (mutex->impl); + LeaveCriticalSection (mutex->p); } gboolean diff --git a/glib/gthread.h b/glib/gthread.h index 495eaa0..8844cdb 100644 --- a/glib/gthread.h +++ b/glib/gthread.h @@ -52,31 +52,39 @@ typedef gpointer (*GThreadFunc) (gpointer data); typedef struct _GThread GThread; -typedef struct _GMutex GMutex; +typedef union _GMutex GMutex; typedef struct _GRecMutex GRecMutex; typedef struct _GRWLock GRWLock; typedef struct _GCond GCond; typedef struct _GPrivate GPrivate; typedef struct _GStaticPrivate GStaticPrivate; -struct _GMutex +union _GMutex { - gpointer impl; + /*< private >*/ + gpointer p; + guint i[2]; }; struct _GRWLock { - gpointer impl; + /*< private >*/ + gpointer p; + guint i[2]; }; struct _GCond { - gpointer impl; + /*< private >*/ + gpointer p; + guint i[2]; }; struct _GRecMutex { - gpointer impl; + /*< private >*/ + gpointer p; + guint i[2]; }; #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } } -- 2.7.4