Update VS 2008/2010 property sheets
[platform/upstream/glib.git] / gthread / gthread-impl.c
index ef79584..b483657 100644 (file)
  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-/* 
+/*
  * MT safe
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "config.h"
 
-#include <glib.h>
+#include "glib.h"
+#include "gthreadprivate.h"
 
 #ifdef G_THREADS_ENABLED
 
+static GSystemThread zero_thread; /* This is initialized to all zero */
 static gboolean thread_system_already_initialized = FALSE;
 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
 
@@ -53,214 +53,194 @@ static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
 #endif
 
 #ifndef PRIORITY_NORMAL_VALUE
-# define PRIORITY_NORMAL_VALUE                                                 \
-  (PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 4 / 10)
+# define PRIORITY_NORMAL_VALUE                                         \
+  ((PRIORITY_LOW_VALUE * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
 #endif /* PRIORITY_NORMAL_VALUE */
 
 #ifndef PRIORITY_HIGH_VALUE
-# define PRIORITY_HIGH_VALUE                                           \
-  (PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 8 / 10) 
+# define PRIORITY_HIGH_VALUE                                           \
+  ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
 #endif /* PRIORITY_HIGH_VALUE */
 
 void g_mutex_init (void);
 void g_mem_init (void);
 void g_messages_init (void);
+void g_convert_init (void);
+void g_rand_init (void);
+void g_main_thread_init (void);
 
-#define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
-
-typedef struct _ErrorCheckInfo ErrorCheckInfo;
-struct _ErrorCheckInfo
+typedef struct _GMutexDebugInfo GMutexDebugInfo;
+struct _GMutexDebugInfo
 {
   gchar *location;
-  GThread *owner;
+  GSystemThread owner;
 };
 
+#define G_MUTEX_DEBUG_INFO(mutex)                                      \
+  (((GMutexDebugInfo*)(((char*)mutex)+G_MUTEX_SIZE)))
+
 static GMutex *
 g_mutex_new_errorcheck_impl (void)
 {
   GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
-  retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
-  G_MUTEX_DEBUG_INFO (retval) = NULL;
+  GMutexDebugInfo *info;
+  retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (GMutexDebugInfo));
+
+  info = G_MUTEX_DEBUG_INFO (retval);
+  g_system_thread_assign (info->owner, zero_thread);
+  info->location = "invalid";
+
   return retval;
 }
 
 static void
-g_mutex_lock_errorcheck_impl (GMutex *mutex, 
-                             gulong magic,
-                             gchar *location)
+g_mutex_lock_errorcheck_impl (GMutex *mutex,
+                             const gulong magic,
+                             gchar * const location)
 {
-  ErrorCheckInfo *info;
-  GThread *self = g_thread_self ();
+  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
+  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
 
-  if (magic != G_MUTEX_DEBUG_MAGIC)
-    location = "unknown";
+  GSystemThread self;
+  g_thread_functions_for_glib_use.thread_self (&self);
 
-  if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
-    {
-      /* if the debug info is NULL, we have not yet locked that mutex,
-       * so we do it now */
-      g_thread_functions_for_glib_use_default.mutex_lock (mutex);
-      /* Now we have to check again, because another thread might have
-       * tried to lock the mutex at the same time we did. This
-       * technique is not 100% save on systems without decent cache
-       * coherence, but we have no choice */
-      if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
-       {
-         info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
-       }
-      g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
-    }
-  
-  info = G_MUTEX_DEBUG_INFO (mutex);
-  if (info->owner == self)
-    g_error ("Trying to recursivly lock a mutex at '%s', "
-            "previously locked at '%s'", 
-            location, info->location);
+  if (g_system_thread_equal (info->owner, self))
+    g_error ("Trying to recursively lock a mutex at '%s', "
+            "previously locked at '%s'",
+            loc, info->location);
 
   g_thread_functions_for_glib_use_default.mutex_lock (mutex);
 
-  info->owner = self;
-  info->location = location;
+  g_system_thread_assign (info->owner, self);
+  info->location = loc;
 }
 
 static gboolean
-g_mutex_trylock_errorcheck_impl (GMutex *mutex, 
-                                gulong magic, 
-                                gchar *location)
+g_mutex_trylock_errorcheck_impl (GMutex *mutex,
+                                const gulong magic,
+                                gchar * const location)
 {
-  ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
-  GThread *self = g_thread_self ();
+  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
+  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
 
-  if (magic != G_MUTEX_DEBUG_MAGIC)
-    location = "unknown";
+  GSystemThread self;
+  g_thread_functions_for_glib_use.thread_self (&self);
 
-  if (!info)
-    {
-      /* This mutex has not yet been used, so simply lock and return TRUE */
-      g_mutex_lock_errorcheck_impl (mutex, magic, location);
-      return TRUE;
-    }
-
-  if (info->owner == self)
+  if (g_system_thread_equal (info->owner, self))
     g_error ("Trying to recursivly lock a mutex at '%s', "
-            "previously locked at '%s'", 
-            location, info->location);
-  
+            "previously locked at '%s'",
+            loc, info->location);
+
   if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
     return FALSE;
 
-  info->owner = self;
-  info->location = location;
+  g_system_thread_assign (info->owner, self);
+  info->location = loc;
 
   return TRUE;
 }
 
 static void
