[glib/wip/mutexes: 18/42] Continue GPrivate rework



commit 82c7d9a5c153a10f9a1a859ae54c22f708fe30b0
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Sep 18 01:10:07 2011 -0400

    Continue GPrivate rework
    
    We remove the macros while at the same time switching all libglib users
    from g_private_new() to g_private_init().  We deal with the strange
    expectations of the libglib code that g_private_* should work before the
    GPrivate has been initialised with a temporary shim.

 glib/gmessages.c      |   10 +++++-----
 glib/gslice.c         |    8 ++++----
 glib/gthread-posix.c  |   18 ++++++++++++++----
 glib/gthread-win32.c  |   17 ++++++++++++++---
 glib/gthread.c        |   12 ++++++------
 glib/gthread.h        |   14 +++-----------
 glib/gthreadprivate.h |    2 ++
 7 files changed, 48 insertions(+), 33 deletions(-)
---
diff --git a/glib/gmessages.c b/glib/gmessages.c
index 3129091..78de3dc 100644
--- a/glib/gmessages.c
+++ b/glib/gmessages.c
@@ -108,7 +108,7 @@ static GLogDomain    *g_log_domains = NULL;
 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
 static GPrintFunc     glib_print_func = NULL;
 static GPrintFunc     glib_printerr_func = NULL;
-static GPrivate	     *g_log_depth = NULL;
+static GPrivate       g_log_depth;
 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
 static GLogFunc       default_log_func = g_log_default_handler;
 static gpointer       default_log_data = NULL;
@@ -469,7 +469,7 @@ g_logv (const gchar   *log_domain,
       test_level = 1 << i;
       if (log_level & test_level)
 	{
-	  guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
+	  guint depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
 	  GLogDomain *domain;
 	  GLogFunc log_func;
 	  GLogLevelFlags domain_fatal_mask;
@@ -497,7 +497,7 @@ g_logv (const gchar   *log_domain,
 	  domain = NULL;
 	  g_mutex_unlock (&g_messages_lock);
 
-	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
+	  g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
 
 	  /* had to defer debug initialization until we can keep track of recursion */
 	  if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
@@ -574,7 +574,7 @@ g_logv (const gchar   *log_domain,
 	    }
 	  
 	  depth--;
-	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
+	  g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
 	}
     }
 }
@@ -1171,7 +1171,7 @@ g_printf_string_upper_bound (const gchar *format,
 void
 _g_messages_thread_init_nomessage (void)
 {
-  g_log_depth = g_private_new (NULL);
+  g_private_init (&g_log_depth, NULL);
   g_messages_prefixed_init ();
   _g_debug_init ();
 }
diff --git a/glib/gslice.c b/glib/gslice.c
index fad5060..f552d45 100644
--- a/glib/gslice.c
+++ b/glib/gslice.c
@@ -202,7 +202,7 @@ static int      smc_notify_free   (void   *pointer,
                                    size_t  size);
 
 /* --- variables --- */
-static GPrivate   *private_thread_memory = NULL;
+static GPrivate    private_thread_memory;
 static gsize       sys_page_size = 0;
 static Allocator   allocator[1] = { { 0, }, };
 static SliceConfig slice_config = {
@@ -398,7 +398,7 @@ _g_slice_thread_init_nomessage (void)
        * to a g_slice_alloc1() before g_thread_init().
        */
     }
-  private_thread_memory = g_private_new (private_thread_memory_cleanup);
+  g_private_init (&private_thread_memory, private_thread_memory_cleanup);
 }
 
 static inline void
