GStaticPrivate: implement via GPrivate
[platform/upstream/glib.git] / glib / deprecated / gthread-deprecated.c
index 5e45f7c..209d776 100644 (file)
@@ -23,6 +23,9 @@
 
 #include "config.h"
 
+/* we know we are deprecated here, no need for warnings */
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
+
 #include "gmessages.h"
 #include "gslice.h"
 #include "gmain.h"
 #include "gthreadprivate.h"
 #include "deprecated/gthread.h"
 
+#include "gutils.h"
+
 /* {{{1 Documentation */
 
 /**
+ * SECTION:threads-deprecated
+ * @title: Deprecated thread API
+ * @short_description: old thread APIs (for reference only)
+ * @see_also: #GThread
+ *
+ * These APIs are deprecated.  You should not use them in new code.
+ * This section remains only to assist with understanding code that was
+ * written to use these APIs at some point in the past.
+ **/
+
+/**
  * GThreadPriority:
  * @G_THREAD_PRIORITY_LOW: a priority lower than normal
  * @G_THREAD_PRIORITY_NORMAL: the default priority
  * to initialize the thread system.
  */
 
+/**
+ * G_THREADS_IMPL_POSIX:
+ *
+ * This macro is defined if POSIX style threads are used.
+ *
+ * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
+ *                 Use G_OS_WIN32 to detect Windows.
+ */
+
+/**
+ * G_THREADS_IMPL_WIN32:
+ *
+ * This macro is defined if Windows style threads are used.
+ *
+ * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
+ */
+
+
 /* {{{1 Exported Variables */
 
