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 #ifdef G_THREADS_ENABLED
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];
45 #include G_THREAD_SOURCE
47 #ifndef PRIORITY_LOW_VALUE
48 # define PRIORITY_LOW_VALUE 0
51 #ifndef PRIORITY_URGENT_VALUE
52 # define PRIORITY_URGENT_VALUE 0
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 */
60 #ifndef PRIORITY_HIGH_VALUE
61 # define PRIORITY_HIGH_VALUE \
62 ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
63 #endif /* PRIORITY_HIGH_VALUE */
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);
72 typedef struct _GMutexDebugInfo GMutexDebugInfo
;
73 struct _GMutexDebugInfo
79 #define G_MUTEX_DEBUG_INFO(mutex) \
80 (((GMutexDebugInfo*)(((char*)mutex)+G_MUTEX_SIZE)))
83 g_mutex_new_errorcheck_impl (void)
85 GMutex
*retval
= g_thread_functions_for_glib_use_default
.mutex_new ();
86 GMutexDebugInfo
*info
;
87 retval
= g_realloc (retval
, G_MUTEX_SIZE
+ sizeof (GMutexDebugInfo
));
89 info
= G_MUTEX_DEBUG_INFO (retval
);
90 g_system_thread_assign (info
->owner
, zero_thread
);
91 info
->location
= "invalid";
97 g_mutex_lock_errorcheck_impl (GMutex
*mutex
,
99 gchar
* const location
)
101 GMutexDebugInfo
*info
= G_MUTEX_DEBUG_INFO (mutex
);
102 gchar
*loc
= (magic
== G_MUTEX_DEBUG_MAGIC
) ? location
: "unknown";
105 g_thread_functions_for_glib_use
.thread_self (&self
);
107 if (g_system_thread_equal (info
->owner
, self
))
108 g_error ("Trying to recursively lock a mutex at '%s', "
109 "previously locked at '%s'",
110 loc
, info
->location
);
112 g_thread_functions_for_glib_use_default
.mutex_lock (mutex
);
114 g_system_thread_assign (info
->owner
, self
);
115 info
->location
= loc
;
119 g_mutex_trylock_errorcheck_impl (GMutex
*mutex
,
121 gchar
* const location
)
123 GMutexDebugInfo
*info
= G_MUTEX_DEBUG_INFO (mutex
);
124 gchar
*loc
= (magic
== G_MUTEX_DEBUG_MAGIC
) ? location
: "unknown";
127 g_thread_functions_for_glib_use
.thread_self (&self
);
129 if (g_system_thread_equal (info
->owner
, self
))
130 g_error ("Trying to recursivly lock a mutex at '%s', "
131 "previously locked at '%s'",
132 loc
, info
->location
);
134 if (!g_thread_functions_for_glib_use_default
.mutex_trylock (mutex
))
137 g_system_thread_assign (info
->owner
, self
);
138 info
->location
= loc
;
144 g_mutex_unlock_errorcheck_impl (GMutex
*mutex
,
146 gchar
* const location
)
148 GMutexDebugInfo
*info
= G_MUTEX_DEBUG_INFO (mutex
);
149 gchar
*loc
= (magic
== G_MUTEX_DEBUG_MAGIC
) ? location
: "unknown";
152 g_thread_functions_for_glib_use
.thread_self (&self
);
154 if (g_system_thread_equal (info
->owner
, zero_thread
))
155 g_error ("Trying to unlock an unlocked mutex at '%s'", loc
);
157 if (!g_system_thread_equal (info
->owner
, self
))
158 g_warning ("Trying to unlock a mutex at '%s', "
159 "previously locked by a different thread at '%s'",
160 loc
, info
->location
);
162 g_system_thread_assign (info
->owner
, zero_thread
);
163 info
->location
= NULL
;
165 g_thread_functions_for_glib_use_default
.mutex_unlock (mutex
);
169 g_mutex_free_errorcheck_impl (GMutex
*mutex
,
171 gchar
* const location
)
173 GMutexDebugInfo
*info
= G_MUTEX_DEBUG_INFO (mutex
);
174 gchar
*loc
= (magic
== G_MUTEX_DEBUG_MAGIC
) ? location
: "unknown";
176 if (info
&& !g_system_thread_equal (info
->owner
, zero_thread
))
177 g_error ("Trying to free a locked mutex at '%s', "
178 "which was previously locked at '%s'",
179 loc
, info
->location
);
181 g_thread_functions_for_glib_use_default
.mutex_free (mutex
);
185 g_cond_wait_errorcheck_impl (GCond
*cond
,
188 gchar
* const location
)
190 GMutexDebugInfo
*info
= G_MUTEX_DEBUG_INFO (mutex
);
191 gchar
*loc
= (magic
== G_MUTEX_DEBUG_MAGIC
) ? location
: "unknown";
194 g_thread_functions_for_glib_use
.thread_self (&self
);
196 if (g_system_thread_equal (info
->owner
, zero_thread
))
197 g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'", loc
);
199 if (!g_system_thread_equal (info
->owner
, self
))
200 g_error ("Trying to use a mutex locked by another thread in "
201 "g_cond_wait() at '%s'", loc
);
203 g_system_thread_assign (info
->owner
, zero_thread
);
204 loc
= info
->location
;
206 g_thread_functions_for_glib_use_default
.cond_wait (cond
, mutex
);
208 g_system_thread_assign (info
->owner
, self
);
209 info
->location
= loc
;
214 g_cond_timed_wait_errorcheck_impl (GCond
*cond
,
218 gchar
* const location
)
220 GMutexDebugInfo
*info
= G_MUTEX_DEBUG_INFO (mutex
);
221 gchar
*loc
= (magic
== G_MUTEX_DEBUG_MAGIC
) ? location
: "unknown";
225 g_thread_functions_for_glib_use
.thread_self (&self
);
227 if (g_system_thread_equal (info
->owner
, zero_thread
))
228 g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
231 if (!g_system_thread_equal (info
->owner
, self
))
232 g_error ("Trying to use a mutex locked by another thread in "
233 "g_cond_timed_wait() at '%s'", loc
);
235 g_system_thread_assign (info
->owner
, zero_thread
);
236 loc
= info
->location
;
238 retval
= g_thread_functions_for_glib_use_default
.cond_timed_wait (cond
,
242 g_system_thread_assign (info
->owner
, self
);
243 info
->location
= loc
;
249 /* unshadow function declaration. See gthread.h */
253 g_thread_init_with_errorcheck_mutexes (GThreadFunctions
* init
)
255 GThreadFunctions errorcheck_functions
;
257 g_error ("Errorcheck mutexes can only be used for native "
258 "thread implementations. Sorry." );
260 #ifdef HAVE_G_THREAD_IMPL_INIT
261 /* This isn't called in g_thread_init, as it doesn't think to get
262 * the default implementation, so we have to call it on our own.
264 * We must call this before copying
265 * g_thread_functions_for_glib_use_default as the
266 * implementation-specific init function might modify the contents
267 * of g_thread_functions_for_glib_use_default based on operating
268 * system version, C library version, or whatever. */
269 g_thread_impl_init();
270 #endif /* HAVE_G_THREAD_IMPL_INIT */
272 errorcheck_functions
= g_thread_functions_for_glib_use_default
;
273 errorcheck_functions
.mutex_new
= g_mutex_new_errorcheck_impl
;
274 errorcheck_functions
.mutex_lock
=
275 (void (*)(GMutex
*)) g_mutex_lock_errorcheck_impl
;
276 errorcheck_functions
.mutex_trylock
=
277 (gboolean (*)(GMutex
*)) g_mutex_trylock_errorcheck_impl
;
278 errorcheck_functions
.mutex_unlock
=
279 (void (*)(GMutex
*)) g_mutex_unlock_errorcheck_impl
;
280 errorcheck_functions
.mutex_free
=
281 (void (*)(GMutex
*)) g_mutex_free_errorcheck_impl
;
282 errorcheck_functions
.cond_wait
=
283 (void (*)(GCond
*, GMutex
*)) g_cond_wait_errorcheck_impl
;
284 errorcheck_functions
.cond_timed_wait
=
285 (gboolean (*)(GCond
*, GMutex
*, GTimeVal
*))
286 g_cond_timed_wait_errorcheck_impl
;
288 g_thread_init (&errorcheck_functions
);
292 g_thread_init (GThreadFunctions
* init
)
296 if (thread_system_already_initialized
)
299 g_warning ("GThread system already initialized, ignoring custom thread implementation.");
304 thread_system_already_initialized
= TRUE
;
308 #ifdef HAVE_G_THREAD_IMPL_INIT
309 /* now do any initialization stuff required by the
310 * implementation, but only if called with a NULL argument, of
311 * course. Otherwise it's up to the user to do so. */
312 g_thread_impl_init();
313 #endif /* HAVE_G_THREAD_IMPL_INIT */
314 init
= &g_thread_functions_for_glib_use_default
;
317 g_thread_use_default_impl
= FALSE
;
319 g_thread_functions_for_glib_use
= *init
;
320 if (g_thread_gettime_impl
)
321 g_thread_gettime
= g_thread_gettime_impl
;
323 supported
= (init
->mutex_new
&&
325 init
->mutex_trylock
&&
326 init
->mutex_unlock
&&
330 init
->cond_broadcast
&&
332 init
->cond_timed_wait
&&
337 init
->thread_create
&&
338 init
->thread_yield
&&
341 init
->thread_set_priority
&&
344 /* if somebody is calling g_thread_init (), it means that he wants to
345 * have thread support, so check this
349 if (g_thread_use_default_impl
)
350 g_error ("Threads are not supported on this platform.");
352 g_error ("The supplied thread function vector is invalid.");
355 g_thread_priority_map
[G_THREAD_PRIORITY_LOW
] = PRIORITY_LOW_VALUE
;
356 g_thread_priority_map
[G_THREAD_PRIORITY_NORMAL
] = PRIORITY_NORMAL_VALUE
;
357 g_thread_priority_map
[G_THREAD_PRIORITY_HIGH
] = PRIORITY_HIGH_VALUE
;
358 g_thread_priority_map
[G_THREAD_PRIORITY_URGENT
] = PRIORITY_URGENT_VALUE
;
360 g_thread_init_glib ();
363 #else /* !G_THREADS_ENABLED */
366 g_thread_init (GThreadFunctions
* init
)
368 g_error ("GLib thread support is disabled.");
372 g_thread_init_with_errorcheck_mutexes (GThreadFunctions
* init
)
374 g_error ("GLib thread support is disabled.");
377 #endif /* !G_THREADS_ENABLED */