drop g_thread_new_full()
[platform/upstream/glib.git] / glib / deprecated / gthread-deprecated.c
index 89e004f..c6aa3ef 100644 (file)
@@ -148,8 +148,6 @@ 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:
@@ -201,6 +199,7 @@ static GSList      *g_thread_all_threads = NULL;
 static GSList      *g_thread_free_indices = NULL;
 
 /* Protects g_thread_all_threads and g_thread_free_indices */
+G_LOCK_DEFINE_STATIC (g_static_mutex);
 G_LOCK_DEFINE_STATIC (g_thread);
 
 /* Misc. GThread functions {{{1 */
@@ -293,6 +292,17 @@ g_enumerable_thread_add (GRealThread *thread)
 
   g_private_set (&enumerable_thread_private, thread);
 }
+
+static gpointer
+g_deprecated_thread_proxy (gpointer data)
+{
+  GRealThread *real = data;
+
+  g_enumerable_thread_add (real);
+
+  return g_thread_proxy (data);
+}
+
 /**
  * g_thread_create:
  * @func: a function to execute in the new thread
@@ -302,16 +312,17 @@ g_enumerable_thread_add (GRealThread *thread)
  *
  * This function creates a new thread.
  *
- * If @joinable is %TRUE, you can wait for this threads termination
- * calling g_thread_join(). Otherwise the thread will just disappear
- * when it terminates.
- *
  * The new thread executes the function @func with the argument @data.
  * If the thread was created successfully, it is returned.
  *
  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
  * The error is set, if and only if the function returns %NULL.
  *
+ * This function returns a reference to the created thread only if
+ * @joinable is %TRUE.  In that case, you must free this reference by
+ * calling g_thread_unref() or g_thread_join().  If @joinable is %FALSE
+ * then you should probably not touch the return value.
+ *
  * Returns: the new #GThread on success
  *
  * Deprecated:2.32: Use g_thread_new() instead
@@ -322,7 +333,7 @@ g_thread_create (GThreadFunc   func,
                  gboolean      joinable,
                  GError      **error)
 {
-  return g_thread_new_internal (NULL, func, data, joinable, 0, g_enumerable_thread_add, error);
+  return g_thread_create_full (func, data, 0, joinable, 0, 0, error);
 }
 
 /**
@@ -339,7 +350,7 @@ g_thread_create (GThreadFunc   func,
  * This function creates a new thread.
  *
  * Deprecated:2.32: The @bound and @priority arguments are now ignored.
- * Use g_thread_new() or g_thread_new_full() instead.
+ * Use g_thread_new().
  */
 GThread *
 g_thread_create_full (GThreadFunc       func,
@@ -350,10 +361,19 @@ g_thread_create_full (GThreadFunc       func,
                       GThreadPriority   priority,
                       GError          **error)
 {
-  return g_thread_new_internal (NULL, func, data, joinable, stack_size, g_enumerable_thread_add, error);
-}
+  GThread *thread;
 
+  thread = g_thread_new_internal (NULL, g_deprecated_thread_proxy,
+                                  func, data, stack_size, error);
 
+  if (!joinable)
+    {
+      thread->joinable = FALSE;
+      g_thread_unref (thread);
+    }
+
+  return thread;
+}
 
 /* GOnce {{{1 ------------------------------------------------------------- */
 gboolean
@@ -495,7 +515,7 @@ g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
 
   if (!result)
     {
-      g_mutex_lock (&g_once_mutex);
+      G_LOCK (g_static_mutex);
 
       result = mutex->mutex;
       if (!result)
@@ -504,7 +524,7 @@ g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
           g_atomic_pointer_set (&mutex->mutex, result);
         }
 
-      g_mutex_unlock (&g_once_mutex);
+      G_UNLOCK (g_static_mutex);
     }
 
   return result;
@@ -639,6 +659,34 @@ g_static_rec_mutex_init (GStaticRecMutex *mutex)
   *mutex = init_mutex;
 }
 
