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/.
37 #include "gthreadprivate.h"
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];
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 * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
56 #endif /* PRIORITY_NORMAL_VALUE */
58 #ifndef PRIORITY_HIGH_VALUE
59 # define PRIORITY_HIGH_VALUE \
60 ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
61 #endif /* PRIORITY_HIGH_VALUE */
63 typedef struct _GMutexDebugInfo GMutexDebugInfo;
64 struct _GMutexDebugInfo
70 #define G_MUTEX_DEBUG_INFO(mutex) \
71 (((GMutexDebugInfo*)(((char*)mutex)+G_MUTEX_SIZE)))
74 g_mutex_new_errorcheck_impl (void)
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));
80 info = G_MUTEX_DEBUG_INFO (retval);
81 g_system_thread_assign (info->owner, zero_thread);
82 info->location = "invalid";
88 g_mutex_lock_errorcheck_impl (GMutex *mutex,
90 gchar * const location)
92 GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
93 gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
96 g_thread_functions_for_glib_use.thread_self (&self);
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);
103 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
105 g_system_thread_assign (info->owner, self);
106 info->location = loc;
110 g_mutex_trylock_errorcheck_impl (GMutex *mutex,
112 gchar * const location)
114 GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
115 gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
118 g_thread_functions_for_glib_use.thread_self (&self);
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);
125 if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
128 g_system_thread_assign (info->owner, self);
129 info->location = loc;
135 g_mutex_unlock_errorcheck_impl (GMutex *mutex,
137 gchar * const location)
139 GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
140 gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
143 g_thread_functions_for_glib_use.thread_self (&self);
145 if (g_system_thread_equal (info->owner, zero_thread))
146 g_error ("Trying to unlock an unlocked mutex at '%s'", loc);
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);
153 g_system_thread_assign (info->owner, zero_thread);
154 info->location = NULL;
156 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
160 g_mutex_free_errorcheck_impl (GMutex *mutex,
162 gchar * const location)
164 GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
165 gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
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);
172 g_thread_functions_for_glib_use_default.mutex_free (mutex);
176 g_cond_wait_errorcheck_impl (GCond *cond,
179 gchar * const location)
181 GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
182 gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
185 g_thread_functions_for_glib_use.thread_self (&self);
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);
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);
194 g_system_thread_assign (info->owner, zero_thread);
195 loc = info->location;
197 g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
199 g_system_thread_assign (info->owner, self);
200 info->location = loc;
205 g_cond_timed_wait_errorcheck_impl (GCond *cond,
209 gchar * const location)
211 GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
212 gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
216 g_thread_functions_for_glib_use.thread_self (&self);
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'",
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);
226 g_system_thread_assign (info->owner, zero_thread);
227 loc = info->location;
229 retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
233 g_system_thread_assign (info->owner, self);
234 info->location = loc;
240 /* unshadow function declaration. See gthread.h */
244 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
246 GThreadFunctions errorcheck_functions;
248 g_error ("Errorcheck mutexes can only be used for native "
249 "thread implementations. Sorry." );
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.
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 */
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;
279 g_thread_init (&errorcheck_functions);
283 g_thread_init (GThreadFunctions* init)
287 if (thread_system_already_initialized)
290 g_warning ("GThread system already initialized, ignoring custom thread implementation.");
295 thread_system_already_initialized = TRUE;
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;
308 g_thread_use_default_impl = FALSE;
310 g_thread_functions_for_glib_use = *init;
312 supported = (init->mutex_new &&
314 init->mutex_trylock &&
315 init->mutex_unlock &&
319 init->cond_broadcast &&
321 init->cond_timed_wait &&
326 init->thread_create &&
327 init->thread_yield &&
330 init->thread_set_priority &&
333 /* if somebody is calling g_thread_init (), it means that he wants to
334 * have thread support, so check this
338 if (g_thread_use_default_impl)
339 g_error ("Threads are not supported on this platform.");
341 g_error ("The supplied thread function vector is invalid.");
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;
349 g_thread_init_glib ();