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