From 2a677d1370a1983c2c5e1a4a6dd5f0d9fa9868b3 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Sun, 2 Oct 2011 20:51:38 -0400 Subject: [PATCH] locks: drop _INIT macros All locks are now zero-initialised, so we can drop the G_*_INIT macros for them. Adjust various users around GLib accordingly and change the docs. https://bugzilla.gnome.org/show_bug.cgi?id=659866 --- docs/reference/glib/glib-sections.txt | 4 --- gio/gunionvolumemonitor.c | 2 +- glib/deprecated/gthread.h | 2 +- glib/gbitlock.c | 2 +- glib/glib-ctor.h | 2 +- glib/gmem.c | 2 +- glib/gmessages.c | 2 +- glib/gslice.c | 2 +- glib/gthread-posix.c | 17 ++++----- glib/gthread-win32.c | 9 ++--- glib/gthread.c | 67 ++++------------------------------- glib/gthread.h | 7 +--- glib/gvarianttypeinfo.c | 2 +- glib/tests/cond.c | 4 +-- glib/tests/mutex.c | 8 ++--- glib/tests/private.c | 6 ++-- glib/tests/rec-mutex.c | 6 ++-- glib/tests/rwlock.c | 18 +++------- gmodule/gmodule.c | 2 +- gobject/gparam.c | 2 +- gobject/gtype.c | 4 +-- tests/thread-test.c | 2 +- 22 files changed, 44 insertions(+), 128 deletions(-) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index c60c16c..d06d5bd 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -605,7 +605,6 @@ g_thread_foreach GMutex -G_MUTEX_INIT g_mutex_init g_mutex_clear g_mutex_new @@ -616,7 +615,6 @@ g_mutex_unlock GRecMutex -G_REC_MUTEX_INIT g_rec_mutex_init g_rec_mutex_clear g_rec_mutex_lock @@ -625,7 +623,6 @@ g_rec_mutex_unlock GRWLock -G_RW_LOCK_INIT g_rw_lock_init g_rw_lock_clear g_rw_lock_writer_lock @@ -678,7 +675,6 @@ g_static_rw_lock_free GCond -G_COND_INIT g_cond_new g_cond_free g_cond_init diff --git a/gio/gunionvolumemonitor.c b/gio/gunionvolumemonitor.c index 6d9b21b..cd814ad 100644 --- a/gio/gunionvolumemonitor.c +++ b/gio/gunionvolumemonitor.c @@ -52,7 +52,7 @@ static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_mo #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR); -static GRecMutex the_volume_monitor_mutex = G_REC_MUTEX_INIT; +static GRecMutex the_volume_monitor_mutex; static GUnionVolumeMonitor *the_volume_monitor = NULL; diff --git a/glib/deprecated/gthread.h b/glib/deprecated/gthread.h index 1669991..8e013a8 100644 --- a/glib/deprecated/gthread.h +++ b/glib/deprecated/gthread.h @@ -126,7 +126,7 @@ typedef struct { struct _GMutex *unused; GMutex mutex; } GStaticMutex; -#define G_STATIC_MUTEX_INIT { NULL, G_MUTEX_INIT } +#define G_STATIC_MUTEX_INIT { NULL, { NULL } } #define g_static_mutex_get_mutex(s) (&(s)->mutex) #endif /* G_OS_WIN32 */ diff --git a/glib/gbitlock.c b/glib/gbitlock.c index 1d241fb..1b2d172 100644 --- a/glib/gbitlock.c +++ b/glib/gbitlock.c @@ -36,7 +36,7 @@ #endif #ifndef HAVE_FUTEX -static GMutex g_futex_mutex = G_MUTEX_INIT; +static GMutex g_futex_mutex; static GSList *g_futex_address_list = NULL; #endif diff --git a/glib/glib-ctor.h b/glib/glib-ctor.h index a7584ea..7b299f9 100644 --- a/glib/glib-ctor.h +++ b/glib/glib-ctor.h @@ -7,7 +7,7 @@ #else /* could be vastly improved... */ #define GLIB_CTOR(func) \ - static GMutex g__##func##_mutex = G_MUTEX_INIT; \ + static GMutex g__##func##_mutex; \ static gboolean g__##func##_initialised; \ static void func (void) #define GLIB_ENSURE_CTOR(func) \ diff --git a/glib/gmem.c b/glib/gmem.c index 0e1b5cf..f19dbc5 100644 --- a/glib/gmem.c +++ b/glib/gmem.c @@ -623,7 +623,7 @@ static guint *profile_data = NULL; static gsize profile_allocs = 0; static gsize profile_zinit = 0; static gsize profile_frees = 0; -static GMutex gmem_profile_mutex = G_MUTEX_INIT; +static GMutex gmem_profile_mutex; #ifdef G_ENABLE_DEBUG static volatile gsize g_trap_free_size = 0; static volatile gsize g_trap_realloc_size = 0; diff --git a/glib/gmessages.c b/glib/gmessages.c index acd55c7..c583188 100644 --- a/glib/gmessages.c +++ b/glib/gmessages.c @@ -101,7 +101,7 @@ struct _GLogHandler /* --- variables --- */ -static GMutex g_messages_lock = G_MUTEX_INIT; +static GMutex g_messages_lock; static GLogDomain *g_log_domains = NULL; static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK; static GPrintFunc glib_print_func = NULL; diff --git a/glib/gslice.c b/glib/gslice.c index 11a297c..4f34fa9 100644 --- a/glib/gslice.c +++ b/glib/gslice.c @@ -212,7 +212,7 @@ static SliceConfig slice_config = { 15 * 1000, /* working_set_msecs */ 1, /* color increment, alt: 0x7fffffff */ }; -static GMutex smc_tree_mutex = G_MUTEX_INIT; /* mutex for G_SLICE=debug-blocks */ +static GMutex smc_tree_mutex; /* mutex for G_SLICE=debug-blocks */ /* --- auxiliary funcitons --- */ void diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c index f2c35ae..dfb2b91 100644 --- a/glib/gthread-posix.c +++ b/glib/gthread-posix.c @@ -139,8 +139,7 @@ g_mutex_get_impl (GMutex *mutex) * This function is useful to initialize a mutex that has been * allocated on the stack, or as part of a larger structure. * It is not necessary to initialize a mutex that has been - * created with g_mutex_new(). Also see #G_MUTEX_INIT for an - * alternative way to initialize statically allocated mutexes. + * created with g_mutex_new() or that has been statically allocated. * * |[ * typedef struct { @@ -317,9 +316,8 @@ g_rec_mutex_get_impl (GRecMutex *rec_mutex) * that has been allocated on the stack, or as part of a larger * structure. * It is not necessary to initialize a recursive mutex that has - * been created with g_rec_mutex_new(). Also see #G_REC_MUTEX_INIT - * for an alternative way to initialize statically allocated - * recursive mutexes. + * been created with g_rec_mutex_new(). It is not necessary to + * initialise a recursive mutex that has been statically allocated. * * |[ * typedef struct { @@ -475,9 +473,9 @@ g_rw_lock_get_impl (GRWLock *lock) * Initializes a #GRWLock so that it can be used. * * This function is useful to initialize a lock that has been - * allocated on the stack, or as part of a larger structure. - * Also see #G_RW_LOCK_INIT for an alternative way to initialize - * statically allocated locks. + * allocated on the stack, or as part of a larger structure. It is not + * necessary to initialise a reader-writer lock that has been statically + * allocated. * * |[ * typedef struct { @@ -683,8 +681,7 @@ g_cond_get_impl (GCond *cond) * This function is useful to initialize a #GCond that has been * allocated on the stack, or as part of a larger structure. * It is not necessary to initialize a #GCond that has been - * created with g_cond_new(). Also see #G_COND_INIT for an - * alternative way to initialize statically allocated #GConds. + * created with g_cond_new() or that has been statically allocated. * * To undo the effect of g_cond_init() when a #GCond is no longer * needed, use g_cond_clear(). diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c index 320dda7..5d6d867 100644 --- a/glib/gthread-win32.c +++ b/glib/gthread-win32.c @@ -71,13 +71,8 @@ g_thread_abort (gint status, * that only get the kernel involved in cases of contention (similar * to how futex()-based mutexes work on Linux). The biggest advantage * of these new types is that they can be statically initialised to - * zero. This allows us to use them directly and still support: - * - * GMutex mutex = G_MUTEX_INIT; - * - * and - * - * GCond cond = G_COND_INIT; + * zero. That means that they are completely ABI compatible with our + * GMutex and GCond APIs. * * Unfortunately, Windows XP lacks these facilities and GLib still * needs to support Windows XP. Our approach here is as follows: diff --git a/glib/gthread.c b/glib/gthread.c index fcac82a..86f1a21 100644 --- a/glib/gthread.c +++ b/glib/gthread.c @@ -298,7 +298,7 @@ * int * give_me_next_number (void) * { - * static GMutex mutex = G_MUTEX_INIT; + * static GMutex mutex; * static int current_number = 0; * int ret_val; * @@ -315,19 +315,6 @@ * functions. */ -/** - * G_MUTEX_INIT: - * - * Initializer for statically allocated #GMutexes. - * Alternatively, g_mutex_init() can be used. - * - * |[ - * GMutex mutex = G_MUTEX_INIT; - * ]| - * - * Since: 2.32 - */ - /* GRecMutex Documentation {{{1 -------------------------------------- */ /** @@ -340,22 +327,7 @@ * unlock the recursive mutex as often as it has been locked. * * A GRecMutex should only be accessed with the - * g_rec_mutex_ functions. Before a GRecMutex - * can be used, it has to be initialized with #G_REC_MUTEX_INIT or - * g_rec_mutex_init(). - * - * Since: 2.32 - */ - -/** - * G_REC_MUTEX_INIT: - * - * Initializer for statically allocated #GRecMutexes. - * Alternatively, g_rec_mutex_init() can be used. - * - * |[ - * GRecMutex mutex = G_REC_MUTEX_INIT; - * ]| + * g_rec_mutex_ functions. * * Since: 2.32 */ @@ -379,7 +351,7 @@ * * An array with access functions * - * GRWLock lock = G_RW_LOCK_INIT; + * GRWLock lock; * GPtrArray *array; * * gpointer @@ -425,21 +397,7 @@ * * * A GRWLock should only be accessed with the - * g_rw_lock_ functions. Before it can be used, - * it has to be initialized with #G_RW_LOCK_INIT or g_rw_lock_init(). - * - * Since: 2.32 - */ - -/** - * G_RW_LOCK_INIT: - * - * Initializer for statically allocated #GRWLocks. - * Alternatively, g_rw_lock_init_init() can be used. - * - * |[ - * GRWLock lock = G_RW_LOCK_INIT; - * ]| + * g_rw_lock_ functions. * * Since: 2.32 */ @@ -507,19 +465,6 @@ * functions. */ -/** - * G_COND_INIT: - * - * Initializer for statically allocated #GConds. - * Alternatively, g_cond_init() can be used. - * - * |[ - * GCond cond = G_COND_INIT; - * ]| - * - * Since: 2.32 - */ - /* GThread Documentation {{{1 ---------------------------------------- */ /** @@ -578,8 +523,8 @@ g_thread_error_quark (void) gboolean g_threads_got_initialized = FALSE; GSystemThread zero_thread; /* This is initialized to all zero */ -GMutex g_once_mutex = G_MUTEX_INIT; -static GCond g_once_cond = G_COND_INIT; +GMutex g_once_mutex; +static GCond g_once_cond; static GSList *g_once_init_list = NULL; static void g_thread_cleanup (gpointer data); diff --git a/glib/gthread.h b/glib/gthread.h index 4ef6bab..495eaa0 100644 --- a/glib/gthread.h +++ b/glib/gthread.h @@ -59,25 +59,21 @@ typedef struct _GCond GCond; typedef struct _GPrivate GPrivate; typedef struct _GStaticPrivate GStaticPrivate; -#define G_MUTEX_INIT { NULL } struct _GMutex { gpointer impl; }; -#define G_RW_LOCK_INIT { NULL } struct _GRWLock { gpointer impl; }; -#define G_COND_INIT { NULL } struct _GCond { gpointer impl; }; -#define G_REC_MUTEX_INIT { NULL } struct _GRecMutex { gpointer impl; @@ -168,8 +164,7 @@ g_once_init_enter (volatile gsize *value_location) #define G_LOCK_NAME(name) g__ ## name ## _lock #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name) -#define G_LOCK_DEFINE(name) \ - GMutex G_LOCK_NAME (name) = G_MUTEX_INIT +#define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name) #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name) #ifdef G_DEBUG_LOCKS diff --git a/glib/gvarianttypeinfo.c b/glib/gvarianttypeinfo.c index 6ac37d5..0c380ce 100644 --- a/glib/gvarianttypeinfo.c +++ b/glib/gvarianttypeinfo.c @@ -709,7 +709,7 @@ g_variant_type_info_member_info (GVariantTypeInfo *info, } /* == new/ref/unref == */ -static GRecMutex g_variant_type_info_lock = G_REC_MUTEX_INIT; +static GRecMutex g_variant_type_info_lock; static GHashTable *g_variant_type_info_table; /* < private > diff --git a/glib/tests/cond.c b/glib/tests/cond.c index e10f982..ac9e8dc 100644 --- a/glib/tests/cond.c +++ b/glib/tests/cond.c @@ -22,8 +22,8 @@ #include -static GCond cond = G_COND_INIT; -static GMutex mutex = G_MUTEX_INIT; +static GCond cond; +static GMutex mutex; static volatile gint next; static void diff --git a/glib/tests/mutex.c b/glib/tests/mutex.c index 66ec5fb..17959c2 100644 --- a/glib/tests/mutex.c +++ b/glib/tests/mutex.c @@ -40,13 +40,12 @@ test_mutex1 (void) static void test_mutex2 (void) { - GMutex mutex = G_MUTEX_INIT; + static GMutex mutex; g_mutex_lock (&mutex); g_mutex_unlock (&mutex); g_mutex_lock (&mutex); g_mutex_unlock (&mutex); - g_mutex_clear (&mutex); } static void @@ -65,7 +64,7 @@ test_mutex3 (void) static void test_mutex4 (void) { - GMutex mutex = G_MUTEX_INIT; + static GMutex mutex; gboolean ret; ret = g_mutex_trylock (&mutex); @@ -76,7 +75,6 @@ test_mutex4 (void) g_mutex_unlock (&mutex); g_mutex_unlock (&mutex); - g_mutex_clear (&mutex); } #define LOCKS 48 @@ -159,7 +157,7 @@ test_mutex5 (void) static gboolean do_addition (gint *value) { - static GMutex lock = G_MUTEX_INIT; + static GMutex lock; gboolean more; /* test performance of "good" cases (ie: short critical sections) */ diff --git a/glib/tests/private.c b/glib/tests/private.c index 9b5b8ea..e3e392f 100644 --- a/glib/tests/private.c +++ b/glib/tests/private.c @@ -253,9 +253,9 @@ test_static_private4 (void) } static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT; -static GMutex m5 = G_MUTEX_INIT; -static GCond c5a = G_COND_INIT; -static GCond c5b = G_COND_INIT; +static GMutex m5; +static GCond c5a; +static GCond c5b; static gint count5; static gpointer diff --git a/glib/tests/rec-mutex.c b/glib/tests/rec-mutex.c index 9ade360..c8d5a35 100644 --- a/glib/tests/rec-mutex.c +++ b/glib/tests/rec-mutex.c @@ -38,19 +38,18 @@ test_rec_mutex1 (void) static void test_rec_mutex2 (void) { - GRecMutex mutex = G_REC_MUTEX_INIT; + static GRecMutex mutex; g_rec_mutex_lock (&mutex); g_rec_mutex_unlock (&mutex); g_rec_mutex_lock (&mutex); g_rec_mutex_unlock (&mutex); - g_rec_mutex_clear (&mutex); } static void test_rec_mutex3 (void) { - GRecMutex mutex = G_REC_MUTEX_INIT; + static GRecMutex mutex; gboolean ret; ret = g_rec_mutex_trylock (&mutex); @@ -61,7 +60,6 @@ test_rec_mutex3 (void) g_rec_mutex_unlock (&mutex); g_rec_mutex_unlock (&mutex); - g_rec_mutex_clear (&mutex); } #define LOCKS 48 diff --git a/glib/tests/rwlock.c b/glib/tests/rwlock.c index 63946e4..d3d0f9d 100644 --- a/glib/tests/rwlock.c +++ b/glib/tests/rwlock.c @@ -38,19 +38,18 @@ test_rwlock1 (void) static void test_rwlock2 (void) { - GRWLock lock = G_RW_LOCK_INIT; + static GRWLock lock; g_rw_lock_writer_lock (&lock); g_rw_lock_writer_unlock (&lock); g_rw_lock_writer_lock (&lock); g_rw_lock_writer_unlock (&lock); - g_rw_lock_clear (&lock); } static void test_rwlock3 (void) { - GRWLock lock = G_RW_LOCK_INIT; + static GRWLock lock; gboolean ret; ret = g_rw_lock_writer_trylock (&lock); @@ -59,26 +58,23 @@ test_rwlock3 (void) g_assert (!ret); g_rw_lock_writer_unlock (&lock); - - g_rw_lock_clear (&lock); } static void test_rwlock4 (void) { - GRWLock lock = G_RW_LOCK_INIT; + static GRWLock lock; g_rw_lock_reader_lock (&lock); g_rw_lock_reader_unlock (&lock); g_rw_lock_reader_lock (&lock); g_rw_lock_reader_unlock (&lock); - g_rw_lock_clear (&lock); } static void test_rwlock5 (void) { - GRWLock lock = G_RW_LOCK_INIT; + static GRWLock lock; gboolean ret; ret = g_rw_lock_reader_trylock (&lock); @@ -88,14 +84,12 @@ test_rwlock5 (void) g_rw_lock_reader_unlock (&lock); g_rw_lock_reader_unlock (&lock); - - g_rw_lock_clear (&lock); } static void test_rwlock6 (void) { - GRWLock lock = G_RW_LOCK_INIT; + static GRWLock lock; gboolean ret; g_rw_lock_writer_lock (&lock); @@ -107,8 +101,6 @@ test_rwlock6 (void) ret = g_rw_lock_writer_trylock (&lock); g_assert (!ret); g_rw_lock_reader_unlock (&lock); - - g_rw_lock_clear (&lock); } diff --git a/gmodule/gmodule.c b/gmodule/gmodule.c index c1a6486..e4ef3eb 100644 --- a/gmodule/gmodule.c +++ b/gmodule/gmodule.c @@ -326,7 +326,7 @@ _g_module_debug_init (void) module_debug_initialized = TRUE; } -static GRecMutex g_module_global_lock = G_REC_MUTEX_INIT; +static GRecMutex g_module_global_lock; GModule* g_module_open (const gchar *file_name, diff --git a/gobject/gparam.c b/gobject/gparam.c index ec4cb89..22f72b2 100644 --- a/gobject/gparam.c +++ b/gobject/gparam.c @@ -891,7 +891,7 @@ param_spec_pool_equals (gconstpointer key_spec_1, GParamSpecPool* g_param_spec_pool_new (gboolean type_prefixing) { - static GMutex init_mutex = G_MUTEX_INIT; + static GMutex init_mutex; GParamSpecPool *pool = g_new (GParamSpecPool, 1); memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex)); diff --git a/gobject/gtype.c b/gobject/gtype.c index da16240..a966200 100644 --- a/gobject/gtype.c +++ b/gobject/gtype.c @@ -368,8 +368,8 @@ typedef struct { /* --- variables --- */ -static GRWLock type_rw_lock = G_RW_LOCK_INIT; -static GRecMutex class_init_rec_mutex = G_REC_MUTEX_INIT; +static GRWLock type_rw_lock; +static GRecMutex class_init_rec_mutex; static guint static_n_class_cache_funcs = 0; static ClassCacheFunc *static_class_cache_funcs = NULL; static guint static_n_iface_check_funcs = 0; diff --git a/tests/thread-test.c b/tests/thread-test.c index fa4f02b..0f41fc7 100644 --- a/tests/thread-test.c +++ b/tests/thread-test.c @@ -110,7 +110,7 @@ test_g_static_rec_mutex (void) static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT; static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT; -static GMutex test_g_static_private_mutex = G_MUTEX_INIT; +static GMutex test_g_static_private_mutex; static guint test_g_static_private_counter = 0; static guint test_g_static_private_ready = 0; -- 2.7.4