glib/giowin32.c glib/gmain.c glib/gstrfuncs.c Decorating variable
[platform/upstream/glib.git] / glib / gthread.c
index 96cb3f4..5d50744 100644 (file)
@@ -1,7 +1,7 @@
 /* GLIB - Library of useful routines for C programming
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
- * gmutex.c: MT safety related functions
+ * gthread.c: MT safety related functions
  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
  *                Owen Taylor
  *
  */
 
 #include "config.h"
-#include "glib.h"
-
-#ifdef G_THREAD_USE_PID_SURROGATE
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <errno.h>
-#endif /* G_THREAD_USE_PID_SURROGATE */
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 
 #include <string.h>
 
+#include "glib.h"
+#include "gthreadinit.h"
+
 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
-# define g_system_thread_equal(thread1, thread2)                       \
-   (thread1.dummy_pointer == thread2.dummy_pointer)
+# define g_system_thread_equal_simple(thread1, thread2)                        \
+   ((thread1).dummy_pointer == (thread2).dummy_pointer)
 # define g_system_thread_assign(dest, src)                             \
-   (dest.dummy_pointer = src.dummy_pointer)
+   ((dest).dummy_pointer = (src).dummy_pointer)
 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
-# define g_system_thread_equal(thread1, thread2)                       \
-   (memcmp (&thread1, &thread2, GLIB_SIZEOF_SYSTEM_THREAD) == 0)
+# define g_system_thread_equal_simple(thread1, thread2)                        \
+   (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
 # define g_system_thread_assign(dest, src)                             \
-   (memcpy (&dest, &src, GLIB_SIZEOF_SYSTEM_THREAD))
+   (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
 
+#define g_system_thread_equal(thread1, thread2)                                \
+  (g_thread_functions_for_glib_use.thread_equal ?                      \
+   g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
+   g_system_thread_equal_simple((thread1), (thread2)))
+
 GQuark 
 g_thread_error_quark (void)
 {
@@ -77,24 +77,8 @@ struct  _GRealThread
   gpointer private_data;
   gpointer retval;
   GSystemThread system_thread;
-#ifdef G_THREAD_USE_PID_SURROGATE
-  pid_t pid;
-#endif /* G_THREAD_USE_PID_SURROGATE */
 };
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-static gint priority_map[] = { 15, 0, -15, -20 };
-static gboolean prio_warned = FALSE;
-# define SET_PRIO(pid, prio) G_STMT_START{                             \
-  gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]);  \
-  if (error == -1 && errno == EACCES && !prio_warned)                  \
-    {                                                                  \
-      prio_warned = TRUE;                                              \
-      g_warning ("Priorities can only be increased by root.");         \
-    }                                                                  \
-  }G_STMT_END
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
 typedef struct _GStaticPrivateNode GStaticPrivateNode;
 struct _GStaticPrivateNode
 {
@@ -111,9 +95,6 @@ static GSystemThread zero_thread; /* This is initialized to all zero */
 gboolean g_thread_use_default_impl = TRUE;
 gboolean g_threads_got_initialized = FALSE;
 
-#if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
-__declspec(dllexport)
-#endif
 GThreadFunctions g_thread_functions_for_glib_use = {
   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
   NULL,                                        /* mutex_lock */
@@ -141,31 +122,73 @@ GThreadFunctions g_thread_functions_for_glib_use = {
 
 /* Local data */
 
-static GMutex   *g_mutex_protect_static_mutex_allocation = NULL;
+static GMutex   *g_once_mutex = NULL;
+static GCond    *g_once_cond = NULL;
 static GPrivate *g_thread_specific_private = NULL;
 static GSList   *g_thread_all_threads = NULL;
 static GSList   *g_thread_free_indeces = NULL;
 
 G_LOCK_DEFINE_STATIC (g_thread);
 
+#ifdef G_THREADS_ENABLED
 /* This must be called only once, before any threads are created.
  * It will only be called from g_thread_init() in -lgthread.
  */
-void
-g_mutex_init (void)
+void 
+g_thread_init_glib (void)
 {
-  GRealThread* main_thread;
   /* We let the main thread (the one that calls g_thread_init) inherit
-   * the data, that it set before calling g_thread_init
+   * the static_private data set before calling g_thread_init
    */
-  main_thread = (GRealThread*) g_thread_self ();
+  GRealThread* main_thread = (GRealThread*) g_thread_self ();
+
+  g_once_mutex = g_mutex_new ();
+  g_once_cond = g_cond_new ();
+
+  _g_convert_thread_init ();
+  _g_rand_thread_init ();
+  _g_main_thread_init ();
+  _g_mem_thread_init ();
+  _g_messages_thread_init ();
+  _g_atomic_thread_init ();
+  g_threads_got_initialized = TRUE;
 
   g_thread_specific_private = g_private_new (g_thread_cleanup);
-  G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
+  g_private_set (g_thread_specific_private, main_thread);
   G_THREAD_UF (thread_self, (&main_thread->system_thread));
 
-  g_mutex_protect_static_mutex_allocation = g_mutex_new ();
+  _g_mem_thread_private_init ();
+  _g_messages_thread_private_init ();
+
+}
+#endif /* G_THREADS_ENABLED */
+
+gpointer 
+g_once_impl (GOnce       *once, 
+            GThreadFunc  func, 
+            gpointer     arg)
+{
+  g_mutex_lock (g_once_mutex);
+
+  while (once->status == G_ONCE_STATUS_PROGRESS)
+    g_cond_wait (g_once_cond, g_once_mutex);
+  
+  if (once->status != G_ONCE_STATUS_READY)
+    {
+      once->status = G_ONCE_STATUS_PROGRESS;
+      g_mutex_unlock (g_once_mutex);
+  
+      once->retval = func (arg);
+
+      g_mutex_lock (g_once_mutex);
+      once->status = G_ONCE_STATUS_READY;
+      g_cond_broadcast (g_once_cond);
+    }
+  
+  g_mutex_unlock (g_once_mutex);
+  
+  return once->retval;
 }
 
 void 
@@ -184,14 +207,23 @@ g_static_mutex_get_mutex_impl (GMutex** mutex)
   if (!g_thread_supported ())
     return NULL;
 
-  g_assert (g_mutex_protect_static_mutex_allocation);
+  g_assert (g_once_mutex);
 
-  g_mutex_lock (g_mutex_protect_static_mutex_allocation);
+  g_mutex_lock (g_once_mutex);
 
   if (!(*mutex)) 
-    *mutex = g_mutex_new (); 
+    {
+      GMutex *new_mutex = g_mutex_new (); 
+      
+      /* The following is a memory barrier to avoid the write 
+       * to *new_mutex being reordered to after writing *mutex */
+      g_mutex_lock (new_mutex);
+      g_mutex_unlock (new_mutex);
+      
+      *mutex = new_mutex;
+    }
 
-  g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
+  g_mutex_unlock (g_once_mutex);
   
   return *mutex;
 }
@@ -313,7 +345,7 @@ g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
 guint    
 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
 {
-  gint depth;
+  guint depth;
 
   g_return_val_if_fail (mutex, 0);
 
@@ -513,10 +545,6 @@ g_thread_create_proxy (gpointer data)
 
   g_assert (data);
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-  thread->pid = getpid ();
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
   /* This has to happen before G_LOCK, as that might call g_thread_self */
   g_private_set (g_thread_specific_private, data);
 
@@ -525,11 +553,6 @@ g_thread_create_proxy (gpointer data)
   G_LOCK (g_thread);
   G_UNLOCK (g_thread);
  
-#ifdef G_THREAD_USE_PID_SURROGATE
-  if (g_thread_use_default_impl)
-    SET_PRIO (thread->pid, thread->thread.priority);
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
   thread->retval = thread->thread.func (thread->thread.data);
 
   return NULL;
@@ -544,12 +567,14 @@ g_thread_create_full (GThreadFunc                  func,
                      GThreadPriority    priority,
                      GError                **error)
 {
-  GRealThread* result = g_new (GRealThread, 1);
+  GRealThread* result;
   GError *local_error = NULL;
   g_return_val_if_fail (func, NULL);
   g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
   g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
   
+  result = g_new (GRealThread, 1);
+
   result->thread.joinable = joinable;
   result->thread.priority = priority;
   result->thread.func = func;
@@ -625,13 +650,8 @@ g_thread_set_priority (GThread* thread,
 
   thread->priority = priority;
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-  if (g_thread_use_default_impl)
-    SET_PRIO (real->pid, priority);
-  else
-#endif /* G_THREAD_USE_PID_SURROGATE */
-    G_THREAD_CF (thread_set_priority, (void)0, 
-                (&real->system_thread, priority));
+  G_THREAD_CF (thread_set_priority, (void)0, 
+              (&real->system_thread, priority));
 }
 
 GThread*
@@ -655,10 +675,6 @@ g_thread_self (void)
       if (g_thread_supported ())
        G_THREAD_UF (thread_self, (&thread->system_thread));
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-      thread->pid = getpid ();
-#endif /* G_THREAD_USE_PID_SURROGATE */
-      
       g_private_set (g_thread_specific_private, thread); 
       
       G_LOCK (g_thread);
@@ -706,7 +722,7 @@ g_static_rw_lock_reader_lock (GStaticRWLock* lock)
 
   g_static_mutex_lock (&lock->mutex);
   lock->want_to_read++;
-  while (lock->write || lock->want_to_write) 
+  while (lock->have_writer || lock->want_to_write) 
     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
   lock->want_to_read--;
   lock->read_counter++;
@@ -724,7 +740,7 @@ g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
     return TRUE;
 
   g_static_mutex_lock (&lock->mutex);
-  if (!lock->write && !lock->want_to_write)
+  if (!lock->have_writer && !lock->want_to_write)
     {
       lock->read_counter++;
       ret_val = TRUE;
@@ -758,10 +774,10 @@ g_static_rw_lock_writer_lock (GStaticRWLock* lock)
 
   g_static_mutex_lock (&lock->mutex);
   lock->want_to_write++;
-  while (lock->write || lock->read_counter)
+  while (lock->have_writer || lock->read_counter)
     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
   lock->want_to_write--;
-  lock->write = TRUE;
+  lock->have_writer = TRUE;
   g_static_mutex_unlock (&lock->mutex);
 }
 
@@ -776,9 +792,9 @@ g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
     return TRUE;
 
   g_static_mutex_lock (&lock->mutex);
-  if (!lock->write && !lock->read_counter)
+  if (!lock->have_writer && !lock->read_counter)
     {
-      lock->write = TRUE;
+      lock->have_writer = TRUE;
       ret_val = TRUE;
     }
   g_static_mutex_unlock (&lock->mutex);
@@ -794,7 +810,7 @@ g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
     return;
 
   g_static_mutex_lock (&lock->mutex);
-  lock->write = FALSE; 
+  lock->have_writer = FALSE; 
   g_static_rw_lock_signal (lock);
   g_static_mutex_unlock (&lock->mutex);
 }