+static GRecMutex *
+g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
+{
+  GRecMutex *result;
+
+  if (!g_thread_supported ())
+    return NULL;
+
+  result = g_atomic_pointer_get (&mutex->mutex.mutex);
+
+  if (!result)
+    {
+      G_LOCK (g_static_mutex);
+
+      result = (GRecMutex *) mutex->mutex.mutex;
+      if (!result)
+        {
+          result = g_slice_new (GRecMutex);
+          g_rec_mutex_init (result);
+          g_atomic_pointer_set (&mutex->mutex.mutex, result);
+        }
+
+      G_UNLOCK (g_static_mutex);
+    }
+
+  return result;
+}
+
 /**
  * g_static_rec_mutex_lock:
  * @mutex: a #GStaticRecMutex to lock.
@@ -653,23 +701,10 @@ g_static_rec_mutex_init (GStaticRecMutex *mutex)
 void
 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
 {
-  GSystemThread self;
-
-  g_return_if_fail (mutex);
-
-  if (!g_thread_supported ())
-    return;
-
-  g_system_thread_self (&self);
-
-  if (g_system_thread_equal (&self, &mutex->owner))
-    {
-      mutex->depth++;
-      return;
-    }
-  g_static_mutex_lock (&mutex->mutex);
-  g_system_thread_assign (mutex->owner, self);
-  mutex->depth = 1;
+  GRecMutex *rm;
+  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
+  g_rec_mutex_lock (rm);
+  mutex->depth++;
 }
 
 /**
@@ -688,27 +723,16 @@ g_static_rec_mutex_lock (GStaticRecMutex* mutex)
 gboolean
 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
 {
-  GSystemThread self;
-
-  g_return_val_if_fail (mutex, FALSE);
-
-  if (!g_thread_supported ())
-    return TRUE;
-
-  g_system_thread_self (&self);
+  GRecMutex *rm;
+  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
 
-  if (g_system_thread_equal (&self, &mutex->owner))
+  if (g_rec_mutex_trylock (rm))
     {
       mutex->depth++;
       return TRUE;
     }
-
-  if (!g_static_mutex_trylock (&mutex->mutex))
+  else
     return FALSE;
-
-  g_system_thread_assign (mutex->owner, self);
-  mutex->depth = 1;
-  return TRUE;
 }
 
 /**
@@ -726,18 +750,10 @@ g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
 void
 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
 {
-  g_return_if_fail (mutex);
-
-  if (!g_thread_supported ())
-    return;
-
-  if (mutex->depth > 1)
-    {
-      mutex->depth--;
-      return;
-    }
-  g_system_thread_assign (mutex->owner, zero_thread);
-  g_static_mutex_unlock (&mutex->mutex);
+  GRecMutex *rm;
+  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
+  mutex->depth--;
+  g_rec_mutex_unlock (rm);
 }
 
 /**
@@ -754,25 +770,14 @@ void
 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
                               guint            depth)
 {
-  GSystemThread self;
-  g_return_if_fail (mutex);
-
-  if (!g_thread_supported ())
-    return;
-
-  if (depth == 0)
-    return;
-
-  g_system_thread_self (&self);
+  GRecMutex *rm;
 
-  if (g_system_thread_equal (&self, &mutex->owner))
+  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
+  while (depth--)
     {
-      mutex->depth += depth;
-      return;
+      g_rec_mutex_lock (rm);
+      mutex->depth++;
     }
-  g_static_mutex_lock (&mutex->mutex);
-  g_system_thread_assign (mutex->owner, self);
-  mutex->depth = depth;
 }
 
 /**
@@ -794,18 +799,13 @@ g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
 guint
 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
 {
-  guint depth;
-
-  g_return_val_if_fail (mutex, 0);
-
-  if (!g_thread_supported ())
-    return 1;
+  GRecMutex *rm;
+  gint depth;
 
+  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
   depth = mutex->depth;
-
-  g_system_thread_assign (mutex->owner, zero_thread);
-  mutex->depth = 0;
-  g_static_mutex_unlock (&mutex->mutex);
+  while (mutex->depth--)
+    g_rec_mutex_unlock (rm);
 
   return depth;
 }
@@ -828,7 +828,13 @@ g_static_rec_mutex_free (GStaticRecMutex *mutex)
 {
   g_return_if_fail (mutex);
 
-  g_static_mutex_free (&mutex->mutex);
+  if (mutex->mutex.mutex)
+    {
+      GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
+
+      g_rec_mutex_clear (rm);
+      g_slice_free (GRecMutex, rm);
+    }
 }
 
 /* GStaticRWLock {{{1 ----------------------------------------------------- */