-g_mutex_unlock_errorcheck_impl (GMutex *mutex, 
-                               gulong magic, 
-                               gchar *location)
+g_mutex_unlock_errorcheck_impl (GMutex *mutex,
+                               const gulong magic,
+                               gchar * const location)
 {
-  ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
-  GThread *self = g_thread_self ();
+  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
+  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
 
-  if (magic != G_MUTEX_DEBUG_MAGIC)
-    location = "unknown";
+  GSystemThread self;
+  g_thread_functions_for_glib_use.thread_self (&self);
 
-  if (!info || info->owner == NULL)
-    g_error ("Trying to unlock an unlocked mutex at '%s'", location);
+  if (g_system_thread_equal (info->owner, zero_thread))
+    g_error ("Trying to unlock an unlocked mutex at '%s'", loc);
 
-  if (info->owner != self)
+  if (!g_system_thread_equal (info->owner, self))
     g_warning ("Trying to unlock a mutex at '%s', "
               "previously locked by a different thread at '%s'",
-              location, info->location);
+              loc, info->location);
 
-  info->owner = NULL;
+  g_system_thread_assign (info->owner, zero_thread);
   info->location = NULL;
 
   g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
 }
 
 static void
-g_mutex_free_errorcheck_impl (GMutex *mutex, 
-                             gulong magic, 
-                             gchar *location)
+g_mutex_free_errorcheck_impl (GMutex *mutex,
+                             const gulong magic,
+                             gchar * const location)
 {
-  ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
-  
-  if (magic != G_MUTEX_DEBUG_MAGIC)
-    location = "unknown";
+  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
+  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
 
-  if (info && info->owner != NULL)
+  if (info && !g_system_thread_equal (info->owner, zero_thread))
     g_error ("Trying to free a locked mutex at '%s', "
-            "which was previously locked at '%s'", 
-            location, info->location);
+            "which was previously locked at '%s'",
+            loc, info->location);
 
-  g_free (G_MUTEX_DEBUG_INFO (mutex));
-  g_thread_functions_for_glib_use_default.mutex_free (mutex);  
+  g_thread_functions_for_glib_use_default.mutex_free (mutex);
 }
 
-static void     
+static void
 g_cond_wait_errorcheck_impl (GCond *cond,
-                            GMutex *mutex, 
-                            gulong magic, 
-                            gchar *location)
+                            GMutex *mutex,
+                            const gulong magic,
+                            gchar * const location)
 {
-  
-  ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
-  GThread *self = g_thread_self ();
+  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
+  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
 
-  if (magic != G_MUTEX_DEBUG_MAGIC)
-    location = "unknown";
+  GSystemThread self;
+  g_thread_functions_for_glib_use.thread_self (&self);
 
-  if (!info || info->owner == NULL)
-    g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
-            location);
+  if (g_system_thread_equal (info->owner, zero_thread))
+    g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'", loc);
 
-  if (info->owner != self)
+  if (!g_system_thread_equal (info->owner, self))
     g_error ("Trying to use a mutex locked by another thread in "
-            "g_cond_wait() at '%s'", location);
+            "g_cond_wait() at '%s'", loc);
 
-  info->owner = NULL;
-  location = info->location;
+  g_system_thread_assign (info->owner, zero_thread);
+  loc = info->location;
 
   g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
 
-  info->owner = self;
-  info->location = location;
+  g_system_thread_assign (info->owner, self);
+  info->location = loc;
 }
-    
 
-static gboolean 
+
+static gboolean
 g_cond_timed_wait_errorcheck_impl (GCond *cond,
                                    GMutex *mutex,
-                                   GTimeVal *end_time, 
-                                  gulong magic, 
-                                  gchar *location)
+                                   GTimeVal *end_time,
+                                  const gulong magic,
+                                  gchar * const location)
 {
-  ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
-  GThread *self = g_thread_self ();
+  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
+  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
   gboolean retval;
 
-  if (magic != G_MUTEX_DEBUG_MAGIC)
-    location = "unknown";
+  GSystemThread self;
+  g_thread_functions_for_glib_use.thread_self (&self);
 
-  if (!info || info->owner == NULL)
+  if (g_system_thread_equal (info->owner, zero_thread))
     g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
-            location);
+            loc);
 
