glib/libglib/src/gthread.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: MT safety related functions
       
     5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
       
     6  *                Owen Taylor
       
     7  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     8  *
       
     9  * This library is free software; you can redistribute it and/or
       
    10  * modify it under the terms of the GNU Lesser General Public
       
    11  * License as published by the Free Software Foundation; either
       
    12  * version 2 of the License, or (at your option) any later version.
       
    13  *
       
    14  * This library is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
       
    17  * Lesser General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU Lesser General Public
       
    20  * License along with this library; if not, write to the
       
    21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    22  * Boston, MA 02111-1307, USA.
       
    23  */
       
    24 
       
    25 /*
       
    26  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
       
    27  * file for a list of people on the GLib Team.  See the ChangeLog
       
    28  * files for a list of changes.  These files are distributed with
       
    29  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
       
    30  */
       
    31 
       
    32 /* 
       
    33  * MT safe
       
    34  */
       
    35 
       
    36 #include "config.h"
       
    37 
       
    38 #ifdef HAVE_UNISTD_H
       
    39 #include <unistd.h>
       
    40 #endif
       
    41 
       
    42 #include <string.h>
       
    43 
       
    44 #include "glib.h"
       
    45 #include "gthreadinit.h"
       
    46 #include "galias.h"
       
    47 
       
    48 #ifdef __SYMBIAN32__
       
    49 #include <glib_wsd.h>
       
    50 #include "glibbackend.h"
       
    51 #endif /* __SYMBIAN32__ */
       
    52 
       
    53 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
       
    54 # define g_system_thread_equal_simple(thread1, thread2)			\
       
    55    ((thread1).dummy_pointer == (thread2).dummy_pointer)
       
    56 # define g_system_thread_assign(dest, src)				\
       
    57    ((dest).dummy_pointer = (src).dummy_pointer)
       
    58 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
       
    59 # define g_system_thread_equal_simple(thread1, thread2)			\
       
    60    (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
       
    61 # define g_system_thread_assign(dest, src)				\
       
    62    (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
       
    63 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
       
    64 
       
    65 #define g_system_thread_equal(thread1, thread2)				\
       
    66   (g_thread_functions_for_glib_use.thread_equal ? 			\
       
    67    g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
       
    68    g_system_thread_equal_simple((thread1), (thread2)))
       
    69 
       
    70 #if EMULATOR
       
    71 
       
    72 PLS(quark,g_thread_error_quark ,GQuark)
       
    73 #define quark (*FUNCTION_NAME(quark,g_thread_error_quark )())
       
    74 
       
    75 #endif /* EMULATOR */
       
    76 
       
    77 EXPORT_C GQuark 
       
    78 g_thread_error_quark (void)
       
    79 {
       
    80   #if !(EMULATOR)
       
    81   static GQuark quark;
       
    82   #endif /* EMULATOR */
       
    83   if (!quark)
       
    84     quark = g_quark_from_static_string ("g_thread_error");
       
    85   return quark;
       
    86 }
       
    87 
       
    88 #if !(EMULATOR)
       
    89 #undef quark
       
    90 #endif /* EMULATOR */
       
    91   
       
    92 /* Keep this in sync with GRealThread in gmain.c! */
       
    93 #if !(EMULATOR)
       
    94 typedef struct _GRealThread GRealThread;
       
    95 struct  _GRealThread
       
    96 {
       
    97   GThread thread;
       
    98   gpointer private_data;
       
    99   GRealThread *next;
       
   100   gpointer retval;
       
   101   GSystemThread system_thread;
       
   102 };
       
   103 #endif /* !(EMULATOR) */
       
   104 
       
   105 typedef struct _GStaticPrivateNode GStaticPrivateNode;
       
   106 struct _GStaticPrivateNode
       
   107 {
       
   108   gpointer       data;
       
   109   GDestroyNotify destroy;
       
   110 };
       
   111 
       
   112 static void g_thread_cleanup (gpointer data);
       
   113 static void g_thread_fail (void);
       
   114 
       
   115 /* Global variables */
       
   116 
       
   117 
       
   118 #if EMULATOR
       
   119 
       
   120 PLS(zero_thread,gthread,GSystemThread)
       
   121 #define zero_thread (*FUNCTION_NAME(zero_thread,gthread)())
       
   122 
       
   123 #else
       
   124 
       
   125 static GSystemThread zero_thread; /* This is initialized to all zero */
       
   126 
       
   127 #endif /* EMULATOR */
       
   128 
       
   129 #if EMULATOR
       
   130 
       
   131 PLS(g_thread_use_default_impl,gthread,gboolean)
       
   132 PLS(g_threads_got_initialized,gthread,gboolean)
       
   133 PLS(g_thread_functions_for_glib_use,gthread,GThreadFunctions)
       
   134 
       
   135 #define g_thread_use_default_impl (*FUNCTION_NAME(g_thread_use_default_impl,gthread)())
       
   136 #define g_threads_got_initialized (*FUNCTION_NAME(g_threads_got_initialized,gthread)())
       
   137 #define g_thread_functions_for_glib_use (*FUNCTION_NAME(g_thread_functions_for_glib_use,gthread)())
       
   138 
       
   139 const GThreadFunctions temp_g_thread_functions_for_glib_use = {
       
   140   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
       
   141   NULL,                                        /* mutex_lock */
       
   142   NULL,                                        /* mutex_trylock */
       
   143   NULL,                                        /* mutex_unlock */
       
   144   NULL,                                        /* mutex_free */
       
   145   (GCond*(*)())g_thread_fail,                  /* cond_new */
       
   146   NULL,                                        /* cond_signal */
       
   147   NULL,                                        /* cond_broadcast */
       
   148   NULL,                                        /* cond_wait */
       
   149   NULL,                                        /* cond_timed_wait  */
       
   150   NULL,                                        /* cond_free */
       
   151   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
       
   152   NULL,                                        /* private_get */
       
   153   NULL,                                        /* private_set */
       
   154   (void(*)(GThreadFunc, gpointer, gulong, 
       
   155 	   gboolean, gboolean, GThreadPriority, 
       
   156 	   gpointer, GError**))g_thread_fail,  /* thread_create */
       
   157   NULL,                                        /* thread_yield */
       
   158   NULL,                                        /* thread_join */
       
   159   NULL,                                        /* thread_exit */
       
   160   NULL,                                        /* thread_set_priority */
       
   161   NULL                                         /* thread_self */
       
   162 }; 
       
   163 
       
   164 #else
       
   165 
       
   166 gboolean g_thread_use_default_impl = TRUE;
       
   167 gboolean g_threads_got_initialized = FALSE;
       
   168 GThreadFunctions g_thread_functions_for_glib_use = {
       
   169   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
       
   170   NULL,                                        /* mutex_lock */
       
   171   NULL,                                        /* mutex_trylock */
       
   172   NULL,                                        /* mutex_unlock */
       
   173   NULL,                                        /* mutex_free */
       
   174   (GCond*(*)())g_thread_fail,                  /* cond_new */
       
   175   NULL,                                        /* cond_signal */
       
   176   NULL,                                        /* cond_broadcast */
       
   177   NULL,                                        /* cond_wait */
       
   178   NULL,                                        /* cond_timed_wait  */
       
   179   NULL,                                        /* cond_free */
       
   180   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
       
   181   NULL,                                        /* private_get */
       
   182   NULL,                                        /* private_set */
       
   183   (void(*)(GThreadFunc, gpointer, gulong, 
       
   184 	   gboolean, gboolean, GThreadPriority, 
       
   185 	   gpointer, GError**))g_thread_fail,  /* thread_create */
       
   186   NULL,                                        /* thread_yield */
       
   187   NULL,                                        /* thread_join */
       
   188   NULL,                                        /* thread_exit */
       
   189   NULL,                                        /* thread_set_priority */
       
   190   NULL                                         /* thread_self */
       
   191 }; 
       
   192 #endif
       
   193 
       
   194 #ifdef __SYMBIAN32__
       
   195 GArray* _g_array_newP               (gboolean          zero_terminated,
       
   196 				   gboolean          clear_,
       
   197 				   guint             element_size);			  
       
   198 #endif // __SYMBIAN32__
       
   199 
       
   200 #ifdef __SYMBIAN32__
       
   201 EXPORT_C gboolean *_g_thread_use_default_impl()
       
   202 {
       
   203 	return &g_thread_use_default_impl;
       
   204 }
       
   205 
       
   206 EXPORT_C gboolean *_g_threads_got_initialized()
       
   207 {
       
   208 	return &g_threads_got_initialized;
       
   209 }
       
   210 
       
   211 EXPORT_C GThreadFunctions *_g_thread_functions_for_glib_use()
       
   212 {
       
   213 	return &g_thread_functions_for_glib_use;
       
   214 }
       
   215 
       
   216 EXPORT_C gboolean g_thread_supported ()
       
   217 {
       
   218 	return g_threads_got_initialized;
       
   219 }
       
   220 
       
   221 #endif /* __SYMBIAN32__ */
       
   222 
       
   223 
       
   224 /* Local data */
       
   225 
       
   226 #if EMULATOR
       
   227 
       
   228 PLS(g_once_mutex,gthread,GMutex *)
       
   229 PLS(g_once_cond,gthread,GCond *)
       
   230 PLS(g_thread_specific_private ,gthread,GPrivate *)
       
   231 PLS(g_thread_all_threads,gthread,GRealThread *)
       
   232 PLS(g_thread_free_indeces,gthread,GSList *)
       
   233 PLS_MACRO(g_thread,gthread,GStaticMutex)
       
   234 
       
   235 #define g_once_mutex (*FUNCTION_NAME(g_once_mutex,gthread)())
       
   236 #define g_once_cond (*FUNCTION_NAME(g_once_cond,gthread)())
       
   237 #define g_thread_specific_private  (*FUNCTION_NAME(g_thread_specific_private ,gthread)())
       
   238 #define g_thread_all_threads (*FUNCTION_NAME(g_thread_all_threads,gthread)())
       
   239 #define g_thread_free_indeces (*FUNCTION_NAME(g_thread_free_indeces,gthread)())
       
   240 #define g__g_thread_lock (*FUNCTION_NAME_MACRO(g_thread,gthread)())
       
   241 
       
   242 #else
       
   243 
       
   244 static GMutex   *g_once_mutex = NULL;
       
   245 static GCond    *g_once_cond = NULL;
       
   246 static GPrivate *g_thread_specific_private = NULL;
       
   247 static GRealThread *g_thread_all_threads = NULL;
       
   248 static GSList   *g_thread_free_indeces = NULL;
       
   249 
       
   250 G_LOCK_DEFINE_STATIC (g_thread);
       
   251 
       
   252 #endif /* EMULATOR */
       
   253 
       
   254 #ifdef G_THREADS_ENABLED
       
   255 /* This must be called only once, before any threads are created.
       
   256  * It will only be called from g_thread_init() in -lgthread.
       
   257  */
       
   258 EXPORT_C void 
       
   259 g_thread_init_glib (void)
       
   260 {
       
   261   /* We let the main thread (the one that calls g_thread_init) inherit
       
   262    * the static_private data set before calling g_thread_init
       
   263    */
       
   264   GRealThread* main_thread = (GRealThread*) g_thread_self ();
       
   265 
       
   266   /* mutex and cond creation works without g_threads_got_initialized */
       
   267   g_once_mutex = g_mutex_new ();
       
   268   g_once_cond = g_cond_new ();
       
   269 
       
   270   /* we may only create mutex and cond in here */
       
   271   _g_mem_thread_init_noprivate_nomessage ();
       
   272 
       
   273   /* setup the basic threading system */
       
   274   g_threads_got_initialized = TRUE;
       
   275   g_thread_specific_private = g_private_new (g_thread_cleanup);
       
   276   g_private_set (g_thread_specific_private, main_thread);
       
   277   G_THREAD_UF (thread_self, (&main_thread->system_thread));
       
   278 
       
   279   /* complete memory system initialization, g_private_*() works now */
       
   280   _g_slice_thread_init_nomessage ();
       
   281 
       
   282   /* accomplish log system initialization to enable messaging */
       
   283   _g_messages_thread_init_nomessage ();
       
   284 
       
   285   /* we may run full-fledged initializers from here */
       
   286   _g_convert_thread_init ();
       
   287   _g_rand_thread_init ();
       
   288   _g_main_thread_init ();
       
   289   _g_atomic_thread_init ();
       
   290   _g_utils_thread_init ();
       
   291 #ifdef G_OS_WIN32
       
   292   _g_win32_thread_init ();
       
   293 #endif
       
   294 }
       
   295 #endif /* G_THREADS_ENABLED */
       
   296 
       
   297 EXPORT_C gpointer 
       
   298 g_once_impl (GOnce       *once, 
       
   299 	     GThreadFunc  func, 
       
   300 	     gpointer     arg)
       
   301 {
       
   302   g_mutex_lock (g_once_mutex);
       
   303 
       
   304   while (once->status == G_ONCE_STATUS_PROGRESS)
       
   305     g_cond_wait (g_once_cond, g_once_mutex);
       
   306   
       
   307   if (once->status != G_ONCE_STATUS_READY)
       
   308     {
       
   309       once->status = G_ONCE_STATUS_PROGRESS;
       
   310       g_mutex_unlock (g_once_mutex);
       
   311   
       
   312       once->retval = func (arg);
       
   313 
       
   314       g_mutex_lock (g_once_mutex);
       
   315       once->status = G_ONCE_STATUS_READY;
       
   316       g_cond_broadcast (g_once_cond);
       
   317     }
       
   318   
       
   319   g_mutex_unlock (g_once_mutex);
       
   320   
       
   321   return once->retval;
       
   322 }
       
   323 
       
   324 EXPORT_C void 
       
   325 g_static_mutex_init (GStaticMutex *mutex)
       
   326 {
       
   327   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
       
   328 
       
   329   g_return_if_fail (mutex);
       
   330 
       
   331   *mutex = init_mutex;
       
   332 }
       
   333 
       
   334 EXPORT_C GMutex *
       
   335 g_static_mutex_get_mutex_impl (GMutex** mutex)
       
   336 {
       
   337   if (!g_thread_supported ())
       
   338     return NULL;
       
   339 
       
   340   g_assert (g_once_mutex);
       
   341 
       
   342   g_mutex_lock (g_once_mutex);
       
   343 
       
   344   if (!(*mutex)) 
       
   345     {
       
   346       GMutex *new_mutex = g_mutex_new (); 
       
   347       
       
   348       /* The following is a memory barrier to avoid the write 
       
   349        * to *new_mutex being reordered to after writing *mutex */
       
   350       g_mutex_lock (new_mutex);
       
   351       g_mutex_unlock (new_mutex);
       
   352       
       
   353       *mutex = new_mutex;
       
   354     }
       
   355 
       
   356   g_mutex_unlock (g_once_mutex);
       
   357   
       
   358   return *mutex;
       
   359 }
       
   360 
       
   361 EXPORT_C void
       
   362 g_static_mutex_free (GStaticMutex* mutex)
       
   363 {
       
   364   GMutex **runtime_mutex;
       
   365   
       
   366   g_return_if_fail (mutex);
       
   367 
       
   368   /* The runtime_mutex is the first (or only) member of GStaticMutex,
       
   369    * see both versions (of glibconfig.h) in configure.in */
       
   370   runtime_mutex = ((GMutex**)mutex);
       
   371   
       
   372   if (*runtime_mutex)
       
   373     g_mutex_free (*runtime_mutex);
       
   374 
       
   375   *runtime_mutex = NULL;
       
   376 }
       
   377 
       
   378 EXPORT_C void     
       
   379 g_static_rec_mutex_init (GStaticRecMutex *mutex)
       
   380 {
       
   381   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
       
   382   
       
   383   g_return_if_fail (mutex);
       
   384 
       
   385   *mutex = init_mutex;
       
   386 }
       
   387 
       
   388 EXPORT_C void
       
   389 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
       
   390 {
       
   391   GSystemThread self;
       
   392 
       
   393   g_return_if_fail (mutex);
       
   394 
       
   395   if (!g_thread_supported ())
       
   396     return;
       
   397 
       
   398   G_THREAD_UF (thread_self, (&self));
       
   399 
       
   400   if (g_system_thread_equal (self, mutex->owner))
       
   401     {
       
   402       mutex->depth++;
       
   403       return;
       
   404     }
       
   405   g_static_mutex_lock (&mutex->mutex);
       
   406   g_system_thread_assign (mutex->owner, self);
       
   407   mutex->depth = 1;
       
   408 }
       
   409 
       
   410 EXPORT_C gboolean
       
   411 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
       
   412 {
       
   413   GSystemThread self;
       
   414 
       
   415   g_return_val_if_fail (mutex, FALSE);
       
   416 
       
   417   if (!g_thread_supported ())
       
   418     return TRUE;
       
   419 
       
   420   G_THREAD_UF (thread_self, (&self));
       
   421 
       
   422   if (g_system_thread_equal (self, mutex->owner))
       
   423     {
       
   424       mutex->depth++;
       
   425       return TRUE;
       
   426     }
       
   427 
       
   428   if (!g_static_mutex_trylock (&mutex->mutex))
       
   429     return FALSE;
       
   430 
       
   431   g_system_thread_assign (mutex->owner, self);
       
   432   mutex->depth = 1;
       
   433   return TRUE;
       
   434 }
       
   435 
       
   436 EXPORT_C void
       
   437 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
       
   438 {
       
   439   g_return_if_fail (mutex);
       
   440 
       
   441   if (!g_thread_supported ())
       
   442     return;
       
   443 
       
   444   if (mutex->depth > 1)
       
   445     {
       
   446       mutex->depth--;
       
   447       return;
       
   448     }
       
   449   g_system_thread_assign (mutex->owner, zero_thread);
       
   450   g_static_mutex_unlock (&mutex->mutex);  
       
   451 }
       
   452 
       
   453 EXPORT_C void
       
   454 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
       
   455 				guint            depth)
       
   456 {
       
   457   GSystemThread self;
       
   458   g_return_if_fail (mutex);
       
   459 
       
   460   if (!g_thread_supported ())
       
   461     return;
       
   462 
       
   463   if (depth == 0)
       
   464     return;
       
   465 
       
   466   G_THREAD_UF (thread_self, (&self));
       
   467 
       
   468   if (g_system_thread_equal (self, mutex->owner))
       
   469     {
       
   470       mutex->depth += depth;
       
   471       return;
       
   472     }
       
   473   g_static_mutex_lock (&mutex->mutex);
       
   474   g_system_thread_assign (mutex->owner, self);
       
   475   mutex->depth = depth;
       
   476 }
       
   477 
       
   478 EXPORT_C guint    
       
   479 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
       
   480 {
       
   481   guint depth;
       
   482 
       
   483   g_return_val_if_fail (mutex, 0);
       
   484 
       
   485   if (!g_thread_supported ())
       
   486     return 1;
       
   487 
       
   488   depth = mutex->depth;
       
   489 
       
   490   g_system_thread_assign (mutex->owner, zero_thread);
       
   491   mutex->depth = 0;
       
   492   g_static_mutex_unlock (&mutex->mutex);
       
   493 
       
   494   return depth;
       
   495 }
       
   496 
       
   497 EXPORT_C void
       
   498 g_static_rec_mutex_free (GStaticRecMutex *mutex)
       
   499 {
       
   500   g_return_if_fail (mutex);
       
   501 
       
   502   g_static_mutex_free (&mutex->mutex);
       
   503 }
       
   504 
       
   505 EXPORT_C void     
       
   506 g_static_private_init (GStaticPrivate *private_key)
       
   507 {
       
   508   private_key->index = 0;
       
   509 }
       
   510 
       
   511 EXPORT_C gpointer
       
   512 g_static_private_get (GStaticPrivate *private_key)
       
   513 {
       
   514   GRealThread *self = (GRealThread*) g_thread_self ();
       
   515   GArray *array;
       
   516 
       
   517   array = self->private_data;
       
   518   if (!array)
       
   519     return NULL;
       
   520 
       
   521   if (!private_key->index)
       
   522     return NULL;
       
   523   else if (private_key->index <= array->len)
       
   524     return g_array_index (array, GStaticPrivateNode, 
       
   525 			  private_key->index - 1).data;
       
   526   else
       
   527     return NULL;
       
   528 }
       
   529 
       
   530 #if EMULATOR
       
   531 
       
   532 PLS(next_index ,g_static_private_set,guint)
       
   533 #define next_index  (*FUNCTION_NAME(next_index ,g_static_private_set)())
       
   534 
       
   535 #endif /* EMULATOR */
       
   536 
       
   537 EXPORT_C void
       
   538 g_static_private_set (GStaticPrivate *private_key, 
       
   539 		      gpointer        data,
       
   540 		      GDestroyNotify  notify)
       
   541 {
       
   542   GRealThread *self = (GRealThread*) g_thread_self ();
       
   543   GArray *array;
       
   544   #if !(EMULATOR)
       
   545   static guint next_index = 0;
       
   546   #endif /* EMULATOR */
       
   547   
       
   548   GStaticPrivateNode *node;
       
   549 
       
   550   array = self->private_data;
       
   551   if (!array)
       
   552     {
       
   553 #ifndef __SYMBIAN32__      
       
   554       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
       
   555 #else
       
   556       array = _g_array_newP (FALSE, TRUE, sizeof (GStaticPrivateNode));
       
   557 #endif // __SYMBIAN32__      
       
   558       self->private_data = array;
       
   559     }
       
   560 
       
   561   if (!private_key->index)
       
   562     {
       
   563       G_LOCK (g_thread);
       
   564 
       
   565       if (!private_key->index)
       
   566 	{
       
   567 	  if (g_thread_free_indeces)
       
   568 	    {
       
   569 	      private_key->index = 
       
   570 		GPOINTER_TO_UINT (g_thread_free_indeces->data);
       
   571 	      g_thread_free_indeces = 
       
   572 		g_slist_delete_link (g_thread_free_indeces,
       
   573 				     g_thread_free_indeces);
       
   574 	    }
       
   575 	  else
       
   576 	    private_key->index = ++next_index;
       
   577 	}
       
   578 
       
   579       G_UNLOCK (g_thread);
       
   580     }
       
   581 
       
   582   if (private_key->index > array->len)
       
   583 #ifndef __SYMBIAN32__    
       
   584     g_array_set_size (array, private_key->index);
       
   585 #else // __SYMBIAN32__
       
   586     _g_array_set_sizeP (array, private_key->index);
       
   587 #endif // !__SYMBIAN32__    
       
   588 
       
   589   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
       
   590   if (node->destroy)
       
   591     {
       
   592       gpointer ddata = node->data;
       
   593       GDestroyNotify ddestroy = node->destroy;
       
   594 
       
   595       node->data = data;
       
   596       node->destroy = notify;
       
   597 
       
   598       ddestroy (ddata);
       
   599     }
       
   600   else
       
   601     {
       
   602       node->data = data;
       
   603       node->destroy = notify;
       
   604     }
       
   605 }
       
   606 
       
   607 #if EMULATOR
       
   608 #undef next_index
       
   609 #endif /* EMULATOR */
       
   610 
       
   611 EXPORT_C void     
       
   612 g_static_private_free (GStaticPrivate *private_key)
       
   613 {
       
   614   guint index = private_key->index;
       
   615   GRealThread *thread;
       
   616 
       
   617   if (!index)
       
   618     return;
       
   619   
       
   620   private_key->index = 0;
       
   621 
       
   622   G_LOCK (g_thread);
       
   623   
       
   624   thread = g_thread_all_threads;
       
   625   while (thread)
       
   626     {
       
   627       GArray *array = thread->private_data;
       
   628       thread = thread->next;
       
   629 
       
   630       if (array && index <= array->len)
       
   631 	{
       
   632 	  GStaticPrivateNode *node = &g_array_index (array, 
       
   633 						     GStaticPrivateNode, 
       
   634 						     index - 1);
       
   635 	  gpointer ddata = node->data;
       
   636 	  GDestroyNotify ddestroy = node->destroy;
       
   637 
       
   638 	  node->data = NULL;
       
   639 	  node->destroy = NULL;
       
   640 
       
   641 	  if (ddestroy) 
       
   642 	    {
       
   643 	      G_UNLOCK (g_thread);
       
   644 	      ddestroy (ddata);
       
   645 	      G_LOCK (g_thread);
       
   646 	      }
       
   647 	}
       
   648     }
       
   649   g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces, 
       
   650 					   GUINT_TO_POINTER (index));
       
   651   G_UNLOCK (g_thread);
       
   652 }
       
   653 
       
   654 static void
       
   655 g_thread_cleanup (gpointer data)
       
   656 {
       
   657   if (data)
       
   658     {
       
   659       GRealThread* thread = data;
       
   660       if (thread->private_data)
       
   661 	{
       
   662 	  GArray* array = thread->private_data;
       
   663 	  guint i;
       
   664 	  
       
   665 	  for (i = 0; i < array->len; i++ )
       
   666 	    {
       
   667 	      GStaticPrivateNode *node = 
       
   668 		&g_array_index (array, GStaticPrivateNode, i);
       
   669 	      if (node->destroy)
       
   670 		node->destroy (node->data);
       
   671 	    }
       
   672 #ifndef __SYMBIAN32__
       
   673 	  g_array_free (array, TRUE);
       
   674 #else // __SYMBIAN32__
       
   675 	  _g_array_freeP(array,TRUE);
       
   676 #endif // !__SYMBIAN32__	  	  
       
   677 	}
       
   678 
       
   679       /* We only free the thread structure, if it isn't joinable. If
       
   680          it is, the structure is freed in g_thread_join */
       
   681       if (!thread->thread.joinable)
       
   682 	{
       
   683 	  GRealThread *t, *p;
       
   684 
       
   685 	  G_LOCK (g_thread);
       
   686 	  for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
       
   687 	    {
       
   688 	      if (t == thread)
       
   689 		{
       
   690 		  if (p)
       
   691 		    p->next = t->next;
       
   692 		  else
       
   693 		    g_thread_all_threads = t->next;
       
   694 		  break;
       
   695 		}
       
   696 	    }
       
   697 	  G_UNLOCK (g_thread);
       
   698 	  
       
   699 	  /* Just to make sure, this isn't used any more */
       
   700 	  g_system_thread_assign (thread->system_thread, zero_thread);
       
   701 #ifndef __SYMBIAN32__
       
   702 	  g_free (thread);
       
   703 #else // __SYMBIAN32__
       
   704 	  pFree(thread);
       
   705 #endif // !__SYMBIAN32__	  	  
       
   706 	}
       
   707     }
       
   708 }
       
   709 
       
   710 static void
       
   711 g_thread_fail (void)
       
   712 {
       
   713   g_error ("The thread system is not yet initialized.");
       
   714 }
       
   715 
       
   716 static gpointer
       
   717 g_thread_create_proxy (gpointer data)
       
   718 {
       
   719   GRealThread* thread = data;
       
   720 
       
   721   g_assert (data);
       
   722 
       
   723   /* This has to happen before G_LOCK, as that might call g_thread_self */
       
   724   g_private_set (g_thread_specific_private, data);
       
   725 
       
   726   /* the lock makes sure, that thread->system_thread is written,
       
   727      before thread->thread.func is called. See g_thread_create. */
       
   728   G_LOCK (g_thread);
       
   729   G_UNLOCK (g_thread);
       
   730  
       
   731   thread->retval = thread->thread.func (thread->thread.data);
       
   732 
       
   733   return NULL;
       
   734 }
       
   735 
       
   736 EXPORT_C GThread* 
       
   737 g_thread_create_full (GThreadFunc 		 func,
       
   738 		      gpointer 		 data,
       
   739 		      gulong 		 stack_size,
       
   740 		      gboolean 		 joinable,
       
   741 		      gboolean 		 bound,
       
   742 		      GThreadPriority 	 priority,
       
   743 		      GError                **error)
       
   744 {
       
   745   GRealThread* result;
       
   746   GError *local_error = NULL;
       
   747   g_return_val_if_fail (func, NULL);
       
   748   g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
       
   749   g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
       
   750 #ifndef __SYMBIAN32__
       
   751   result = g_new0 (GRealThread, 1);
       
   752 #else // __SYMBIAN32__
       
   753   result = (GRealThread*)pAlloc(sizeof(GRealThread));
       
   754 #endif // !__SYMBIAN32__  
       
   755   
       
   756 
       
   757   result->thread.joinable = joinable;
       
   758   result->thread.priority = priority;
       
   759   result->thread.func = func;
       
   760   result->thread.data = data;
       
   761   result->private_data = NULL; 
       
   762   G_LOCK (g_thread);
       
   763   G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 
       
   764 			       stack_size, joinable, bound, priority,
       
   765 			       &result->system_thread, &local_error));
       
   766   result->next = g_thread_all_threads;
       
   767   g_thread_all_threads = result;
       
   768   G_UNLOCK (g_thread);
       
   769 
       
   770   if (local_error)
       
   771     {
       
   772       g_propagate_error (error, local_error);
       
   773       g_free (result);
       
   774       return NULL;
       
   775     }
       
   776 
       
   777   return (GThread*) result;
       
   778 }
       
   779 
       
   780 EXPORT_C void
       
   781 g_thread_exit (gpointer retval)
       
   782 {
       
   783   GRealThread* real = (GRealThread*) g_thread_self ();
       
   784   real->retval = retval;
       
   785   G_THREAD_CF (thread_exit, (void)0, ());
       
   786 }
       
   787 
       
   788 EXPORT_C gpointer
       
   789 g_thread_join (GThread* thread)
       
   790 {
       
   791   GRealThread* real = (GRealThread*) thread;
       
   792   GRealThread *p, *t;
       
   793   gpointer retval;
       
   794 
       
   795   g_return_val_if_fail (thread, NULL);
       
   796   g_return_val_if_fail (thread->joinable, NULL);
       
   797   g_return_val_if_fail (!g_system_thread_equal (real->system_thread, 
       
   798 						zero_thread), NULL);
       
   799 
       
   800   G_THREAD_UF (thread_join, (&real->system_thread));
       
   801 
       
   802   retval = real->retval;
       
   803 
       
   804   G_LOCK (g_thread);
       
   805   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
       
   806     {
       
   807       if (t == (GRealThread*) thread)
       
   808 	{
       
   809 	  if (p)
       
   810 	    p->next = t->next;
       
   811 	  else
       
   812 	    g_thread_all_threads = t->next;
       
   813 	  break;
       
   814 	}
       
   815     }
       
   816   G_UNLOCK (g_thread);
       
   817 
       
   818   /* Just to make sure, this isn't used any more */
       
   819   thread->joinable = 0;
       
   820   g_system_thread_assign (real->system_thread, zero_thread);
       
   821 
       
   822   /* the thread structure for non-joinable threads is freed upon
       
   823      thread end. We free the memory here. This will leave a loose end,
       
   824      if a joinable thread is not joined. */
       
   825 
       
   826 #ifndef __SYMBIAN32__
       
   827   g_free (thread);
       
   828 #else // __SYMBIAN32__
       
   829   pFree(thread);
       
   830 #endif // !__SYMBIAN32__  
       
   831 
       
   832   return retval;
       
   833 }
       
   834 
       
   835 EXPORT_C void
       
   836 g_thread_set_priority (GThread* thread, 
       
   837 		       GThreadPriority priority)
       
   838 {
       
   839   GRealThread* real = (GRealThread*) thread;
       
   840 
       
   841   g_return_if_fail (thread);
       
   842   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
       
   843   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
       
   844   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
       
   845 
       
   846   thread->priority = priority;
       
   847 
       
   848   G_THREAD_CF (thread_set_priority, (void)0, 
       
   849 	       (&real->system_thread, priority));
       
   850 }
       
   851 
       
   852 EXPORT_C GThread*
       
   853 g_thread_self (void)
       
   854 {
       
   855   GRealThread* thread = g_private_get (g_thread_specific_private);
       
   856 
       
   857   if (!thread)
       
   858     {  
       
   859       /* If no thread data is available, provide and set one.  This
       
   860          can happen for the main thread and for threads, that are not
       
   861          created by GLib. */
       
   862 #ifndef __SYMBIAN32__      
       
   863       thread = g_new0 (GRealThread, 1);
       
   864 #else // __SYMBIAN32__      
       
   865       thread = (GRealThread*)pAlloc(sizeof(GRealThread));
       
   866 #endif // !__SYMBIAN32__      
       
   867       thread->thread.joinable = FALSE; /* This is a save guess */
       
   868       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
       
   869 							     just a guess */
       
   870       thread->thread.func = NULL;
       
   871       thread->thread.data = NULL;
       
   872       thread->private_data = NULL;
       
   873 
       
   874       if (g_thread_supported ())
       
   875 	G_THREAD_UF (thread_self, (&thread->system_thread));
       
   876 
       
   877       g_private_set (g_thread_specific_private, thread); 
       
   878       
       
   879       G_LOCK (g_thread);
       
   880       thread->next = g_thread_all_threads;
       
   881       g_thread_all_threads = thread;
       
   882       G_UNLOCK (g_thread);
       
   883     }
       
   884   
       
   885   return (GThread*)thread;
       
   886 }
       
   887 
       
   888 EXPORT_C void
       
   889 g_static_rw_lock_init (GStaticRWLock* lock)
       
   890 {
       
   891   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
       
   892 
       
   893   g_return_if_fail (lock);
       
   894 
       
   895   *lock = init_lock;
       
   896 }
       
   897 
       
   898 inline static void 
       
   899 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
       
   900 {
       
   901   if (!*cond)
       
   902       *cond = g_cond_new ();
       
   903   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
       
   904 }
       
   905 
       
   906 inline static void 
       
   907 g_static_rw_lock_signal (GStaticRWLock* lock)
       
   908 {
       
   909   if (lock->want_to_write && lock->write_cond)
       
   910     g_cond_signal (lock->write_cond);
       
   911   else if (lock->want_to_read && lock->read_cond)
       
   912     g_cond_broadcast (lock->read_cond);
       
   913 }
       
   914 
       
   915 EXPORT_C void 
       
   916 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
       
   917 {
       
   918   g_return_if_fail (lock);
       
   919 
       
   920   if (!g_threads_got_initialized)
       
   921     return;
       
   922 
       
   923   g_static_mutex_lock (&lock->mutex);
       
   924   lock->want_to_read++;
       
   925   while (lock->have_writer || lock->want_to_write) 
       
   926     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
       
   927   lock->want_to_read--;
       
   928   lock->read_counter++;
       
   929   g_static_mutex_unlock (&lock->mutex);
       
   930 }
       
   931 
       
   932 EXPORT_C gboolean 
       
   933 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
       
   934 {
       
   935   gboolean ret_val = FALSE;
       
   936 
       
   937   g_return_val_if_fail (lock, FALSE);
       
   938 
       
   939   if (!g_threads_got_initialized)
       
   940     return TRUE;
       
   941 
       
   942   g_static_mutex_lock (&lock->mutex);
       
   943   if (!lock->have_writer && !lock->want_to_write)
       
   944     {
       
   945       lock->read_counter++;
       
   946       ret_val = TRUE;
       
   947     }
       
   948   g_static_mutex_unlock (&lock->mutex);
       
   949   return ret_val;
       
   950 }
       
   951 
       
   952 EXPORT_C void 
       
   953 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
       
   954 {
       
   955   g_return_if_fail (lock);
       
   956 
       
   957   if (!g_threads_got_initialized)
       
   958     return;
       
   959 
       
   960   g_static_mutex_lock (&lock->mutex);
       
   961   lock->read_counter--;
       
   962   if (lock->read_counter == 0)
       
   963     g_static_rw_lock_signal (lock);
       
   964   g_static_mutex_unlock (&lock->mutex);
       
   965 }
       
   966 
       
   967 EXPORT_C void 
       
   968 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
       
   969 {
       
   970   g_return_if_fail (lock);
       
   971 
       
   972   if (!g_threads_got_initialized)
       
   973     return;
       
   974 
       
   975   g_static_mutex_lock (&lock->mutex);
       
   976   lock->want_to_write++;
       
   977   while (lock->have_writer || lock->read_counter)
       
   978     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
       
   979   lock->want_to_write--;
       
   980   lock->have_writer = TRUE;
       
   981   g_static_mutex_unlock (&lock->mutex);
       
   982 }
       
   983 
       
   984 EXPORT_C gboolean 
       
   985 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
       
   986 {
       
   987   gboolean ret_val = FALSE;
       
   988 
       
   989   g_return_val_if_fail (lock, FALSE);
       
   990   
       
   991   if (!g_threads_got_initialized)
       
   992     return TRUE;
       
   993 
       
   994   g_static_mutex_lock (&lock->mutex);
       
   995   if (!lock->have_writer && !lock->read_counter)
       
   996     {
       
   997       lock->have_writer = TRUE;
       
   998       ret_val = TRUE;
       
   999     }
       
  1000   g_static_mutex_unlock (&lock->mutex);
       
  1001   return ret_val;
       
  1002 }
       
  1003 
       
  1004 EXPORT_C void 
       
  1005 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
       
  1006 {
       
  1007   g_return_if_fail (lock);
       
  1008   
       
  1009   if (!g_threads_got_initialized)
       
  1010     return;
       
  1011 
       
  1012   g_static_mutex_lock (&lock->mutex);
       
  1013   lock->have_writer = FALSE; 
       
  1014   g_static_rw_lock_signal (lock);
       
  1015   g_static_mutex_unlock (&lock->mutex);
       
  1016 }
       
  1017 
       
  1018 EXPORT_C void 
       
  1019 g_static_rw_lock_free (GStaticRWLock* lock)
       
  1020 {
       
  1021   g_return_if_fail (lock);
       
  1022   
       
  1023   if (lock->read_cond)
       
  1024     {
       
  1025       g_cond_free (lock->read_cond);
       
  1026       lock->read_cond = NULL;
       
  1027     }
       
  1028   if (lock->write_cond)
       
  1029     {
       
  1030       g_cond_free (lock->write_cond);
       
  1031       lock->write_cond = NULL;
       
  1032     }
       
  1033   g_static_mutex_free (&lock->mutex);
       
  1034 }
       
  1035 
       
  1036 /**
       
  1037  * g_thread_foreach
       
  1038  * @thread_func: function to call for all GThread structures
       
  1039  * @user_data:   second argument to @thread_func
       
  1040  *
       
  1041  * Call @thread_func on all existing #GThread structures. Note that
       
  1042  * threads may decide to exit while @thread_func is running, so
       
  1043  * without intimate knowledge about the lifetime of foreign threads,
       
  1044  * @thread_func shouldn't access the GThread* pointer passed in as
       
  1045  * first argument. However, @thread_func will not be called for threads
       
  1046  * which are known to have exited already.
       
  1047  *
       
  1048  * Due to thread lifetime checks, this function has an execution complexity
       
  1049  * which is quadratic in the number of existing threads.
       
  1050  *
       
  1051  * Since: 2.10
       
  1052  */
       
  1053 EXPORT_C void
       
  1054 g_thread_foreach (GFunc    thread_func,
       
  1055                   gpointer user_data)
       
  1056 {
       
  1057   GSList *slist = NULL;
       
  1058   GRealThread *thread;
       
  1059   g_return_if_fail (thread_func != NULL);
       
  1060   /* snapshot the list of threads for iteration */
       
  1061   G_LOCK (g_thread);
       
  1062   for (thread = g_thread_all_threads; thread; thread = thread->next)
       
  1063     slist = g_slist_prepend (slist, thread);
       
  1064   G_UNLOCK (g_thread);
       
  1065   /* walk the list, skipping non-existant threads */
       
  1066   while (slist)
       
  1067     {
       
  1068       GSList *node = slist;
       
  1069       slist = node->next;
       
  1070       /* check whether the current thread still exists */
       
  1071       G_LOCK (g_thread);
       
  1072       for (thread = g_thread_all_threads; thread; thread = thread->next)
       
  1073         if (thread == node->data)
       
  1074           break;
       
  1075       G_UNLOCK (g_thread);
       
  1076       if (thread)
       
  1077         thread_func (thread, user_data);
       
  1078       g_slist_free_1 (node);
       
  1079     }
       
  1080 }
       
  1081 
       
  1082 #define __G_THREAD_C__
       
  1083 #include "galiasdef.c"