Some rewording.
[glib.git] / gthread / gthread-impl.c
blob901c3b430efa6224c63e8ac6bc1ffef6801da3f6
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/.
30 /*
31 * MT safe
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <glib.h>
39 #include <gthreadinit.h>
41 #ifdef G_THREADS_ENABLED
43 static gboolean thread_system_already_initialized = FALSE;
44 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
46 #include G_THREAD_SOURCE
48 #ifndef PRIORITY_LOW_VALUE
49 # define PRIORITY_LOW_VALUE 0
50 #endif
52 #ifndef PRIORITY_URGENT_VALUE
53 # define PRIORITY_URGENT_VALUE 0
54 #endif
56 #ifndef PRIORITY_NORMAL_VALUE
57 # define PRIORITY_NORMAL_VALUE \
58 ((PRIORITY_LOW_VALUE * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
59 #endif /* PRIORITY_NORMAL_VALUE */
61 #ifndef PRIORITY_HIGH_VALUE
62 # define PRIORITY_HIGH_VALUE \
63 ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
64 #endif /* PRIORITY_HIGH_VALUE */
66 void g_mutex_init (void);
67 void g_mem_init (void);
68 void g_messages_init (void);
69 void g_convert_init (void);
70 void g_rand_init (void);
71 void g_main_thread_init (void);
73 #define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
75 typedef struct _ErrorCheckInfo ErrorCheckInfo;
76 struct _ErrorCheckInfo
78 gchar *location;
79 GThread *owner;
82 static GMutex *
83 g_mutex_new_errorcheck_impl (void)
85 GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
86 retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
87 G_MUTEX_DEBUG_INFO (retval) = NULL;
88 return retval;
91 static void
92 g_mutex_lock_errorcheck_impl (GMutex *mutex,
93 gulong magic,
94 gchar *location)
96 ErrorCheckInfo *info;
97 GThread *self = g_thread_self ();
99 if (magic != G_MUTEX_DEBUG_MAGIC)
100 location = "unknown";
102 if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
104 /* if the debug info is NULL, we have not yet locked that mutex,
105 * so we do it now */
106 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
107 /* Now we have to check again, because another thread might have
108 * tried to lock the mutex at the same time we did. This
109 * technique is not 100% save on systems without decent cache
110 * coherence, but we have no choice */
111 if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
113 info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
115 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
118 info = G_MUTEX_DEBUG_INFO (mutex);
119 if (info->owner == self)
120 g_error ("Trying to recursivly lock a mutex at '%s', "
121 "previously locked at '%s'",
122 location, info->location);
124 g_thread_functions_for_glib_use_default.mutex_lock (mutex);
126 info->owner = self;
127 info->location = location;
130 static gboolean
131 g_mutex_trylock_errorcheck_impl (GMutex *mutex,
132 gulong magic,
133 gchar *location)
135 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
136 GThread *self = g_thread_self ();
138 if (magic != G_MUTEX_DEBUG_MAGIC)
139 location = "unknown";
141 if (!info)
143 /* This mutex has not yet been used, so simply lock and return TRUE */
144 g_mutex_lock_errorcheck_impl (mutex, magic, location);
145 return TRUE;
148 if (info->owner == self)
149 g_error ("Trying to recursivly lock a mutex at '%s', "
150 "previously locked at '%s'",
151 location, info->location);
153 if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
154 return FALSE;
156 info->owner = self;
157 info->location = location;
159 return TRUE;
162 static void
163 g_mutex_unlock_errorcheck_impl (GMutex *mutex,
164 gulong magic,
165 gchar *location)
167 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
168 GThread *self = g_thread_self ();
170 if (magic != G_MUTEX_DEBUG_MAGIC)
171 location = "unknown";
173 if (!info || info->owner == NULL)
174 g_error ("Trying to unlock an unlocked mutex at '%s'", location);
176 if (info->owner != self)
177 g_warning ("Trying to unlock a mutex at '%s', "
178 "previously locked by a different thread at '%s'",
179 location, info->location);
181 info->owner = NULL;
182 info->location = NULL;
184 g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
187 static void
188 g_mutex_free_errorcheck_impl (GMutex *mutex,
189 gulong magic,
190 gchar *location)
192 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
194 if (magic != G_MUTEX_DEBUG_MAGIC)
195 location = "unknown";
197 if (info && info->owner != NULL)
198 g_error ("Trying to free a locked mutex at '%s', "
199 "which was previously locked at '%s'",
200 location, info->location);
202 g_free (G_MUTEX_DEBUG_INFO (mutex));
203 g_thread_functions_for_glib_use_default.mutex_free (mutex);
206 static void
207 g_cond_wait_errorcheck_impl (GCond *cond,
208 GMutex *mutex,
209 gulong magic,
210 gchar *location)
213 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
214 GThread *self = g_thread_self ();
216 if (magic != G_MUTEX_DEBUG_MAGIC)
217 location = "unknown";
219 if (!info || info->owner == NULL)
220 g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
221 location);
223 if (info->owner != self)
224 g_error ("Trying to use a mutex locked by another thread in "
225 "g_cond_wait() at '%s'", location);
227 info->owner = NULL;
228 location = info->location;
230 g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
232 info->owner = self;
233 info->location = location;
237 static gboolean
238 g_cond_timed_wait_errorcheck_impl (GCond *cond,
239 GMutex *mutex,
240 GTimeVal *end_time,
241 gulong magic,
242 gchar *location)
244 ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
245 GThread *self = g_thread_self ();
246 gboolean retval;
248 if (magic != G_MUTEX_DEBUG_MAGIC)
249 location = "unknown";
251 if (!info || info->owner == NULL)
252 g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
253 location);
255 if (info->owner != self)
256 g_error ("Trying to use a mutex locked by another thread in "
257 "g_cond_timed_wait() at '%s'", location);
259 info->owner = NULL;
260 location = info->location;
262 retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
263 mutex,
264 end_time);
266 info->owner = self;
267 info->location = location;
269 return retval;
273 /* unshadow function declaration. See gthread.h */
274 #undef g_thread_init
276 void
277 g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
279 GThreadFunctions errorcheck_functions;
280 if (init)
281 g_error ("Errorcheck mutexes can only be used for native "
282 "thread implementations. Sorry." );
284 #ifdef HAVE_G_THREAD_IMPL_INIT
285 /* This isn't called in g_thread_init, as it doesn't think to get
286 * the default implementation, so we have to call it on our own.
288 * We must call this before copying
289 * g_thread_functions_for_glib_use_default as the
290 * implementation-specific init function might modify the contents
291 * of g_thread_functions_for_glib_use_default based on operating
292 * system version, C library version, or whatever. */
293 g_thread_impl_init();
294 #endif /* HAVE_G_THREAD_IMPL_INIT */
296 errorcheck_functions = g_thread_functions_for_glib_use_default;
297 errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
298 errorcheck_functions.mutex_lock =
299 (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
300 errorcheck_functions.mutex_trylock =
301 (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
302 errorcheck_functions.mutex_unlock =
303 (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
304 errorcheck_functions.mutex_free =
305 (void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
306 errorcheck_functions.cond_wait =
307 (void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
308 errorcheck_functions.cond_timed_wait =
309 (gboolean (*)(GCond *, GMutex *, GTimeVal *))
310 g_cond_timed_wait_errorcheck_impl;
312 g_thread_init (&errorcheck_functions);
315 void
316 g_thread_init (GThreadFunctions* init)
318 gboolean supported;
320 if (thread_system_already_initialized)
321 g_error ("GThread system may only be initialized once.");
323 thread_system_already_initialized = TRUE;
325 if (init == NULL)
327 #ifdef HAVE_G_THREAD_IMPL_INIT
328 /* now do any initialization stuff required by the
329 * implementation, but only if called with a NULL argument, of
330 * course. Otherwise it's up to the user to do so. */
331 g_thread_impl_init();
332 #endif /* HAVE_G_THREAD_IMPL_INIT */
333 init = &g_thread_functions_for_glib_use_default;
335 else
336 g_thread_use_default_impl = FALSE;
338 g_thread_functions_for_glib_use = *init;
340 supported = (init->mutex_new &&
341 init->mutex_lock &&
342 init->mutex_trylock &&
343 init->mutex_unlock &&
344 init->mutex_free &&
345 init->cond_new &&
346 init->cond_signal &&
347 init->cond_broadcast &&
348 init->cond_wait &&
349 init->cond_timed_wait &&
350 init->cond_free &&
351 init->private_new &&
352 init->private_get &&
353 init->private_set &&
354 init->thread_create &&
355 init->thread_yield &&
356 init->thread_join &&
357 init->thread_exit &&
358 init->thread_set_priority &&
359 init->thread_self);
361 /* if somebody is calling g_thread_init (), it means that he wants to
362 * have thread support, so check this
364 if (!supported)
366 if (g_thread_use_default_impl)
367 g_error ("Threads are not supported on this platform.");
368 else
369 g_error ("The supplied thread function vector is invalid.");
372 g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
373 g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
374 g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
375 g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
377 g_thread_init_glib ();
380 #else /* !G_THREADS_ENABLED */
382 void
383 g_thread_init (GThreadFunctions* init)
385 g_error ("GLib thread support is disabled.");
388 #endif /* !G_THREADS_ENABLED */