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