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