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