diff --git a/ChangeLog b/ChangeLog index a54344a69..b3b2632c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index a54344a69..b3b2632c6 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,13 @@ +2000-09-29 Sebastian Wilhelmi + + * configure.in, glib.h: Added errorcheck mutexes. These are + activated through the preprocessor symbol + G_ERRORCHECK_MUTEXES. Need to add an extra word to StaticMutex in + order to achieve this. g_(static_)mutex_* functions instrument the + mutex operations with mutex name and location, when compiled with + -DG_ERRORCHECK_MUTEXES. g_thread_init activates the errorcheck + mutexes, when compiled with -DG_ERRORCHECK_MUTEXES. + 2000-09-28 Havoc Pennington * glib.h (GThreadPriority): fix indentation diff --git a/configure.in b/configure.in index b85d7b710..be002dc66 100644 --- a/configure.in +++ b/configure.in @@ -1330,16 +1330,19 @@ typedef struct _GStaticMutex GStaticMutex; struct _GStaticMutex { struct _GMutex *runtime_mutex; - union { - char pad[$g_mutex_sizeof]; - double dummy_double; - void *dummy_pointer; - long dummy_long; - } aligned_pad_u; + struct { + union { + char pad[$g_mutex_sizeof]; + double dummy_double; + void *dummy_pointer; + long dummy_long; + } mutex; + void *debug_info; + } static_mutex; }; -#define G_STATIC_MUTEX_INIT { NULL, { { $g_mutex_contents} } } +#define G_STATIC_MUTEX_INIT { NULL, { { { $g_mutex_contents} }, NULL } } #define g_static_mutex_get_mutex(mutex) \ - (g_thread_use_default_impl ? ((GMutex*) &((mutex)->aligned_pad_u)) : \ + (g_thread_use_default_impl ? ((GMutex*) &((mutex)->static_mutex)) : \ g_static_mutex_get_mutex_impl (&((mutex)->runtime_mutex))) _______EOF else diff --git a/glib.h b/glib.h index c2f32fecc..f485552fe 100644 --- a/glib.h +++ b/glib.h @@ -3147,22 +3147,56 @@ GLIB_VAR gboolean g_threads_got_initialized; */ void g_thread_init (GThreadFunctions *vtable); +/* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all + * mutexes will check for re-locking and re-unlocking */ + +/* Initialize thread system with errorcheck mutexes. vtable must be + * NULL.Do not call directly. Use #define G_ERRORCHECK_MUTEXES + * instead. + */ +void g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable); + +/* A random number to recognize debug calls to g_mutex_... */ +#define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7 + +#ifdef G_ERRORCHECK_MUTEXES +#define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable) +#endif + /* internal function for fallback static mutex implementation */ GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex); /* shorthands for conditional and unconditional function calls */ -#define G_THREAD_UF(name, arglist) \ - (*g_thread_functions_for_glib_use . name) arglist -#define G_THREAD_CF(name, fail, arg) \ - (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail)) -/* keep in mind, all those mutexes and static mutexes are not - * recursive in general, don't rely on that - */ +#define G_THREAD_UF(op, arglist) \ + (*g_thread_functions_for_glib_use . op) arglist +#define G_THREAD_CF(op, fail, arg) \ + (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail)) +#define G_THREAD_ECF(op, fail, mutex, name, type) \ + (g_thread_supported () ? \ + ((type(*)(GMutex*, gulong, gchar*, gchar*)) \ + (*g_thread_functions_for_glib_use . op)) \ + (mutex, G_MUTEX_DEBUG_MAGIC, G_STRINGIFY (name), G_STRLOC) : (fail)) +#ifndef G_ERRORCHECK_MUTEXES +#define g_mutex_lock_with_debug_name(mutex, name) \ + G_THREAD_CF (mutex_lock, (void)0, (mutex)) +#define g_mutex_trylock_with_debug_name(mutex, name) \ + G_THREAD_CF (mutex_trylock, TRUE, (mutex)) +#define g_mutex_unlock_with_debug_name(mutex, name) \ + G_THREAD_CF (mutex_unlock, (void)0, (mutex)) +#else /* G_ERRORCHECK_MUTEXES */ +#define g_mutex_lock_with_debug_name(mutex, name) \ + G_THREAD_ECF (mutex_lock, (void)0, mutex, name, void) +#define g_mutex_trylock_with_debug_name(mutex, name) \ + G_THREAD_ECF (mutex_trylock, TRUE, mutex, name, gboolean) +#define g_mutex_unlock_with_debug_name(mutex, name) \ + G_THREAD_ECF (mutex_unlock, (void)0, mutex, name, void) +#endif /* G_ERRORCHECK_MUTEXES */ + #define g_thread_supported() (g_threads_got_initialized) #define g_mutex_new() G_THREAD_UF (mutex_new, ()) -#define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex)) -#define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex)) -#define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex)) +#define g_mutex_lock(mutex) g_mutex_lock_with_debug_name(mutex, mutex) +#define g_mutex_trylock(mutex) g_mutex_trylock_with_debug_name(mutex, mutex) +#define g_mutex_unlock(mutex) g_mutex_unlock_with_debug_name(mutex, mutex) #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex)) #define g_cond_new() G_THREAD_UF (cond_new, ()) #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond)) @@ -3203,11 +3237,11 @@ void g_thread_set_priority (GThread *thread, * use */ #define g_static_mutex_lock(mutex) \ - g_mutex_lock (g_static_mutex_get_mutex (mutex)) + g_mutex_lock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) #define g_static_mutex_trylock(mutex) \ - g_mutex_trylock (g_static_mutex_get_mutex (mutex)) + g_mutex_trylock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) #define g_static_mutex_unlock(mutex) \ - g_mutex_unlock (g_static_mutex_get_mutex (mutex)) + g_mutex_unlock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) struct _GStaticPrivate { diff --git a/glib/glib.h b/glib/glib.h index c2f32fecc..f485552fe 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -3147,22 +3147,56 @@ GLIB_VAR gboolean g_threads_got_initialized; */ void g_thread_init (GThreadFunctions *vtable); +/* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all + * mutexes will check for re-locking and re-unlocking */ + +/* Initialize thread system with errorcheck mutexes. vtable must be + * NULL.Do not call directly. Use #define G_ERRORCHECK_MUTEXES + * instead. + */ +void g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable); + +/* A random number to recognize debug calls to g_mutex_... */ +#define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7 + +#ifdef G_ERRORCHECK_MUTEXES +#define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable) +#endif + /* internal function for fallback static mutex implementation */ GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex); /* shorthands for conditional and unconditional function calls */ -#define G_THREAD_UF(name, arglist) \ - (*g_thread_functions_for_glib_use . name) arglist -#define G_THREAD_CF(name, fail, arg) \ - (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail)) -/* keep in mind, all those mutexes and static mutexes are not - * recursive in general, don't rely on that - */ +#define G_THREAD_UF(op, arglist) \ + (*g_thread_functions_for_glib_use . op) arglist +#define G_THREAD_CF(op, fail, arg) \ + (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail)) +#define G_THREAD_ECF(op, fail, mutex, name, type) \ + (g_thread_supported () ? \ + ((type(*)(GMutex*, gulong, gchar*, gchar*)) \ + (*g_thread_functions_for_glib_use . op)) \ + (mutex, G_MUTEX_DEBUG_MAGIC, G_STRINGIFY (name), G_STRLOC) : (fail)) +#ifndef G_ERRORCHECK_MUTEXES +#define g_mutex_lock_with_debug_name(mutex, name) \ + G_THREAD_CF (mutex_lock, (void)0, (mutex)) +#define g_mutex_trylock_with_debug_name(mutex, name) \ + G_THREAD_CF (mutex_trylock, TRUE, (mutex)) +#define g_mutex_unlock_with_debug_name(mutex, name) \ + G_THREAD_CF (mutex_unlock, (void)0, (mutex)) +#else /* G_ERRORCHECK_MUTEXES */ +#define g_mutex_lock_with_debug_name(mutex, name) \ + G_THREAD_ECF (mutex_lock, (void)0, mutex, name, void) +#define g_mutex_trylock_with_debug_name(mutex, name) \ + G_THREAD_ECF (mutex_trylock, TRUE, mutex, name, gboolean) +#define g_mutex_unlock_with_debug_name(mutex, name) \ + G_THREAD_ECF (mutex_unlock, (void)0, mutex, name, void) +#endif /* G_ERRORCHECK_MUTEXES */ + #define g_thread_supported() (g_threads_got_initialized) #define g_mutex_new() G_THREAD_UF (mutex_new, ()) -#define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex)) -#define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex)) -#define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex)) +#define g_mutex_lock(mutex) g_mutex_lock_with_debug_name(mutex, mutex) +#define g_mutex_trylock(mutex) g_mutex_trylock_with_debug_name(mutex, mutex) +#define g_mutex_unlock(mutex) g_mutex_unlock_with_debug_name(mutex, mutex) #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex)) #define g_cond_new() G_THREAD_UF (cond_new, ()) #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond)) @@ -3203,11 +3237,11 @@ void g_thread_set_priority (GThread *thread, * use */ #define g_static_mutex_lock(mutex) \ - g_mutex_lock (g_static_mutex_get_mutex (mutex)) + g_mutex_lock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) #define g_static_mutex_trylock(mutex) \ - g_mutex_trylock (g_static_mutex_get_mutex (mutex)) + g_mutex_trylock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) #define g_static_mutex_unlock(mutex) \ - g_mutex_unlock (g_static_mutex_get_mutex (mutex)) + g_mutex_unlock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) struct _GStaticPrivate { diff --git a/gthread/ChangeLog b/gthread/ChangeLog index 5dcdffde2..f0de00140 100644 --- a/gthread/ChangeLog +++ b/gthread/ChangeLog @@ -1,3 +1,15 @@ +2000-09-29 Sebastian Wilhelmi + + * gthread-impl.c: Added errorcheck mutexes. New exported function + g_thread_init_with_errorcheck_mutexes, which is called instead of + g_thread_init, when compiled with -DG_ERRORCHECK_MUTEXES. New + static functions + g_mutex_(new|lock|trylock|unlock|free)_errorcheck_impl to + implement errorcheck mutexes. + + * gthread-posix.impl.c, gthread-solaris-impl.c: Define the size of + a mutex. + 2000-09-21 Tor Lillqvist * makefile.mingw.in: Use pthreads macros from ../build. diff --git a/gthread/gthread-impl.c b/gthread/gthread-impl.c index 2c665e814..a1e55bc03 100644 --- a/gthread/gthread-impl.c +++ b/gthread/gthread-impl.c @@ -48,6 +48,153 @@ 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 *name; + 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 inline void +fill_info (ErrorCheckInfo *info, + GThread *self, + gulong magic, + gchar *name, + gchar *location) +{ + info->owner = self; + if (magic == G_MUTEX_DEBUG_MAGIC) + { + /* We are used with special instrumented calls, where name and + * location is provided as well, so use them */ + info->name = name; + info->location = location; + } + else + info->name = info->location = "unknown"; +} + +static void +g_mutex_lock_errorcheck_impl (GMutex *mutex, + gulong magic, + gchar *name, + gchar *location) +{ + ErrorCheckInfo *info; + GThread *self = g_thread_self (); + 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 anothe thread might mave + * locked 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 the mutex '%s' at '%s', " + "previously locked by name '%s' at '%s'", + name, location, info->name, info->location); + + g_thread_functions_for_glib_use_default.mutex_lock (mutex); + + fill_info (info, self, magic, name, location); +} + +static gboolean +g_mutex_trylock_errorcheck_impl (GMutex *mutex, + gulong magic, + gchar *name, + gchar *location) +{ + ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex); + GThread *self = g_thread_self (); + if (!info) + { + /* This mutex has not yet been used, so simply lock and return TRUE */ + g_mutex_lock_errorcheck_impl (mutex, magic, name, location); + return TRUE; + } + if (info->owner == self) + g_error ("Trying to recursivly lock the mutex '%s' at '%s', " + "previously locked by name '%s' at '%s'", + name, location, info->name, info->location); + + if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex)) + return FALSE; + + fill_info (info, self, magic, name, location); + return TRUE; +} + +static void +g_mutex_unlock_errorcheck_impl (GMutex *mutex, + gulong magic, + gchar *name, + gchar *location) +{ + ErrorCheckInfo *info = G_MUTEX_DEBUG_INFO (mutex); + GThread *self = g_thread_self (); + if (magic != G_MUTEX_DEBUG_MAGIC) + name = location = "unknown"; + if (!info || info->owner != self) + g_error ("Trying to unlock the unlocked mutex '%s' at '%s'", + name, location); + info->owner = NULL; + info->name = info->location = NULL; + g_thread_functions_for_glib_use_default.mutex_unlock (mutex); +} + +static void +g_mutex_free_errorcheck_impl (GMutex *mutex) +{ + g_free (G_MUTEX_DEBUG_INFO (mutex)); + g_thread_functions_for_glib_use_default.mutex_free (mutex); +} + +/* unshadow function declaration. See glib.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 = g_mutex_free_errorcheck_impl; + + g_thread_init (&errorcheck_functions); +} + void g_thread_init (GThreadFunctions* init) { diff --git a/gthread/gthread-posix.c b/gthread/gthread-posix.c index bae02f10c..18caa148b 100644 --- a/gthread/gthread-posix.c +++ b/gthread/gthread-posix.c @@ -73,6 +73,8 @@ gulong g_thread_min_stack_size = 0; +#define G_MUTEX_SIZE (sizeof (pthread_mutex_t)) + #define HAVE_G_THREAD_IMPL_INIT static void g_thread_impl_init() diff --git a/gthread/gthread-solaris.c b/gthread/gthread-solaris.c index 99e5ee97a..76bc644c2 100644 --- a/gthread/gthread-solaris.c +++ b/gthread/gthread-solaris.c @@ -51,6 +51,8 @@ gulong g_thread_min_stack_size = 0; +#define G_MUTEX_SIZE (sizeof (mutex_t)) + #define HAVE_G_THREAD_IMPL_INIT static void g_thread_impl_init()