-  if (info->owner != self)
+  if (!g_system_thread_equal (info->owner, self))
     g_error ("Trying to use a mutex locked by another thread in "
-            "g_cond_timed_wait() at '%s'", location);
+            "g_cond_timed_wait() at '%s'", loc);
 
-  info->owner = NULL;
-  location = info->location;
-  
-  retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond, 
-                                                                   mutex, 
+  g_system_thread_assign (info->owner, zero_thread);
+  loc = info->location;
+
+  retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
+                                                                   mutex,
                                                                    end_time);
 
-  info->owner = self;
-  info->location = location;
+  g_system_thread_assign (info->owner, self);
+  info->location = loc;
 
   return retval;
 }
@@ -269,12 +249,12 @@ g_cond_timed_wait_errorcheck_impl (GCond *cond,
 /* unshadow function declaration. See gthread.h */
 #undef g_thread_init
 
-void 
+void
 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
 {
   GThreadFunctions errorcheck_functions;
   if (init)
-    g_error ("Errorcheck mutexes can only be used for native " 
+    g_error ("Errorcheck mutexes can only be used for native "
             "thread implementations. Sorry." );
 
 #ifdef HAVE_G_THREAD_IMPL_INIT
@@ -291,20 +271,20 @@ g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
 
   errorcheck_functions = g_thread_functions_for_glib_use_default;
   errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
-  errorcheck_functions.mutex_lock = 
+  errorcheck_functions.mutex_lock =
     (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
-  errorcheck_functions.mutex_trylock = 
+  errorcheck_functions.mutex_trylock =
     (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
-  errorcheck_functions.mutex_unlock = 
+  errorcheck_functions.mutex_unlock =
     (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
-  errorcheck_functions.mutex_free = 
+  errorcheck_functions.mutex_free =
     (void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
-  errorcheck_functions.cond_wait = 
+  errorcheck_functions.cond_wait =
     (void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
-  errorcheck_functions.cond_timed_wait = 
-    (gboolean (*)(GCond *, GMutex *, GTimeVal *)) 
+  errorcheck_functions.cond_timed_wait =
+    (gboolean (*)(GCond *, GMutex *, GTimeVal *))
     g_cond_timed_wait_errorcheck_impl;
-    
+
   g_thread_init (&errorcheck_functions);
 }
 
@@ -314,8 +294,13 @@ g_thread_init (GThreadFunctions* init)
   gboolean supported;
 
   if (thread_system_already_initialized)
-    g_error ("GThread system may only be initialized once.");
-    
+    {
+      if (init != NULL)
+       g_warning ("GThread system already initialized, ignoring custom thread implementation.");
+
+      return;
+    }
+
   thread_system_already_initialized = TRUE;
 
   if (init == NULL)
@@ -332,24 +317,23 @@ g_thread_init (GThreadFunctions* init)
     g_thread_use_default_impl = FALSE;
 
   g_thread_functions_for_glib_use = *init;
-
-  /* It is important, that g_threads_got_initialized is not set before the
-   * thread initialization functions of the different modules are called
-   */
-  supported = (init->mutex_new &&  
-              init->mutex_lock && 
-              init->mutex_trylock && 
-              init->mutex_unlock && 
-              init->mutex_free && 
-              init->cond_new && 
-              init->cond_signal && 
-              init->cond_broadcast && 
-              init->cond_wait && 
+  if (g_thread_gettime_impl)
+    g_thread_gettime = g_thread_gettime_impl;
+
+  supported = (init->mutex_new &&
+              init->mutex_lock &&
+              init->mutex_trylock &&
+              init->mutex_unlock &&
+              init->mutex_free &&
+              init->cond_new &&
+              init->cond_signal &&
+              init->cond_broadcast &&
+              init->cond_wait &&
               init->cond_timed_wait &&
               init->cond_free &&
               init->private_new &&
               init->private_get &&
-              init->private_get &&
+              init->private_set &&
               init->thread_create &&
               init->thread_yield &&
               init->thread_join &&
@@ -373,20 +357,7 @@ g_thread_init (GThreadFunctions* init)
   g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
   g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
 
-  /* now call the thread initialization functions of the different
-   * glib modules. order does matter, g_mutex_init MUST come first.
-   */
-  g_mutex_init ();
-  g_mem_init ();
-  g_messages_init ();
-
-  /* now we can set g_threads_got_initialized and thus enable
-   * all the thread functions
-   */
-  g_threads_got_initialized = TRUE;
-
-  /* we want the main thread to run with normal priority */
-  g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
+  g_thread_init_glib ();
 }
 
 #else /* !G_THREADS_ENABLED */
@@ -397,4 +368,10 @@ g_thread_init (GThreadFunctions* init)
   g_error ("GLib thread support is disabled.");
 }
 
+void
+g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
+{
+  g_error ("GLib thread support is disabled.");
+}
+
 #endif /* !G_THREADS_ENABLED */