glib/libgthread/src/gthread-impl.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  *
       
     4  * gthread.c: thread related functions
       
     5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
       
     6  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     7  *
       
     8  * This library is free software; you can redistribute it and/or
       
     9  * modify it under the terms of the GNU Lesser General Public
       
    10  * License as published by the Free Software Foundation; either
       
    11  * version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This library is distributed in the hope that it will be useful,
       
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
       
    16  * Lesser General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU Lesser General Public
       
    19  * License along with this library; if not, write to the
       
    20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    21  * Boston, MA 02111-1307, USA.
       
    22  */
       
    23 
       
    24 /*
       
    25  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
       
    26  * file for a list of people on the GLib Team.  See the ChangeLog
       
    27  * files for a list of changes.  These files are distributed with
       
    28  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
       
    29  */
       
    30 
       
    31 /* 
       
    32  * MT safe
       
    33  */
       
    34 
       
    35 #ifdef HAVE_CONFIG_H
       
    36 #include <config.h>
       
    37 #endif
       
    38 
       
    39 #include <glib.h>
       
    40 #include <gthreadinit.h>
       
    41 
       
    42 #ifdef __SYMBIAN32__
       
    43 #include <glib_global.h>
       
    44 #include <gthread_wsd.h>
       
    45 #endif /* __SYMBIAN32__ */
       
    46 #ifdef G_THREADS_ENABLED
       
    47 
       
    48 #if EMULATOR
       
    49 
       
    50 PLS(thread_system_already_initialized,gthread_impl,gboolean)
       
    51 PLS_ARRAY(g_thread_priority_map,gthread_impl,gint)
       
    52 
       
    53 #define thread_system_already_initialized (*FUNCTION_NAME(thread_system_already_initialized,gthread_impl)())
       
    54 #define g_thread_priority_map (FUNCTION_NAME(g_thread_priority_map,gthread_impl)())
       
    55 
       
    56 #else
       
    57 
       
    58 static gboolean thread_system_already_initialized = FALSE;
       
    59 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
       
    60 
       
    61 #endif /* EMULATOR */
       
    62 
       
    63 #include G_THREAD_SOURCE
       
    64 
       
    65 #ifndef PRIORITY_LOW_VALUE
       
    66 # define PRIORITY_LOW_VALUE 0
       
    67 #endif
       
    68 
       
    69 #ifndef PRIORITY_URGENT_VALUE
       
    70 # define PRIORITY_URGENT_VALUE 0
       
    71 #endif
       
    72 
       
    73 #ifndef PRIORITY_NORMAL_VALUE
       
    74 # define PRIORITY_NORMAL_VALUE						\
       
    75   ((PRIORITY_LOW_VALUE * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
       
    76 #endif /* PRIORITY_NORMAL_VALUE */
       
    77 
       
    78 #ifndef PRIORITY_HIGH_VALUE
       
    79 # define PRIORITY_HIGH_VALUE						\
       
    80   ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
       
    81 #endif /* PRIORITY_HIGH_VALUE */
       
    82 
       
    83 void g_mutex_init (void);
       
    84 void g_mem_init (void);
       
    85 void g_messages_init (void);
       
    86 void g_convert_init (void);
       
    87 void g_rand_init (void);
       
    88 void g_main_thread_init (void);
       
    89 
       
    90 #define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
       
    91 
       
    92 typedef struct _ErrorCheckInfo ErrorCheckInfo;
       
    93 struct _ErrorCheckInfo
       
    94 {
       
    95   gchar *location;
       
    96   GThread *owner;
       
    97 };
       
    98 
       
    99 static GMutex *
       
   100 g_mutex_new_errorcheck_impl (void)
       
   101 {
       
   102   GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
       
   103   retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
       
   104   G_MUTEX_DEBUG_INFO (retval) = NULL;
       
   105   return retval;
       
   106 }
       
   107 
       
   108 static void
       
   109 g_mutex_lock_errorcheck_impl (GMutex *mutex, 
       
   110 			      gulong magic,
       
   111 			      gchar *location)
       
   112 {
       
   113   ErrorCheckInfo *info;
       
   114   GThread *self = g_thread_self ();
       
   115 
       
   116   if (magic != G_MUTEX_DEBUG_MAGIC)
       
   117     location = "unknown";
       
   118 
       
   119   if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
       
   120     {
       
   121       /* if the debug info is NULL, we have not yet locked that mutex,
       
   122        * so we do it now */
       
   123       g_thread_functions_for_glib_use_default.mutex_lock (mutex);
       
   124       /* Now we have to check again, because another thread might have
       
   125        * tried to lock the mutex at the same time we did. This
       
   126        * technique is not 100% save on systems without decent cache
       
   127        * coherence, but we have no choice */
       
   128       if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
       
   129 	{
       
   130 	  info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
       
   131 	}
       
   132       g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
       
   133     }
       
   134   
       
   135   info = G_MUTEX_DEBUG_INFO (mutex);
       
   136   if (info->owner == self)
       
   137     g_error ("Trying to recursivly lock a mutex at '%s', "
       
   138 	     "previously locked at '%s'", 
       
   139 	     location, info->location);
       
   140 
       
   141   g_thread_functions_for_glib_use_default.mutex_lock (mutex);
       
   142 
       
   143   info->owner = self;
       
   144   info->location = location;
       
   145 }
       
   146 
       
   147 static gboolean
       
   148 g_mutex_trylock_errorcheck_impl (GMutex *mutex, 
       
   149 				 gulong magic, 
       
   150 				 gchar *location)
       
   151 {
       
   152   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
       
   153   GThread *self = g_thread_self ();
       
   154 
       
   155   if (magic != G_MUTEX_DEBUG_MAGIC)
       
   156     location = "unknown";
       
   157 
       
   158   if (!info)
       
   159     {
       
   160       /* This mutex has not yet been used, so simply lock and return TRUE */
       
   161       g_mutex_lock_errorcheck_impl (mutex, magic, location);
       
   162       return TRUE;
       
   163     }
       
   164 
       
   165   if (info->owner == self)
       
   166     g_error ("Trying to recursivly lock a mutex at '%s', "
       
   167 	     "previously locked at '%s'", 
       
   168 	     location, info->location);
       
   169   
       
   170   if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
       
   171     return FALSE;
       
   172 
       
   173   info->owner = self;
       
   174   info->location = location;
       
   175 
       
   176   return TRUE;
       
   177 }
       
   178 
       
   179 static void
       
   180 g_mutex_unlock_errorcheck_impl (GMutex *mutex, 
       
   181 				gulong magic, 
       
   182 				gchar *location)
       
   183 {
       
   184   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
       
   185   GThread *self = g_thread_self ();
       
   186 
       
   187   if (magic != G_MUTEX_DEBUG_MAGIC)
       
   188     location = "unknown";
       
   189 
       
   190   if (!info || info->owner == NULL)
       
   191     g_error ("Trying to unlock an unlocked mutex at '%s'", location);
       
   192 
       
   193   if (info->owner != self)
       
   194     g_warning ("Trying to unlock a mutex at '%s', "
       
   195 	       "previously locked by a different thread at '%s'",
       
   196 	       location, info->location);
       
   197 
       
   198   info->owner = NULL;
       
   199   info->location = NULL;
       
   200 
       
   201   g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
       
   202 }
       
   203 
       
   204 static void
       
   205 g_mutex_free_errorcheck_impl (GMutex *mutex, 
       
   206 			      gulong magic, 
       
   207 			      gchar *location)
       
   208 {
       
   209   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
       
   210   
       
   211   if (magic != G_MUTEX_DEBUG_MAGIC)
       
   212     location = "unknown";
       
   213 
       
   214   if (info && info->owner != NULL)
       
   215     g_error ("Trying to free a locked mutex at '%s', "
       
   216 	     "which was previously locked at '%s'", 
       
   217 	     location, info->location);
       
   218 
       
   219   g_free (G_MUTEX_DEBUG_INFO (mutex));
       
   220   g_thread_functions_for_glib_use_default.mutex_free (mutex);  
       
   221 }
       
   222 
       
   223 static void     
       
   224 g_cond_wait_errorcheck_impl (GCond *cond,
       
   225 			     GMutex *mutex, 
       
   226 			     gulong magic, 
       
   227 			     gchar *location)
       
   228 {
       
   229   
       
   230   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
       
   231   GThread *self = g_thread_self ();
       
   232 
       
   233   if (magic != G_MUTEX_DEBUG_MAGIC)
       
   234     location = "unknown";
       
   235 
       
   236   if (!info || info->owner == NULL)
       
   237     g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
       
   238 	     location);
       
   239 
       
   240   if (info->owner != self)
       
   241     g_error ("Trying to use a mutex locked by another thread in "
       
   242 	     "g_cond_wait() at '%s'", location);
       
   243 
       
   244   info->owner = NULL;
       
   245   location = info->location;
       
   246 
       
   247   g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
       
   248 
       
   249   info->owner = self;
       
   250   info->location = location;
       
   251 }
       
   252     
       
   253 
       
   254 static gboolean 
       
   255 g_cond_timed_wait_errorcheck_impl (GCond *cond,
       
   256                                    GMutex *mutex,
       
   257                                    GTimeVal *end_time, 
       
   258 				   gulong magic, 
       
   259 				   gchar *location)
       
   260 {
       
   261   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
       
   262   GThread *self = g_thread_self ();
       
   263   gboolean retval;
       
   264 
       
   265   if (magic != G_MUTEX_DEBUG_MAGIC)
       
   266     location = "unknown";
       
   267 
       
   268   if (!info || info->owner == NULL)
       
   269     g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
       
   270 	     location);
       
   271 
       
   272   if (info->owner != self)
       
   273     g_error ("Trying to use a mutex locked by another thread in "
       
   274 	     "g_cond_timed_wait() at '%s'", location);
       
   275 
       
   276   info->owner = NULL;
       
   277   location = info->location;
       
   278   
       
   279   retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond, 
       
   280 								    mutex, 
       
   281 								    end_time);
       
   282 
       
   283   info->owner = self;
       
   284   info->location = location;
       
   285 
       
   286   return retval;
       
   287 }
       
   288 
       
   289 
       
   290 /* unshadow function declaration. See gthread.h */
       
   291 #undef g_thread_init
       
   292 
       
   293 EXPORT_C void 
       
   294 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
       
   295 {
       
   296   GThreadFunctions errorcheck_functions;
       
   297   if (init)
       
   298     g_error ("Errorcheck mutexes can only be used for native " 
       
   299 	     "thread implementations. Sorry." );
       
   300 
       
   301 #ifdef HAVE_G_THREAD_IMPL_INIT
       
   302   /* This isn't called in g_thread_init, as it doesn't think to get
       
   303    * the default implementation, so we have to call it on our own.
       
   304    *
       
   305    * We must call this before copying
       
   306    * g_thread_functions_for_glib_use_default as the
       
   307    * implementation-specific init function might modify the contents
       
   308    * of g_thread_functions_for_glib_use_default based on operating
       
   309    * system version, C library version, or whatever. */
       
   310   g_thread_impl_init();
       
   311 #endif /* HAVE_G_THREAD_IMPL_INIT */
       
   312 
       
   313   errorcheck_functions = g_thread_functions_for_glib_use_default;
       
   314   errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
       
   315   errorcheck_functions.mutex_lock = 
       
   316     (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
       
   317   errorcheck_functions.mutex_trylock = 
       
   318     (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
       
   319   errorcheck_functions.mutex_unlock = 
       
   320     (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
       
   321   errorcheck_functions.mutex_free = 
       
   322     (void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
       
   323   errorcheck_functions.cond_wait = 
       
   324     (void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
       
   325   errorcheck_functions.cond_timed_wait = 
       
   326     (gboolean (*)(GCond *, GMutex *, GTimeVal *)) 
       
   327     g_cond_timed_wait_errorcheck_impl;
       
   328     
       
   329   g_thread_init (&errorcheck_functions);
       
   330 }
       
   331 
       
   332 EXPORT_C void
       
   333 g_thread_init (GThreadFunctions* init)
       
   334 {
       
   335   gboolean supported;
       
   336 
       
   337   if (thread_system_already_initialized)
       
   338     g_error ("GThread system may only be initialized once.");
       
   339     
       
   340   thread_system_already_initialized = TRUE;
       
   341 
       
   342   if (init == NULL)
       
   343     {
       
   344 #ifdef HAVE_G_THREAD_IMPL_INIT
       
   345       /* now do any initialization stuff required by the
       
   346        * implementation, but only if called with a NULL argument, of
       
   347        * course. Otherwise it's up to the user to do so. */
       
   348       g_thread_impl_init();
       
   349 #endif /* HAVE_G_THREAD_IMPL_INIT */
       
   350       init = (GThreadFunctions*)&g_thread_functions_for_glib_use_default;
       
   351     }
       
   352   else
       
   353     g_thread_use_default_impl = FALSE;
       
   354 
       
   355   g_thread_functions_for_glib_use = *init;
       
   356 
       
   357   supported = (init->mutex_new &&  
       
   358 	       init->mutex_lock && 
       
   359 	       init->mutex_trylock && 
       
   360 	       init->mutex_unlock && 
       
   361 	       init->mutex_free && 
       
   362 	       init->cond_new && 
       
   363 	       init->cond_signal && 
       
   364 	       init->cond_broadcast && 
       
   365 	       init->cond_wait && 
       
   366 	       init->cond_timed_wait &&
       
   367 	       init->cond_free &&
       
   368 	       init->private_new &&
       
   369 	       init->private_get &&
       
   370 	       init->private_set &&
       
   371 	       init->thread_create &&
       
   372 	       init->thread_yield &&
       
   373 	       init->thread_join &&
       
   374 	       init->thread_exit &&
       
   375 	       init->thread_set_priority &&
       
   376 	       init->thread_self);
       
   377 
       
   378   /* if somebody is calling g_thread_init (), it means that he wants to
       
   379    * have thread support, so check this
       
   380    */
       
   381   if (!supported)
       
   382     {
       
   383       if (g_thread_use_default_impl)
       
   384 	g_error ("Threads are not supported on this platform.");
       
   385       else
       
   386 	g_error ("The supplied thread function vector is invalid.");
       
   387     }
       
   388 
       
   389   g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
       
   390   g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
       
   391   g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
       
   392   g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
       
   393 
       
   394   g_thread_init_glib ();
       
   395 }
       
   396 
       
   397 #else /* !G_THREADS_ENABLED */
       
   398 
       
   399 void
       
   400 EXPORT_C g_thread_init (GThreadFunctions* init)
       
   401 {
       
   402   g_error ("GLib thread support is disabled.");
       
   403 }
       
   404 
       
   405 #endif /* !G_THREADS_ENABLED */