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