@@ -434,7 +434,7 @@ g_mutex_lock_a (GMutex *mutex,
 static inline ThreadMemory*
 thread_memory_from_self (void)
 {
-  ThreadMemory *tmem = g_private_get (private_thread_memory);
+  ThreadMemory *tmem = g_private_get (&private_thread_memory);
   if (G_UNLIKELY (!tmem))
     {
       static ThreadMemory *single_thread_memory = NULL;   /* remember single-thread info for multi-threaded case */
@@ -463,7 +463,7 @@ thread_memory_from_self (void)
        * threaded case. but not *across* g_thread_init(), after multi-thread
        * initialization it returns NULL for previously set single-thread data.
        */
-      g_private_set (private_thread_memory, tmem);
+      g_private_set (&private_thread_memory, tmem);
       /* save single-thread thread memory structure, in case we need to
        * pick it up again after multi-thread initialization happened.
        */
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index e85a719..237b8cb 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -207,7 +207,7 @@ g_cond_timedwait (GCond  *cond,
 /* {{{1 GPrivate */
 
 GPrivate *
-(g_private_new) (GDestroyNotify notify)
+g_private_new (GDestroyNotify notify)
 {
   GPrivate *key;
 
@@ -224,21 +224,31 @@ g_private_init (GPrivate       *key,
                 GDestroyNotify  notify)
 {
   pthread_key_create (&key->key, notify);
+  key->ready = TRUE;
 }
 
 gpointer
-(g_private_get) (GPrivate *key)
+g_private_get (GPrivate *key)
 {
+  if (!key->ready)
+    return key->single_value;
+
   /* quote POSIX: No errors are returned from pthread_getspecific(). */
   return pthread_getspecific (key->key);
 }
 
 void
-(g_private_set) (GPrivate *key,
-                 gpointer  value)
+g_private_set (GPrivate *key,
+               gpointer  value)
 {
   gint status;
 
+  if (!key->ready)
+    {
+      key->single_value = value;
+      return;
+    }
+
   if G_UNLIKELY ((status = pthread_setspecific (key->key, value)) != 0)
     g_thread_abort (status, "pthread_setspecific");
 }
diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c
index 81fd7de..7910589 100644
--- a/glib/gthread-win32.c
+++ b/glib/gthread-win32.c
@@ -246,7 +246,7 @@ struct _GPrivateDestructor
 static GPrivateDestructor * volatile g_private_destructors;
 
 GPrivate *
-(g_private_new) (GDestroyNotify notify)
+g_private_new (GDestroyNotify notify)
 {
   GPrivate *key;
 
@@ -275,18 +275,29 @@ g_private_init (GPrivate       *key,
   do
     destructor->next = g_private_destructors;
   while (InterlockedCompareExchangePointer (&g_private_destructors, destructor->next, destructor) != destructor->next);
+
+  key->ready = TRUE;
 }
 
 gpointer
-(g_private_get) (GPrivate *key)
+g_private_get (GPrivate *key)
 {
+  if (!key->ready)
+    return key->single_value;
+
   return TlsGetValue (key->index);
 }
 
 void
-(g_private_set) (GPrivate *key,
+g_private_set (GPrivate *key,
                gpointer  value)
 {
+  if (!key->ready)
+    {
+      key->single_value = value;
+      return;
+    }
+
   TlsSetValue (key->index, value);
 }
 
diff --git a/glib/gthread.c b/glib/gthread.c
index 5c7522b..feb9e87 100644
--- a/glib/gthread.c
+++ b/glib/gthread.c
@@ -875,7 +875,7 @@ static GThreadFunctions g_thread_functions_for_glib_use_old = {
 
 static GMutex    g_once_mutex = G_MUTEX_INIT;
 static GCond     g_once_cond = G_COND_INIT;
-static GPrivate *g_thread_specific_private = NULL;
+static GPrivate  g_thread_specific_private;
 static GRealThread *g_thread_all_threads = NULL;
 static GSList   *g_thread_free_indices = NULL;
 static GSList*   g_once_init_list = NULL;
@@ -944,8 +944,8 @@ g_thread_init_glib (void)
 
   /* setup the basic threading system */
   g_threads_got_initialized = TRUE;
-  g_thread_specific_private = g_private_new (g_thread_cleanup);
-  g_private_set (g_thread_specific_private, main_thread);
+  g_private_init (&g_thread_specific_private, g_thread_cleanup);
+  g_private_set (&g_thread_specific_private, main_thread);
   G_THREAD_UF (thread_self, (&main_thread->system_thread));
 
   /* complete memory system initialization, g_private_*() works now */
@@ -1923,7 +1923,7 @@ g_thread_create_proxy (gpointer data)
   g_assert (data);
 
   /* This has to happen before G_LOCK, as that might call g_thread_self */
-  g_private_set (g_thread_specific_private, data);
+  g_private_set (&g_thread_specific_private, data);
 
   /* the lock makes sure, that thread->system_thread is written,
      before thread->thread.func is called. See g_thread_create. */
@@ -2146,7 +2146,7 @@ g_thread_set_priority (GThread* thread,
 GThread*
 g_thread_self (void)
 {
-  GRealThread* thread = g_private_get (g_thread_specific_private);
+  GRealThread* thread = g_private_get (&g_thread_specific_private);
 
   if (!thread)
     {
@@ -2164,7 +2164,7 @@ g_thread_self (void)
       if (g_thread_supported ())
 	G_THREAD_UF (thread_self, (&thread->system_thread));
 
-      g_private_set (g_thread_specific_private, thread);
+      g_private_set (&g_thread_specific_private, thread);
 
       G_LOCK (g_thread);
       thread->next = g_thread_all_threads;
diff --git a/glib/gthread.h b/glib/gthread.h
index 77afc4e..e9d5ebe 100644
--- a/glib/gthread.h
+++ b/glib/gthread.h
@@ -187,14 +187,6 @@ GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
 #else
 #define g_thread_supported()    (g_threads_got_initialized)
 #endif
-#define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
-#define g_private_get(private_key) G_THREAD_CF (private_get, \
-                                                ((gpointer)private_key), \
-                                                (private_key))
-#define g_private_set(private_key, value) G_THREAD_CF (private_set, \
-                                                       (void) (private_key = \
-                                                        (GPrivate*) (value)), \
-                                                       (private_key, value))
 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
 
 #define g_thread_create(func, data, joinable, error)			\
@@ -408,9 +400,9 @@ void                    g_mutex_free                                    (GMutex
 GCond *                 g_cond_new                                      (void);
 void                    g_cond_free                                     (GCond          *cond);
 
-GPrivate *              (g_private_new)                                 (GDestroyNotify  notify);
-gpointer                (g_private_get)                                 (GPrivate       *key);
-void                    (g_private_set)                                 (GPrivate       *key,
+GPrivate *              g_private_new                                   (GDestroyNotify  notify);
+gpointer                g_private_get                                   (GPrivate       *key);
+void                    g_private_set                                   (GPrivate       *key,
                                                                          gpointer        value);
 
 G_END_DECLS
diff --git a/glib/gthreadprivate.h b/glib/gthreadprivate.h
index 4b9c2f4..05a91cf 100644
--- a/glib/gthreadprivate.h
+++ b/glib/gthreadprivate.h
@@ -57,6 +57,8 @@ G_GNUC_INTERNAL void _g_thread_impl_init  (void);
 
 struct _GPrivate
 {
+  gpointer single_value;
+  gboolean ready;
 #ifdef G_OS_WIN32
   gint index;
 #else



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]