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