-gboolean g_thread_use_default_impl = TRUE;
+/* Set this FALSE to have previously-compiled GStaticMutex code use the
+ * slow path (ie: call into us) to avoid compatibility problems.
+ */
+gboolean g_thread_use_default_impl = FALSE;
 
 GThreadFunctions g_thread_functions_for_glib_use =
 {
@@ -109,6 +146,55 @@ gettime (void)
 
 guint64 (*g_thread_gettime) (void) = gettime;
 
+/* Initialisation {{{1 ---------------------------------------------------- */
+gboolean         g_threads_got_initialized = TRUE;
+GSystemThread    zero_thread; /* This is initialized to all zero */
+
+
+/**
+ * g_thread_init:
+ * @vtable: a function table of type #GThreadFunctions, that provides
+ *     the entry points to the thread system to be used. Since 2.32,
+ *     this parameter is ignored and should always be %NULL
+ *
+ * If you use GLib from more than one thread, you must initialize the
+ * thread system by calling g_thread_init().
+ *
+ * Since version 2.24, calling g_thread_init() multiple times is allowed,
+ * but nothing happens except for the first call.
+ *
+ * Since version 2.32, GLib does not support custom thread implementations
+ * anymore and the @vtable parameter is ignored and you should pass %NULL.
+ *
+ * <note><para>g_thread_init() must not be called directly or indirectly
+ * in a callback from GLib. Also no mutexes may be currently locked while
+ * calling g_thread_init().</para></note>
+ *
+ * <note><para>To use g_thread_init() in your program, you have to link
+ * with the libraries that the command <command>pkg-config --libs
+ * gthread-2.0</command> outputs. This is not the case for all the
+ * other thread-related functions of GLib. Those can be used without
+ * having to link with the thread libraries.</para></note>
+ */
+
+/**
+ * g_thread_get_initialized:
+ *
+ * Indicates if g_thread_init() has been called.
+ *
+ * Returns: %TRUE if threads have been initialized.
+ *
+ * Since: 2.20
+ */
+gboolean
+g_thread_get_initialized (void)
+{
+  return g_thread_supported ();
+}
+
+/* We need this for ABI compatibility */
+void g_thread_init_glib (void) { }
+
 /* Internal variables {{{1 */
 
 static GRealThread *g_thread_all_threads = NULL;
@@ -404,24 +490,24 @@ g_static_mutex_init (GStaticMutex *mutex)
  * Deprecated: 2.32: Just use a #GMutex
  */
 GMutex *
-g_static_mutex_get_mutex_impl (GMutex** mutex)
+g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
 {
   GMutex *result;
 
   if (!g_thread_supported ())
     return NULL;
 
-  result = g_atomic_pointer_get (mutex);
+  result = g_atomic_pointer_get (&mutex->mutex);
 
   if (!result)
     {
       g_mutex_lock (&g_once_mutex);
 
-      result = *mutex;
+      result = mutex->mutex;
       if (!result)
         {
           result = g_mutex_new ();
-          g_atomic_pointer_set (mutex, result);
+          g_atomic_pointer_set (&mutex->mutex, result);
         }
 
       g_mutex_unlock (&g_once_mutex);
@@ -1135,6 +1221,24 @@ struct _GStaticPrivateNode
   GStaticPrivate *owner;
 };
 
+void
+g_static_private_cleanup (gpointer data)
+{
+  GArray *array = data;
+  guint i;
+
+  for (i = 0; i < array->len; i++ )
+    {
+      GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
+      if (node->destroy)
+        node->destroy (node->data);
+    }
+
+  g_array_free (array, TRUE);
+}
+
+GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
+
 /**
  * GStaticPrivate:
  *
@@ -1206,10 +1310,10 @@ g_static_private_init (GStaticPrivate *private_key)
 gpointer
 g_static_private_get (GStaticPrivate *private_key)
 {
-  GRealThread *self = (GRealThread*) g_thread_self ();
   GArray *array;
   gpointer ret = NULL;
-  array = self->private_data;
+
+  array = g_private_get (&static_private_private);
 
   if (array && private_key->index != 0 && private_key->index <= array->len)
     {
@@ -1261,7 +1365,6 @@ g_static_private_set (GStaticPrivate *private_key,
                       gpointer        data,
                       GDestroyNotify  notify)
 {
-  GRealThread *self = (GRealThread*) g_thread_self ();
   GArray *array;
   static guint next_index = 0;
   GStaticPrivateNode *node;
@@ -1285,11 +1388,11 @@ g_static_private_set (GStaticPrivate *private_key,
       G_UNLOCK (g_thread);
     }
 
-  array = self->private_data;
+  array = g_private_get (&static_private_private);
   if (!array)
     {
       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
-      self->private_data = array;
+      g_private_set (&static_private_private, array);
     }
   if (private_key->index > array->len)
     g_array_set_size (array, private_key->index);
@@ -1335,26 +1438,88 @@ g_static_private_free (GStaticPrivate *private_key)
   G_UNLOCK (g_thread);
 }
 
+/* GMutex {{{1 ------------------------------------------------------ */
+
+/**
+ * g_mutex_new:
+ *
+ * Allocates and initializes a new #GMutex.
+ *
+ * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
+ *
+ * Deprecated:3.32:GMutex can now be statically allocated, or embedded
+ * in structures and initialised with g_mutex_init().
+ */
+GMutex *
+g_mutex_new (void)
+{
+  GMutex *mutex;
+
+  mutex = g_slice_new (GMutex);
+  g_mutex_init (mutex);
+
+  return mutex;
+}
+
+/**
+ * g_mutex_free:
+ * @mutex: a #GMutex
+ *
+ * Destroys a @mutex that has been created with g_mutex_new().
+ *
+ * Calling g_mutex_free() on a locked mutex may result
+ * in undefined behaviour.
+ *
+ * Deprecated:3.32:GMutex can now be statically allocated, or embedded
+ * in structures and initialised with g_mutex_init().
+ */
 void
-g_static_private_cleanup (GRealThread *thread)
+g_mutex_free (GMutex *mutex)
 {
-  GArray *array;
+  g_mutex_clear (mutex);
+  g_slice_free (GMutex, mutex);
+}
 
-  array = thread->private_data;
-  thread->private_data = NULL;
+/* GCond {{{1 ------------------------------------------------------ */
 
-  if (array)
-    {
-      guint i;
+/**
+ * g_cond_new:
+ *
+ * Allocates and initializes a new #GCond.
+ *
+ * Returns: a newly allocated #GCond. Free with g_cond_free()
+ *
+ * Deprecated:3.32:GCond can now be statically allocated, or embedded
+ * in structures and initialised with g_cond_init().
+ */
+GCond *
+g_cond_new (void)
+{
+  GCond *cond;
 
-      for (i = 0; i < array->len; i++ )
-        {
-          GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
-          if (node->destroy)
-            node->destroy (node->data);
-        }
-      g_array_free (array, TRUE);
-    }
+  cond = g_slice_new (GCond);
+  g_cond_init (cond);
+
+  return cond;
+}
+
+/**
+ * g_cond_free:
+ * @cond: a #GCond
+ *
+ * Destroys a #GCond that has been created with g_cond_new().
+ *
+ * Calling g_cond_free() for a #GCond on which threads are
+ * blocking leads to undefined behaviour.
+ *
+ * Deprecated:3.32:GCond can now be statically allocated, or embedded
+ * in structures and initialised with g_cond_init().
+ */
+void
+g_cond_free (GCond *cond)
+{
+  g_cond_clear (cond);
+  g_slice_free (GCond, cond);
 }
 
 /* {{{1 Epilogue */