1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: thread related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
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.
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.
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.
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/.
40 static gboolean thread_system_already_initialized = FALSE;
41 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT];
43 #include G_THREAD_SOURCE
45 #ifndef PRIORITY_LOW_VALUE
46 # define PRIORITY_LOW_VALUE 0
49 #ifndef PRIORITY_URGENT_VALUE
50 # define PRIORITY_URGENT_VALUE 0
53 #ifndef PRIORITY_NORMAL_VALUE
54 # define PRIORITY_NORMAL_VALUE \
55 PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 40 / 100
56 #endif /* PRIORITY_NORMAL_VALUE */
58 #ifndef PRIORITY_HIGH_VALUE
59 # define PRIORITY_HIGH_VALUE \
60 PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 80 / 100
61 #endif /* PRIORITY_HIGH_VALUE */
63 void g_mutex_init (void);
64 void g_mem_init (void);
65 void g_messages_init (void);
67 #define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
69 typedef struct _ErrorCheckInfo ErrorCheckInfo;
70 struct _ErrorCheckInfo
77 g_mutex_new_errorcheck_impl (void)
79 GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
80 retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
81 G_MUTEX_DEBUG_INFO (retval) = NULL;
86 g_mutex_lock_errorcheck_impl (GMutex *mutex,
91 GThread *self = g_thread_self ();
93 if (magic != G_MUTEX_DEBUG_MAGIC)
96 if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
98 /* if the debug info is NULL, we have not yet locked that mutex,
100 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
101 /* Now we have to check again, because another thread might have
102 * tried to lock the mutex at the same time we did. This
103 * technique is not 100% save on systems without decent cache
104 * coherence, but we have no choice */
105 if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
107 info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
109 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
112 info = G_MUTEX_DEBUG_INFO (mutex);
113 if (info->owner == self)
114 g_error ("Trying to recursivly lock a mutex at '%s', "
115 "previously locked at '%s'",
116 location, info->location);
118 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
121 info->location = location;
125 g_mutex_trylock_errorcheck_impl (GMutex *mutex,
129 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
130 GThread *self = g_thread_self ();
132 if (magic != G_MUTEX_DEBUG_MAGIC)
133 location = "unknown";
137 /* This mutex has not yet been used, so simply lock and return TRUE */
138 g_mutex_lock_errorcheck_impl (mutex, magic, location);
142 if (info->owner == self)
143 g_error ("Trying to recursivly lock a mutex at '%s', "
144 "previously locked at '%s'",
145 location, info->location);
147 if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
151 info->location = location;
157 g_mutex_unlock_errorcheck_impl (GMutex *mutex,
161 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
162 GThread *self = g_thread_self ();
164 if (magic != G_MUTEX_DEBUG_MAGIC)
165 location = "unknown";
167 if (!info || info->owner == NULL)
168 g_error ("Trying to unlock an unlocked mutex at '%s'", location);
170 if (info->owner != self)
171 g_warning ("Trying to unlock a mutex at '%s', "
172 "previously locked by a different thread at '%s'",
173 location, info->location);
176 info->location = NULL;
178 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
182 g_mutex_free_errorcheck_impl (GMutex *mutex,
186 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
188 if (magic != G_MUTEX_DEBUG_MAGIC)
189 location = "unknown";
191 if (info && info->owner != NULL)
192 g_error ("Trying to free a locked mutex at '%s', "
193 "which was previously locked at '%s'",
194 location, info->location);
196 g_free (G_MUTEX_DEBUG_INFO (mutex));
197 g_thread_functions_for_glib_use_default.mutex_free (mutex);
201 g_cond_wait_errorcheck_impl (GCond *cond,
207 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
208 GThread *self = g_thread_self ();
210 if (magic != G_MUTEX_DEBUG_MAGIC)
211 location = "unknown";
213 if (!info || info->owner == NULL)
214 g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
217 if (info->owner != self)
218 g_error ("Trying to use a mutex locked by another thread in "
219 "g_cond_wait() at '%s'", location);
222 location = info->location;
224 g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
227 info->location = location;
232 g_cond_timed_wait_errorcheck_impl (GCond *cond,
238 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
239 GThread *self = g_thread_self ();
242 if (magic != G_MUTEX_DEBUG_MAGIC)
243 location = "unknown";
245 if (!info || info->owner == NULL)
246 g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
249 if (info->owner != self)
250 g_error ("Trying to use a mutex locked by another thread in "
251 "g_cond_timed_wait() at '%s'", location);
254 location = info->location;
256 retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
261 info->location = location;
267 /* unshadow function declaration. See gthread.h */
271 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
273 GThreadFunctions errorcheck_functions;
275 g_error ("Errorcheck mutexes can only be used for native "
276 "thread implementations. Sorry." );
277 errorcheck_functions = g_thread_functions_for_glib_use_default;
278 errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
279 errorcheck_functions.mutex_lock =
280 (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
281 errorcheck_functions.mutex_trylock =
282 (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
283 errorcheck_functions.mutex_unlock =
284 (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
285 errorcheck_functions.mutex_free =
286 (void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
287 errorcheck_functions.cond_wait =
288 (void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
289 errorcheck_functions.cond_timed_wait =
290 (gboolean (*)(GCond *, GMutex *, GTimeVal *))
291 g_cond_timed_wait_errorcheck_impl;
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 g_thread_impl_init();
299 g_thread_init (&errorcheck_functions);
303 g_thread_init (GThreadFunctions* init)
307 #ifndef G_THREADS_ENABLED
308 g_error ("GLib thread support is disabled.");
309 #endif /* !G_THREADS_ENABLED */
311 if (thread_system_already_initialized)
312 g_error ("GThread system may only be initialized once.");
314 thread_system_already_initialized = TRUE;
317 init = &g_thread_functions_for_glib_use_default;
319 g_thread_use_default_impl = FALSE;
321 #if defined (G_PLATFORM_WIN32) && defined (__GNUC__)
322 memcpy(&g_thread_functions_for_glib_use, init, sizeof (*init));
324 g_thread_functions_for_glib_use = *init;
326 /* It is important, that g_threads_got_initialized is not set before the
327 * thread initialization functions of the different modules are called
330 supported = (init->mutex_new &&
332 init->mutex_trylock &&
333 init->mutex_unlock &&
337 init->cond_broadcast &&
339 init->cond_timed_wait &&
345 /* if somebody is calling g_thread_init (), it means that he wants to
346 * have thread support, so check this
350 if (g_thread_use_default_impl)
351 g_error ("Threads are not supported on this platform.");
353 g_error ("The supplied thread function vector is invalid.");
356 /* now do any initialization stuff required by the implementation,
357 * but only if called with a NULL argument, of course. Otherwise
358 * it's up to the user to do so. */
360 #ifdef HAVE_G_THREAD_IMPL_INIT
361 if (g_thread_use_default_impl)
362 g_thread_impl_init();
365 g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
366 g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
367 g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
368 g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
370 /* now call the thread initialization functions of the different
371 * glib modules. order does matter, g_mutex_init MUST come first.
377 /* now we can set g_threads_got_initialized and thus enable
378 * all the thread functions
380 g_threads_got_initialized = TRUE;
382 /* we want the main thread to run with normal priority */
383 g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);