1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* gthread.c: thread related functions
* Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
static gboolean thread_system_already_initialized = FALSE;
static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
#include G_THREAD_SOURCE
#ifndef PRIORITY_LOW_VALUE
# define PRIORITY_LOW_VALUE 0
#endif
#ifndef PRIORITY_URGENT_VALUE
# define PRIORITY_URGENT_VALUE 0
#endif
#ifndef PRIORITY_NORMAL_VALUE
# define PRIORITY_NORMAL_VALUE \
(PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 4 / 10)
#endif /* PRIORITY_NORMAL_VALUE */
#ifndef PRIORITY_HIGH_VALUE
# define PRIORITY_HIGH_VALUE \
(PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 8 / 10)
#endif /* PRIORITY_HIGH_VALUE */
void g_mutex_init (void);
void g_mem_init (void);
void g_messages_init (void);
#define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
typedef struct _ErrorCheckInfo ErrorCheckInfo;
struct _ErrorCheckInfo
{
gchar *location;
GThread *owner;
};
static GMutex *
g_mutex_new_errorcheck_impl (void)
{
GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (gpointer));
G_MUTEX_DEBUG_INFO (retval) = NULL;
return retval;
}
static void
g_mutex_lock_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
{
ErrorCheckInfo *info;
GThread *self = g_thread_self ();
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
{
/* if the debug info is NULL, we have not yet locked that mutex,
* so we do it now */
g_thread_functions_for_glib_use_default.mutex_lock (mutex);
/* Now we have to check again, because another thread might have
* tried to lock the mutex at the same time we did. This
* technique is not 100% save on systems without decent cache
* coherence, but we have no choice */
if (G_MUTEX_DEBUG_INFO (mutex) == NULL)
{
info = G_MUTEX_DEBUG_INFO (mutex) = g_new0 (ErrorCheckInfo, 1);
}
g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
}
info = G_MUTEX_DEBUG_INFO (mutex);
if (info->owner == self)
g_error ("Trying to recursivly lock a mutex at '%s', "
"previously locked at '%s'",
location, info->location);
g_thread_functions_for_glib_use_default.mutex_lock (mutex);
info->owner = self;
info->location = location;
}
static gboolean
g_mutex_trylock_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GThread *self = g_thread_self ();
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info)
{
/* This mutex has not yet been used, so simply lock and return TRUE */
g_mutex_lock_errorcheck_impl (mutex, magic, location);
return TRUE;
}
if (info->owner == self)
g_error ("Trying to recursivly lock a mutex at '%s', "
"previously locked at '%s'",
location, info->location);
if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
return FALSE;
info->owner = self;
info->location = location;
return TRUE;
}
static void
g_mutex_unlock_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GThread *self = g_thread_self ();
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info || info->owner == NULL)
g_error ("Trying to unlock an unlocked mutex at '%s'", location);
if (info->owner != self)
g_warning ("Trying to unlock a mutex at '%s', "
"previously locked by a different thread at '%s'",
location, info->location);
info->owner = NULL;
info->location = NULL;
g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
}
static void
g_mutex_free_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (info && info->owner != NULL)
g_error ("Trying to free a locked mutex at '%s', "
"which was previously locked at '%s'",
location, info->location);
g_free (G_MUTEX_DEBUG_INFO (mutex));
g_thread_functions_for_glib_use_default.mutex_free (mutex);
}
static void
g_cond_wait_errorcheck_impl (GCond *cond,
GMutex *mutex,
gulong magic,
gchar *location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GThread *self = g_thread_self ();
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info || info->owner == NULL)
g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
location);
if (info->owner != self)
g_error ("Trying to use a mutex locked by another thread in "
"g_cond_wait() at '%s'", location);
info->owner = NULL;
location = info->location;
g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
info->owner = self;
info->location = location;
}
static gboolean
g_cond_timed_wait_errorcheck_impl (GCond *cond,
GMutex *mutex,
GTimeVal *end_time,
gulong magic,
gchar *location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GThread *self = g_thread_self ();
gboolean retval;
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info || info->owner == NULL)
g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
location);
if (info->owner != self)
g_error ("Trying to use a mutex locked by another thread in "
"g_cond_timed_wait() at '%s'", location);
info->owner = NULL;
location = info->location;
retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
mutex,
end_time);
info->owner = self;
info->location = location;
return retval;
}
/* unshadow function declaration. See gthread.h */
#undef g_thread_init
void
g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
{
GThreadFunctions errorcheck_functions;
if (init)
g_error ("Errorcheck mutexes can only be used for native "
"thread implementations. Sorry." );
errorcheck_functions = g_thread_functions_for_glib_use_default;
errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
errorcheck_functions.mutex_lock =
(void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
errorcheck_functions.mutex_trylock =
(gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
errorcheck_functions.mutex_unlock =
(void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
errorcheck_functions.mutex_free =
(void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
errorcheck_functions.cond_wait =
(void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
errorcheck_functions.cond_timed_wait =
(gboolean (*)(GCond *, GMutex *, GTimeVal *))
g_cond_timed_wait_errorcheck_impl;
#ifdef HAVE_G_THREAD_IMPL_INIT
/* This isn't called in g_thread_init, as it doesn't think to get
* the default implementation, so we have to call it on our own. */
g_thread_impl_init();
#endif
g_thread_init (&errorcheck_functions);
}
void
g_thread_init (GThreadFunctions* init)
{
gboolean supported;
#ifndef G_THREADS_ENABLED
g_error ("GLib thread support is disabled.");
#endif /* !G_THREADS_ENABLED */
if (thread_system_already_initialized)
g_error ("GThread system may only be initialized once.");
thread_system_already_initialized = TRUE;
if (init == NULL)
{
#ifdef HAVE_G_THREAD_IMPL_INIT
/* now do any initialization stuff required by the
* implementation, but only if called with a NULL argument, of
* course. Otherwise it's up to the user to do so. */
g_thread_impl_init();
#endif /* HAVE_G_THREAD_IMPL_INIT */
init = &g_thread_functions_for_glib_use_default;
}
else
g_thread_use_default_impl = FALSE;
g_thread_functions_for_glib_use = *init;
/* It is important, that g_threads_got_initialized is not set before the
* thread initialization functions of the different modules are called
*/
supported = (init->mutex_new &&
init->mutex_lock &&
init->mutex_trylock &&
init->mutex_unlock &&
init->mutex_free &&
init->cond_new &&
init->cond_signal &&
init->cond_broadcast &&
init->cond_wait &&
init->cond_timed_wait &&
init->cond_free &&
init->private_new &&
init->private_get &&
init->private_get &&
init->thread_create &&
init->thread_yield &&
init->thread_join &&
init->thread_exit &&
init->thread_set_priority &&
init->thread_self);
/* if somebody is calling g_thread_init (), it means that he wants to
* have thread support, so check this
*/
if (!supported)
{
if (g_thread_use_default_impl)
g_error ("Threads are not supported on this platform.");
else
g_error ("The supplied thread function vector is invalid.");
}
g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
/* now call the thread initialization functions of the different
* glib modules. order does matter, g_mutex_init MUST come first.
*/
g_mutex_init ();
g_mem_init ();
g_messages_init ();
/* now we can set g_threads_got_initialized and thus enable
* all the thread functions
*/
g_threads_got_initialized = TRUE;
/* we want the main thread to run with normal priority */
g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
}
|