Broadcast the condition, if there are waiting readers, as all might read
[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 static gboolean thread_system_already_initialized = FALSE;
41 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT];
42
43 #include G_THREAD_SOURCE
44
45 #ifndef PRIORITY_LOW_VALUE
46 # define PRIORITY_LOW_VALUE 0
47 #endif
48
49 #ifndef PRIORITY_URGENT_VALUE
50 # define PRIORITY_URGENT_VALUE 0
51 #endif
52
53 #ifndef PRIORITY_NORMAL_VALUE
54 # define PRIORITY_NORMAL_VALUE                                          \
55   PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 40 / 100 
56 #endif /* PRIORITY_NORMAL_VALUE */
57
58 #ifndef PRIORITY_HIGH_VALUE
59 # define PRIORITY_HIGH_VALUE                                            \
60   PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 80 / 100 
61 #endif /* PRIORITY_HIGH_VALUE */
62
63 void g_mutex_init (void);
64 void g_mem_init (void);
65 void g_messages_init (void);
66
67 #define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
68
69 typedef struct _ErrorCheckInfo ErrorCheckInfo;
70 struct _ErrorCheckInfo
71 {
72   gchar *location;
73   GThread *owner;
74 };
75
76 static GMutex *
77 g_mutex_new_errorcheck_impl (void)
78 {
79   GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
80   retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
81   G_MUTEX_DEBUG_INFO (retval) = NULL;
82   return retval;
83 }
84
85 static void
86 g_mutex_lock_errorcheck_impl (GMutex *mutex, 
87                               gulong magic,
88                               gchar *location)
89 {
90   ErrorCheckInfo *info;
91   GThread *self = g_thread_self ();
92
93   if (magic != G_MUTEX_DEBUG_MAGIC)
94     location = "unknown";
95
96   if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
97     {
98       /* if the debug info is NULL, we have not yet locked that mutex,
99        * so we do it now */
100       g_thread_functions_for_glib_use_default.mutex_lock (mutex);
101       /* Now we have to check again, because another thread might have
102        * tried to lock the mutex at the same time we did. This
103        * technique is not 100% save on systems without decent cache
104        * coherence, but we have no choice */
105       if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
106         {
107           info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
108         }
109       g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
110     }
111   
112   info = G_MUTEX_DEBUG_INFO (mutex);
113   if (info->owner == self)
114     g_error ("Trying to recursivly lock a mutex at '%s', "
115              "previously locked at '%s'", 
116              location, info->location);
117
118   g_thread_functions_for_glib_use_default.mutex_lock (mutex);
119
120   info->owner = self;
121   info->location = location;
122 }
123
124 static gboolean
125 g_mutex_trylock_errorcheck_impl (GMutex *mutex, 
126                                  gulong magic, 
127                                  gchar *location)
128 {
129   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
130   GThread *self = g_thread_self ();
131
132   if (magic != G_MUTEX_DEBUG_MAGIC)
133     location = "unknown";
134
135   if (!info)
136     {
137       /* This mutex has not yet been used, so simply lock and return TRUE */
138       g_mutex_lock_errorcheck_impl (mutex, magic, location);
139       return TRUE;
140     }
141
142   if (info->owner == self)
143     g_error ("Trying to recursivly lock a mutex at '%s', "
144              "previously locked at '%s'", 
145              location, info->location);
146   
147   if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
148     return FALSE;
149
150   info->owner = self;
151   info->location = location;
152
153   return TRUE;
154 }
155
156 static void
157 g_mutex_unlock_errorcheck_impl (GMutex *mutex, 
158                                 gulong magic, 
159                                 gchar *location)
160 {
161   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
162   GThread *self = g_thread_self ();
163
164   if (magic != G_MUTEX_DEBUG_MAGIC)
165     location = "unknown";
166
167   if (!info || info->owner == NULL)
168     g_error ("Trying to unlock an unlocked mutex at '%s'", location);
169
170   if (info->owner != self)
171     g_warning ("Trying to unlock a mutex at '%s', "
172                "previously locked by a different thread at '%s'",
173                location, info->location);
174
175   info->owner = NULL;
176   info->location = NULL;
177
178   g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
179 }
180
181 static void
182 g_mutex_free_errorcheck_impl (GMutex *mutex)
183 {
184   if (info && info->owner != NULL)
185     g_error ("Trying to free a locked mutex at '%s', "
186              "which was previously locked at '%s'", 
187              location, info->location);
188
189   g_free (G_MUTEX_DEBUG_INFO (mutex));
190   g_thread_functions_for_glib_use_default.mutex_free (mutex);  
191 }
192
193 static void     
194 g_cond_wait_errorcheck_impl (GCond *cond,
195                              GMutex *mutex, 
196                              gulong magic, 
197                              gchar *location)
198 {
199   
200   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
201   GThread *self = g_thread_self ();
202
203   if (magic != G_MUTEX_DEBUG_MAGIC)
204     location = "unknown";
205
206   if (!info || info->owner == NULL)
207     g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
208              location);
209
210   if (info->owner != self)
211     g_error ("Trying to use a mutex locked by another thread in "
212              "g_cond_wait() at '%s'", location);
213
214   info->owner = NULL;
215   location = info->location;
216
217   g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
218
219   info->owner = self;
220   info->location = location;
221 }
222     
223
224 static gboolean 
225 g_cond_timed_wait_errorcheck_impl (GCond *cond,
226                                    GMutex *mutex,
227                                    GTimeVal *end_time, 
228                                    gulong magic, 
229                                    gchar *location)
230 {
231   ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
232   GThread *self = g_thread_self ();
233   gboolean retval;
234
235   if (magic != G_MUTEX_DEBUG_MAGIC)
236     location = "unknown";
237
238   if (!info || info->owner == NULL)
239     g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
240              location);
241
242   if (info->owner != self)
243     g_error ("Trying to use a mutex locked by another thread in "
244              "g_cond_timed_wait() at '%s'", location);
245
246   info->owner = NULL;
247   location = info->location;
248   
249   retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond, 
250                                                                     mutex, 
251                                                                     end_time);
252
253   info->owner = self;
254   info->location = location;
255
256   return retval;
257 }
258
259
260 /* unshadow function declaration. See glib.h */
261 #undef g_thread_init
262
263 void 
264 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
265 {
266   GThreadFunctions errorcheck_functions;
267   if (init)
268     g_error ("Errorcheck mutexes can only be used for native " 
269              "thread implementations. Sorry." );
270   errorcheck_functions = g_thread_functions_for_glib_use_default;
271   errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
272   errorcheck_functions.mutex_lock = 
273     (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
274   errorcheck_functions.mutex_trylock = 
275     (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
276   errorcheck_functions.mutex_unlock = 
277     (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
278   errorcheck_functions.mutex_free = g_mutex_free_errorcheck_impl;
279   errorcheck_functions.cond_wait = g_cond_wait_errorcheck_impl;
280   errorcheck_functions.cond_timed_wait = g_cond_timed_wait_errorcheck_impl;
281     
282   g_thread_init (&errorcheck_functions);
283 }
284
285 void
286 g_thread_init (GThreadFunctions* init)
287 {
288   gboolean supported;
289
290 #ifndef G_THREADS_ENABLED
291   g_error ("GLib thread support is disabled.");
292 #endif  /* !G_THREADS_ENABLED */
293
294   if (thread_system_already_initialized)
295     g_error ("GThread system may only be initialized once.");
296     
297   thread_system_already_initialized = TRUE;
298
299   if (init == NULL)
300     init = &g_thread_functions_for_glib_use_default;
301   else
302     g_thread_use_default_impl = FALSE;
303
304 #if defined (G_OS_WIN32) && defined (__GNUC__)
305   memcpy(&g_thread_functions_for_glib_use, init, sizeof (*init));
306 #else
307   g_thread_functions_for_glib_use = *init;
308 #endif
309   /* It is important, that g_threads_got_initialized is not set before the
310    * thread initialization functions of the different modules are called
311    */
312
313   supported = (init->mutex_new &&  
314                init->mutex_lock && 
315                init->mutex_trylock && 
316                init->mutex_unlock && 
317                init->mutex_free && 
318                init->cond_new && 
319                init->cond_signal && 
320                init->cond_broadcast && 
321                init->cond_wait && 
322                init->cond_timed_wait &&
323                init->cond_free &&
324                init->private_new &&
325                init->private_get &&
326                init->private_get);
327
328   /* if somebody is calling g_thread_init (), it means that he wants to
329    * have thread support, so check this
330    */
331   if (!supported)
332     {
333       if (g_thread_use_default_impl)
334         g_error ("Threads are not supported on this platform.");
335       else
336         g_error ("The supplied thread function vector is invalid.");
337     }
338
339   /* now do any initialization stuff required by the implementation,
340    * but only if called with a NULL argument, of course. Otherwise
341    * it's up to the user to do so. */
342
343 #ifdef HAVE_G_THREAD_IMPL_INIT
344   if (g_thread_use_default_impl)
345     g_thread_impl_init();
346 #endif
347
348   g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
349   g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
350   g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
351   g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
352
353   /* now call the thread initialization functions of the different
354    * glib modules. order does matter, g_mutex_init MUST come first.
355    */
356   g_mutex_init ();
357   g_mem_init ();
358   g_messages_init ();
359
360   /* now we can set g_threads_got_initialized and thus enable
361    * all the thread functions
362    */
363   g_threads_got_initialized = TRUE;
364
365   /* we want the main thread to run with normal priority */
366   g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
367 }