mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-24 14:36:13 +01:00
GThread: remove errorcheck mutex support
This can only possibly work if we call g_thread_init(), which we are moving away from.
This commit is contained in:
parent
41cbb40dca
commit
3f93141243
@ -126,15 +126,6 @@ GLIB_VAR guint64 (*g_thread_gettime) (void);
|
||||
*/
|
||||
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);
|
||||
|
||||
/* Checks if thread support is initialized. Identical to the
|
||||
* g_thread_supported macro but provided for language bindings.
|
||||
*/
|
||||
@ -143,10 +134,6 @@ gboolean g_thread_get_initialized (void);
|
||||
/* 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);
|
||||
|
||||
|
@ -36,7 +36,6 @@
|
||||
#include "glib.h"
|
||||
#include "gthreadprivate.h"
|
||||
|
||||
static GSystemThread zero_thread; /* This is initialized to all zero */
|
||||
static gboolean thread_system_already_initialized = FALSE;
|
||||
static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
|
||||
|
||||
@ -60,225 +59,6 @@ static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
|
||||
((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
|
||||
#endif /* PRIORITY_HIGH_VALUE */
|
||||
|
||||
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 ();
|
||||
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,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
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 (g_system_thread_equal (info->owner, self))
|
||||
g_error ("Trying to recursively lock a mutex at '%s', "
|
||||
"previously locked at '%s'",
|
||||
loc, info->location);
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_lock (mutex);
|
||||
|
||||
g_system_thread_assign (info->owner, self);
|
||||
info->location = loc;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_mutex_trylock_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
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 (g_system_thread_equal (info->owner, self))
|
||||
g_error ("Trying to recursivly lock a mutex at '%s', "
|
||||
"previously locked at '%s'",
|
||||
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 = loc;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_unlock_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
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 (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'",
|
||||
loc, info->location);
|
||||
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
info->location = NULL;
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_free_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
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'",
|
||||
loc, info->location);
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_free (mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_wait_errorcheck_impl (GCond *cond,
|
||||
GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
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 (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'", loc);
|
||||
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
loc = info->location;
|
||||
|
||||
g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
|
||||
|
||||
g_system_thread_assign (info->owner, self);
|
||||
info->location = loc;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
g_cond_timed_wait_errorcheck_impl (GCond *cond,
|
||||
GMutex *mutex,
|
||||
GTimeVal *end_time,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
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 (g_system_thread_equal (info->owner, zero_thread))
|
||||
g_error ("Trying to use an unlocked mutex in g_cond_timed_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_timed_wait() at '%s'", loc);
|
||||
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
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 = loc;
|
||||
|
||||
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." );
|
||||
|
||||
#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.
|
||||
*
|
||||
* We must call this before copying
|
||||
* g_thread_functions_for_glib_use_default as the
|
||||
* implementation-specific init function might modify the contents
|
||||
* of g_thread_functions_for_glib_use_default based on operating
|
||||
* system version, C library version, or whatever. */
|
||||
g_thread_impl_init();
|
||||
#endif /* HAVE_G_THREAD_IMPL_INIT */
|
||||
|
||||
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;
|
||||
|
||||
g_thread_init (&errorcheck_functions);
|
||||
}
|
||||
|
||||
void
|
||||
g_thread_init (GThreadFunctions* init)
|
||||
{
|
||||
@ -348,3 +128,11 @@ g_thread_init (GThreadFunctions* init)
|
||||
|
||||
g_thread_init_glib ();
|
||||
}
|
||||
|
||||
void
|
||||
g_thread_init_with_errorcheck_mutexes (GThreadFunctions *vtable)
|
||||
{
|
||||
g_assert (vtable == NULL);
|
||||
|
||||
g_thread_init (NULL);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user