Make the magic and location arguments to the error-checking-mutex

2006-05-10  Sebastian Wilhelmi  <wilhelmi@google.com>

	* glib/gthread.h, gthread/gthread-impl.c: Make the magic and
	location arguments to the error-checking-mutex functions const and
	do not write to them, as we might not own them. Clean up the
	error-checking-mutex code quite a bit. (#335198, Chris Wilson)
This commit is contained in:
Sebastian Wilhelmi 2006-05-11 00:18:46 +00:00 committed by Sebastian Wilhelmi
parent a93560b83d
commit 268084caf9
4 changed files with 110 additions and 133 deletions

View File

@ -1,5 +1,10 @@
2006-05-10 Sebastian Wilhelmi <wilhelmi@google.com>
* glib/gthread.h, gthread/gthread-impl.c: Make the magic and
location arguments to the error-checking-mutex functions const and
do not write to them, as we might not own them. Clean up the
error-checking-mutex code quite a bit. (#335198, Chris Wilson)
* glib/gthread.c: Use g_atomic_pointer_set instead of old
homegrown version now that we have it. (#335198, Chris Wilson)

View File

@ -1,5 +1,10 @@
2006-05-10 Sebastian Wilhelmi <wilhelmi@google.com>
* glib/gthread.h, gthread/gthread-impl.c: Make the magic and
location arguments to the error-checking-mutex functions const and
do not write to them, as we might not own them. Clean up the
error-checking-mutex code quite a bit. (#335198, Chris Wilson)
* glib/gthread.c: Use g_atomic_pointer_set instead of old
homegrown version now that we have it. (#335198, Chris Wilson)

View File

@ -148,7 +148,8 @@ GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
#define G_THREAD_CF(op, fail, arg) \
(g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
#define G_THREAD_ECF(op, fail, mutex, type) \
(g_thread_supported () ? ((type(*)(GMutex*, gulong, gchar*)) \
(g_thread_supported () ? \
((type(*)(GMutex*, const gulong, gchar const*)) \
(*g_thread_functions_for_glib_use . op)) \
(mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))
@ -374,4 +375,3 @@ extern void glib_dummy_decl (void);
G_END_DECLS
#endif /* __G_THREAD_H__ */

View File

@ -69,119 +69,95 @@ void g_convert_init (void);
void g_rand_init (void);
void g_main_thread_init (void);
#define G_MUTEX_DEBUG_INFO(mutex) (*((gpointer*)(((char*)mutex)+G_MUTEX_SIZE)))
typedef struct _ErrorCheckInfo ErrorCheckInfo;
struct _ErrorCheckInfo
typedef struct _GMutexDebugInfo GMutexDebugInfo;
struct _GMutexDebugInfo
{
gchar *location;
GSystemThread owner;
};
#define G_MUTEX_DEBUG_INFO(mutex) \
(((GMutexDebugInfo*)(((char*)mutex)+G_MUTEX_SIZE)))
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;
GMutexDebugInfo *info;
retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (GMutexDebugInfo));
info = G_MUTEX_DEBUG_INFO (retval);
g_system_thread_assign (info->owner, zero_thread);
info->location = "invalid";
return retval;
}
static void
g_mutex_lock_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
const gulong magic,
gchar * const location)
{
ErrorCheckInfo *info;
GSystemThread self;
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
GSystemThread self;
g_thread_functions_for_glib_use.thread_self (&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 (g_system_thread_equal (info->owner, self))
g_error ("Trying to recursivly lock a mutex at '%s', "
"previously locked at '%s'",
location, info->location);
loc, info->location);
g_thread_functions_for_glib_use_default.mutex_lock (mutex);
g_system_thread_assign (info->owner, self);
info->location = location;
info->location = loc;
}
static gboolean
g_mutex_trylock_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
const gulong magic,
gchar * const location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
GSystemThread self;
g_thread_functions_for_glib_use.thread_self (&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 (g_system_thread_equal (info->owner, self))
g_error ("Trying to recursivly lock a mutex at '%s', "
"previously locked at '%s'",
location, info->location);
loc, info->location);
if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
return FALSE;
g_system_thread_assign (info->owner, self);
info->location = location;
info->location = loc;
return TRUE;
}
static void
g_mutex_unlock_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
const gulong magic,
gchar * const location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GSystemThread self;
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
GSystemThread self;
g_thread_functions_for_glib_use.thread_self (&self);
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info || g_system_thread_equal (info->owner, zero_thread))
g_error ("Trying to unlock an unlocked mutex at '%s'", location);
if (g_system_thread_equal (info->owner, zero_thread))
g_error ("Trying to unlock an unlocked mutex at '%s'", loc);
if (!g_system_thread_equal (info->owner, self))
g_warning ("Trying to unlock a mutex at '%s', "
"previously locked by a different thread at '%s'",
location, info->location);
loc, info->location);
g_system_thread_assign (info->owner, zero_thread);
info->location = NULL;
@ -191,53 +167,46 @@ g_mutex_unlock_errorcheck_impl (GMutex *mutex,
static void
g_mutex_free_errorcheck_impl (GMutex *mutex,
gulong magic,
gchar *location)
const gulong magic,
gchar * const location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
if (info && !g_system_thread_equal (info->owner, zero_thread))
g_error ("Trying to free a locked mutex at '%s', "
"which was previously locked at '%s'",
location, info->location);
loc, 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)
const gulong magic,
gchar * const location)
{
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GSystemThread self;
g_thread_functions_for_glib_use.thread_self (&self);
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info || g_system_thread_equal (info->owner, zero_thread))
g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'",
location);
if (g_system_thread_equal (info->owner, zero_thread))
g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'", loc);
if (!g_system_thread_equal (info->owner, self))
g_error ("Trying to use a mutex locked by another thread in "
"g_cond_wait() at '%s'", location);
"g_cond_wait() at '%s'", loc);
g_system_thread_assign (info->owner, zero_thread);
location = info->location;
loc = info->location;
g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
g_system_thread_assign (info->owner, self);
info->location = location;
info->location = loc;
}
@ -245,35 +214,33 @@ static gboolean
g_cond_timed_wait_errorcheck_impl (GCond *cond,
GMutex *mutex,
GTimeVal *end_time,
gulong magic,
gchar *location)
const gulong magic,
gchar * const location)
{
ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex);
GSystemThread self;
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
gboolean retval;
GSystemThread self;
g_thread_functions_for_glib_use.thread_self (&self);
if (magic != G_MUTEX_DEBUG_MAGIC)
location = "unknown";
if (!info || g_system_thread_equal (info->owner, zero_thread))
if (g_system_thread_equal (info->owner, zero_thread))
g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
location);
loc);
if (!g_system_thread_equal (info->owner, self))
g_error ("Trying to use a mutex locked by another thread in "
"g_cond_timed_wait() at '%s'", location);
"g_cond_timed_wait() at '%s'", loc);
g_system_thread_assign (info->owner, zero_thread);
location = info->location;
loc = info->location;
retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
mutex,
end_time);
g_system_thread_assign (info->owner, self);
info->location = location;
info->location = loc;
return retval;
}