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
78 g_mutex_new_errorcheck_impl (void)
80 GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
81 retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
82 G_MUTEX_DEBUG_INFO (retval) = NULL;
87 fill_info (ErrorCheckInfo *info,
94 if (magic == G_MUTEX_DEBUG_MAGIC)
96 /* We are used with special instrumented calls, where name and
97 * location is provided as well, so use them */
99 info->location = location;
102 info->name = info->location = "unknown";
106 g_mutex_lock_errorcheck_impl (GMutex *mutex,
111 ErrorCheckInfo *info;
112 GThread *self = g_thread_self ();
113 if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
115 /* if the debug info is NULL, we have not yet locked that mutex,
117 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
118 /* Now we have to check again, because anothe thread might mave
119 * locked the mutex at the same time, we did. This technique is
120 * not 100% save on systems without decent cache coherence,
121 * but we have no choice */
122 if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
124 info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
126 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
129 info = G_MUTEX_DEBUG_INFO (mutex);
130 if (info->owner == self)
131 g_error ("Trying to recursivly lock the mutex '%s' at '%s', "
132 "previously locked by name '%s' at '%s'",
133 name, location, info->name, info->location);
135 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
137 fill_info (info, self, magic, name, location);
141 g_mutex_trylock_errorcheck_impl (GMutex *mutex,
146 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
147 GThread *self = g_thread_self ();
150 /* This mutex has not yet been used, so simply lock and return TRUE */
151 g_mutex_lock_errorcheck_impl (mutex, magic, name, location);
154 if (info->owner == self)
155 g_error ("Trying to recursivly lock the mutex '%s' at '%s', "
156 "previously locked by name '%s' at '%s'",
157 name, location, info->name, info->location);
159 if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
162 fill_info (info, self, magic, name, location);
167 g_mutex_unlock_errorcheck_impl (GMutex *mutex,
172 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
173 GThread *self = g_thread_self ();
174 if (magic != G_MUTEX_DEBUG_MAGIC)
175 name = location = "unknown";
176 if (!info || info->owner != self)
177 g_error ("Trying to unlock the unlocked mutex '%s' at '%s'",
180 info->name = info->location = NULL;
181 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
185 g_mutex_free_errorcheck_impl (GMutex *mutex)
187 g_free (G_MUTEX_DEBUG_INFO (mutex));
188 g_thread_functions_for_glib_use_default.mutex_free (mutex);
191 /* unshadow function declaration. See glib.h */
195 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
197 GThreadFunctions errorcheck_functions;
199 g_error ("Errorcheck mutexes can only be used for native "
200 "thread implementations. Sorry." );
201 errorcheck_functions = g_thread_functions_for_glib_use_default;
202 errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
203 errorcheck_functions.mutex_lock =
204 (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
205 errorcheck_functions.mutex_trylock =
206 (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
207 errorcheck_functions.mutex_unlock =
208 (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
209 errorcheck_functions.mutex_free = g_mutex_free_errorcheck_impl;
211 g_thread_init (&errorcheck_functions);
215 g_thread_init (GThreadFunctions* init)
219 #ifndef G_THREADS_ENABLED
220 g_error ("GLib thread support is disabled.");
221 #endif /* !G_THREADS_ENABLED */
223 if (thread_system_already_initialized)
224 g_error ("GThread system may only be initialized once.");
226 thread_system_already_initialized = TRUE;
229 init = &g_thread_functions_for_glib_use_default;
231 g_thread_use_default_impl = FALSE;
233 #if defined (G_OS_WIN32) && defined (__GNUC__)
234 memcpy(&g_thread_functions_for_glib_use, init, sizeof (*init));
236 g_thread_functions_for_glib_use = *init;
238 /* It is important, that g_threads_got_initialized is not set before the
239 * thread initialization functions of the different modules are called
242 supported = (init->mutex_new &&
244 init->mutex_trylock &&
245 init->mutex_unlock &&
249 init->cond_broadcast &&
251 init->cond_timed_wait &&
257 /* if somebody is calling g_thread_init (), it means that he wants to
258 * have thread support, so check this
262 if (g_thread_use_default_impl)
263 g_error ("Threads are not supported on this platform.");
265 g_error ("The supplied thread function vector is invalid.");
268 /* now do any initialization stuff required by the implementation,
269 * but only if called with a NULL argument, of course. Otherwise
270 * it's up to the user to do so. */
272 #ifdef HAVE_G_THREAD_IMPL_INIT
273 if (g_thread_use_default_impl)
274 g_thread_impl_init();
277 g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
278 g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
279 g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
280 g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
282 /* now call the thread initialization functions of the different
283 * glib modules. order does matter, g_mutex_init MUST come first.
289 /* now we can set g_threads_got_initialized and thus enable
290 * all the thread functions
292 g_threads_got_initialized = TRUE;
294 /* we want the main thread to run with normal priority */
295 g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);