Remove g_mutex_new()/g_cond_new() in testcases

These were the last users of the dynamic allocation API.

Keep the uses in glib/tests/mutex.c since this is actually meant to test
the API (which has to continue working, even if it is deprecated).

https://bugzilla.gnome.org/show_bug.cgi?id=660739
This commit is contained in:
Ryan Lortie 2011-10-04 19:04:41 -04:00
parent 9793708931
commit 6f343ca548
10 changed files with 97 additions and 115 deletions

View File

@ -52,24 +52,19 @@ opened_for_read (GObject *source, GAsyncResult *result, gpointer loop)
static gboolean idle_start_test1_thread (gpointer loop);
static gpointer test1_thread (gpointer user_data);
static GCond *test1_cond;
static GMutex *test1_mutex;
static gboolean test1_done;
static GCond test1_cond;
static GMutex test1_mutex;
static void
test_thread_independence (void)
{
GMainLoop *loop;
test1_cond = g_cond_new ();
test1_mutex = g_mutex_new ();
loop = g_main_loop_new (NULL, FALSE);
g_idle_add (idle_start_test1_thread, loop);
g_main_loop_run (loop);
g_main_loop_unref (loop);
g_mutex_free (test1_mutex);
g_cond_free (test1_cond);
}
static gboolean
@ -79,16 +74,19 @@ idle_start_test1_thread (gpointer loop)
GThread *thread;
gboolean io_completed;
g_mutex_lock (test1_mutex);
g_mutex_lock (&test1_mutex);
thread = g_thread_create (test1_thread, NULL, TRUE, NULL);
g_get_current_time (&time);
time.tv_sec += 2;
io_completed = g_cond_timed_wait (test1_cond, test1_mutex, &time);
g_assert (io_completed);
while (!test1_done)
{
io_completed = g_cond_timed_wait (&test1_cond, &test1_mutex, &time);
g_assert (io_completed);
}
g_thread_join (thread);
g_mutex_unlock (test1_mutex);
g_mutex_unlock (&test1_mutex);
g_main_loop_quit (loop);
return FALSE;
}
@ -101,8 +99,7 @@ test1_thread (gpointer user_data)
GFile *file;
/* Wait for main thread to be waiting on test1_cond */
g_mutex_lock (test1_mutex);
g_mutex_unlock (test1_mutex);
g_mutex_lock (&test1_mutex);
context = g_main_context_new ();
g_assert (g_main_context_get_thread_default () == NULL);
@ -119,7 +116,10 @@ test1_thread (gpointer user_data)
g_main_loop_run (loop);
g_main_loop_unref (loop);
g_cond_signal (test1_cond);
test1_done = TRUE;
g_cond_signal (&test1_cond);
g_mutex_unlock (&test1_mutex);
return NULL;
}

View File

@ -391,8 +391,9 @@ teardown_without_loop (Test *test,
}
typedef struct {
GMutex *loop_mutex;
GCond *loop_started;
GMutex loop_mutex;
GCond loop_started;
gboolean started;
Test *test;
} ThreadLoop;
@ -403,15 +404,16 @@ thread_loop (gpointer user_data)
ThreadLoop *closure = user_data;
Test *test = closure->test;
g_mutex_lock (closure->loop_mutex);
g_mutex_lock (&closure->loop_mutex);
g_assert (test->loop_thread == g_thread_self ());
g_assert (test->loop == NULL);
test->loop = g_main_loop_new (context, TRUE);
g_main_context_acquire (context);
g_cond_signal (closure->loop_started);
g_mutex_unlock (closure->loop_mutex);
closure->started = TRUE;
g_cond_signal (&closure->loop_started);
g_mutex_unlock (&closure->loop_mutex);
while (g_main_loop_is_running (test->loop))
g_main_context_iteration (context, TRUE);
@ -429,14 +431,16 @@ setup_with_thread_loop (Test *test,
setup_without_loop (test, user_data);
closure.loop_mutex = g_mutex_new ();
closure.loop_started = g_cond_new ();
g_mutex_init (&closure.loop_mutex);
g_cond_init (&closure.loop_started);
closure.started = FALSE;
closure.test = test;
g_mutex_lock (closure.loop_mutex);
g_mutex_lock (&closure.loop_mutex);
test->loop_thread = g_thread_create (thread_loop, &closure, TRUE, &error);
g_cond_wait (closure.loop_started, closure.loop_mutex);
g_mutex_unlock (closure.loop_mutex);
while (!closure.started)
g_cond_wait (&closure.loop_started, &closure.loop_mutex);
g_mutex_unlock (&closure.loop_mutex);
/*
* When a loop is running then interaction should always occur in the main
@ -444,8 +448,8 @@ setup_with_thread_loop (Test *test,
*/
test->interaction_thread = test->loop_thread;
g_mutex_free (closure.loop_mutex);
g_cond_free (closure.loop_started);
g_mutex_clear (&closure.loop_mutex);
g_cond_clear (&closure.loop_started);
}
static void

View File

@ -25,7 +25,7 @@
/* This test tests the macros for defining dynamic types.
*/
static GMutex *sync_mutex = NULL;
static GMutex sync_mutex;
static gboolean loaded = FALSE;
/* MODULE */
@ -169,8 +169,8 @@ ref_unref_thread (gpointer data)
*/
if (g_test_verbose())
g_print ("WAITING!\n");
g_mutex_lock (sync_mutex);
g_mutex_unlock (sync_mutex);
g_mutex_lock (&sync_mutex);
g_mutex_unlock (&sync_mutex);
if (g_test_verbose ())
g_print ("STARTING\n");
@ -206,7 +206,7 @@ test_multithreaded_dynamic_type_init (void)
g_assert (!loaded);
/* pause newly created threads */
g_mutex_lock (sync_mutex);
g_mutex_lock (&sync_mutex);
/* create threads */
for (i = 0; i < N_THREADS; i++) {
@ -214,7 +214,7 @@ test_multithreaded_dynamic_type_init (void)
}
/* execute threads */
g_mutex_unlock (sync_mutex);
g_mutex_unlock (&sync_mutex);
for (i = 0; i < N_THREADS; i++) {
g_thread_join (threads[i]);
@ -356,8 +356,6 @@ main (int argc,
g_test_init (&argc, &argv, NULL);
g_type_init ();
sync_mutex = g_mutex_new();
g_test_add_func ("/GObject/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);
g_test_add_func ("/GObject/dynamic-interface-properties", test_dynamic_interface_properties);

View File

@ -79,8 +79,8 @@ G_DEFINE_TYPE_WITH_CODE (MyTester2, my_tester2, G_TYPE_OBJECT,
static void my_tester2_init (MyTester2*t) {}
static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
static GCond *sync_cond = NULL;
static GMutex *sync_mutex = NULL;
static GCond sync_cond;
static GMutex sync_mutex;
static gpointer
tester_init_thread (gpointer data)
@ -91,8 +91,8 @@ tester_init_thread (gpointer data)
* then run interface and class initializers,
* using unsafe_call_counter concurrently
*/
g_mutex_lock (sync_mutex);
g_mutex_unlock (sync_mutex);
g_mutex_lock (&sync_mutex);
g_mutex_unlock (&sync_mutex);
/* test default interface initialization for face0 */
g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
/* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
@ -108,13 +108,13 @@ static void
test_threaded_class_init (void)
{
/* pause newly created threads */
g_mutex_lock (sync_mutex);
g_mutex_lock (&sync_mutex);
/* create threads */
g_thread_create (tester_init_thread, (gpointer) my_tester0_get_type(), TRUE, NULL);
g_thread_create (tester_init_thread, (gpointer) my_tester1_get_type(), TRUE, NULL);
g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
/* execute threads */
g_mutex_unlock (sync_mutex);
g_mutex_unlock (&sync_mutex);
while (g_atomic_int_get (&mtsafe_call_counter) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS)
{
if (g_test_verbose())
@ -155,9 +155,9 @@ prop_tester_class_init (PropTesterClass *c)
gobject_class->set_property = prop_tester_set_property; /* silence GObject checks */
g_mutex_lock (sync_mutex);
g_cond_signal (sync_cond);
g_mutex_unlock (sync_mutex);
g_mutex_lock (&sync_mutex);
g_cond_signal (&sync_cond);
g_mutex_unlock (&sync_mutex);
for (i = 0; i < 100; i++) /* wait a bit. */
g_thread_yield();
@ -184,14 +184,14 @@ static void
test_threaded_object_init (void)
{
GThread *creator;
g_mutex_lock (sync_mutex);
g_mutex_lock (&sync_mutex);
creator = g_thread_create (object_create, NULL, TRUE, NULL);
/* really provoke the race */
g_cond_wait (sync_cond, sync_mutex);
g_cond_wait (&sync_cond, &sync_mutex);
object_create (NULL);
g_mutex_unlock (sync_mutex);
g_mutex_unlock (&sync_mutex);
g_thread_join (creator);
}
@ -204,9 +204,6 @@ main (int argc,
g_test_init (&argc, &argv, NULL);
g_type_init ();
sync_cond = g_cond_new();
sync_mutex = g_mutex_new();
g_test_add_func ("/GObject/threaded-class-init", test_threaded_class_init);
g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);

View File

@ -27,8 +27,8 @@
* be locked while the context array mutex is locked
*/
GPtrArray *context_array;
GMutex *context_array_mutex;
GCond *context_array_cond;
GMutex context_array_mutex;
GCond context_array_cond;
GMainLoop *main_loop;
@ -144,14 +144,14 @@ adder_thread (gpointer data)
context = g_main_context_new ();
g_mutex_lock (context_array_mutex);
g_mutex_lock (&context_array_mutex);
g_ptr_array_add (context_array, context);
if (context_array->len == NTHREADS)
g_cond_broadcast (context_array_cond);
g_cond_broadcast (&context_array_cond);
g_mutex_unlock (context_array_mutex);
g_mutex_unlock (&context_array_mutex);
addr_data.dest = channels[1];
addr_data.loop = g_main_loop_new (context, FALSE);
@ -183,11 +183,11 @@ adder_thread (gpointer data)
g_print ("Timeout run %d times\n", addr_data.count);
#endif
g_mutex_lock (context_array_mutex);
g_mutex_lock (&context_array_mutex);
g_ptr_array_remove (context_array, context);
if (context_array->len == 0)
g_main_loop_quit (main_loop);
g_mutex_unlock (context_array_mutex);
g_mutex_unlock (&context_array_mutex);
cleanup_crawlers (context);
@ -343,10 +343,10 @@ create_crawler (void)
G_LOCK (crawler_array_lock);
g_ptr_array_add (crawler_array, source);
g_mutex_lock (context_array_mutex);
g_mutex_lock (&context_array_mutex);
g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
g_source_unref (source);
g_mutex_unlock (context_array_mutex);
g_mutex_unlock (&context_array_mutex);
G_UNLOCK (crawler_array_lock);
}
@ -386,14 +386,14 @@ recurser_start (gpointer data)
GMainContext *context;
GSource *source;
g_mutex_lock (context_array_mutex);
g_mutex_lock (&context_array_mutex);
context = context_array->pdata[g_random_int_range (0, context_array->len)];
source = g_idle_source_new ();
g_source_set_name (source, "Recursing idle source");
g_source_set_callback (source, recurser_idle, context, NULL);
g_source_attach (source, context);
g_source_unref (source);
g_mutex_unlock (context_array_mutex);
g_mutex_unlock (&context_array_mutex);
return TRUE;
}
@ -407,8 +407,6 @@ main (int argc,
g_thread_init (NULL);
context_array = g_ptr_array_new ();
context_array_mutex = g_mutex_new ();
context_array_cond = g_cond_new ();
crawler_array = g_ptr_array_new ();
@ -419,12 +417,12 @@ main (int argc,
/* Wait for all threads to start
*/
g_mutex_lock (context_array_mutex);
g_mutex_lock (&context_array_mutex);
if (context_array->len < NTHREADS)
g_cond_wait (context_array_cond, context_array_mutex);
g_cond_wait (&context_array_cond, &context_array_mutex);
g_mutex_unlock (context_array_mutex);
g_mutex_unlock (&context_array_mutex);
for (i = 0; i < NCRAWLERS; i++)
create_crawler ();

View File

@ -50,7 +50,6 @@ static guint mem_chunk_recursion = 0;
# define LEAVE_MEM_CHUNK_ROUTINE() (mem_chunk_recursion = MEM_CHUNK_ROUTINE_COUNT () - 1)
/* --- old memchunk prototypes --- */
void old_mem_chunks_init (void);
GMemChunk* old_mem_chunk_new (const gchar *name,
gint atom_size,
gulong area_size,
@ -127,15 +126,9 @@ static gint old_mem_chunk_area_search (GMemArea *a,
/* here we can't use StaticMutexes, as they depend upon a working
* g_malloc, the same holds true for StaticPrivate
*/
static GMutex *mem_chunks_lock = NULL;
static GMutex mem_chunks_lock;
static GMemChunk *mem_chunks = NULL;
void
old_mem_chunks_init (void)
{
mem_chunks_lock = g_mutex_new ();
}
GMemChunk*
old_mem_chunk_new (const gchar *name,
gint atom_size,
@ -175,13 +168,13 @@ old_mem_chunk_new (const gchar *name,
rarea_size = old_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
g_mutex_lock (mem_chunks_lock);
g_mutex_lock (&mem_chunks_lock);
mem_chunk->next = mem_chunks;
mem_chunk->prev = NULL;
if (mem_chunks)
mem_chunks->prev = mem_chunk;
mem_chunks = mem_chunk;
g_mutex_unlock (mem_chunks_lock);
g_mutex_unlock (&mem_chunks_lock);
LEAVE_MEM_CHUNK_ROUTINE ();
@ -206,7 +199,7 @@ old_mem_chunk_destroy (GMemChunk *mem_chunk)
g_free (temp_area);
}
g_mutex_lock (mem_chunks_lock);
g_mutex_lock (&mem_chunks_lock);
if (mem_chunk->next)
mem_chunk->next->prev = mem_chunk->prev;
if (mem_chunk->prev)
@ -214,7 +207,7 @@ old_mem_chunk_destroy (GMemChunk *mem_chunk)
if (mem_chunk == mem_chunks)
mem_chunks = mem_chunks->next;
g_mutex_unlock (mem_chunks_lock);
g_mutex_unlock (&mem_chunks_lock);
if (mem_chunk->type == G_ALLOC_AND_FREE)
g_tree_destroy (mem_chunk->mem_tree);
@ -545,20 +538,20 @@ old_mem_chunk_info (void)
gint count;
count = 0;
g_mutex_lock (mem_chunks_lock);
g_mutex_lock (&mem_chunks_lock);
mem_chunk = mem_chunks;
while (mem_chunk)
{
count += 1;
mem_chunk = mem_chunk->next;
}
g_mutex_unlock (mem_chunks_lock);
g_mutex_unlock (&mem_chunks_lock);
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%d mem chunks", count);
g_mutex_lock (mem_chunks_lock);
g_mutex_lock (&mem_chunks_lock);
mem_chunk = mem_chunks;
g_mutex_unlock (mem_chunks_lock);
g_mutex_unlock (&mem_chunks_lock);
while (mem_chunk)
{

View File

@ -23,8 +23,8 @@
#define N_THREADS (13)
static GMutex *tmutex = NULL;
static GCond *tcond = NULL;
static GMutex tmutex;
static GCond tcond;
static volatile int thread_call_count = 0;
static char dummy_value = 'x';
@ -96,9 +96,9 @@ initializer3 (void)
static gpointer
tmain_call_initializer3 (gpointer user_data)
{
g_mutex_lock (tmutex);
g_cond_wait (tcond, tmutex);
g_mutex_unlock (tmutex);
g_mutex_lock (&tmutex);
g_cond_wait (&tcond, &tmutex);
g_mutex_unlock (&tmutex);
//g_printf ("[");
initializer3();
//g_printf ("]\n");
@ -124,15 +124,13 @@ main (int argc,
g_assert (p == &dummy_value);
/* setup threads */
g_thread_init (NULL);
tmutex = g_mutex_new ();
tcond = g_cond_new ();
/* start multiple threads for initializer3() */
g_mutex_lock (tmutex);
g_mutex_lock (&tmutex);
for (i = 0; i < N_THREADS; i++)
threads[i] = g_thread_create (tmain_call_initializer3, 0, FALSE, NULL);
g_mutex_unlock (tmutex);
g_mutex_unlock (&tmutex);
/* concurrently call initializer3() */
g_cond_broadcast (tcond);
g_cond_broadcast (&tcond);
/* loop until all threads passed the call to initializer3() */
while (g_atomic_int_get (&thread_call_count) < i)
{
@ -140,14 +138,14 @@ main (int argc,
g_thread_yield(); /* concurrent shuffling for single core */
else
g_usleep (1000); /* concurrent shuffling for multi core */
g_cond_broadcast (tcond);
g_cond_broadcast (&tcond);
}
/* call multiple (unoptimized) initializers from multiple threads */
g_mutex_lock (tmutex);
g_mutex_lock (&tmutex);
g_atomic_int_set (&thread_call_count, 0);
for (i = 0; i < N_THREADS; i++)
g_thread_create (stress_concurrent_initializers, 0, FALSE, NULL);
g_mutex_unlock (tmutex);
g_mutex_unlock (&tmutex);
while (g_atomic_int_get (&thread_call_count) < 256 * 4 * N_THREADS)
g_usleep (50 * 1000); /* wait for all 5 threads to complete */
return 0;
@ -157,7 +155,8 @@ main (int argc,
* to uncover possible races in the g_once_init_enter_impl()/
* g_once_init_leave() implementations
*/
#define g_once_init_enter g_once_init_enter_impl
#undef g_once_init_enter
#undef g_once_init_leave
/* define 16 * 16 simple initializers */
#define DEFINE_TEST_INITIALIZER(N) \
@ -262,8 +261,8 @@ stress_concurrent_initializers (void *user_data)
};
int i;
/* sync to main thread */
g_mutex_lock (tmutex);
g_mutex_unlock (tmutex);
g_mutex_lock (&tmutex);
g_mutex_unlock (&tmutex);
/* initialize concurrently */
for (i = 0; i < G_N_ELEMENTS (initializers); i++)
{

View File

@ -30,7 +30,7 @@ struct ThreadData
int thread_id;
GThread* gthread;
GMutex* to_free_mutex;
GMutex to_free_mutex;
void* to_free [N_THREADS * N_ALLOCS];
int bytes_to_free [N_THREADS * N_ALLOCS];
int n_to_free;
@ -57,11 +57,11 @@ thread_func (void *arg)
/* associate block with random thread */
int t = rand() % N_THREADS;
g_mutex_lock (tdata[t].to_free_mutex);
g_mutex_lock (&tdata[t].to_free_mutex);
tdata[t].to_free[tdata[t].n_to_free] = mem;
tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes;
tdata[t].n_to_free++;
g_mutex_unlock (tdata[t].to_free_mutex);
g_mutex_unlock (&tdata[t].to_free_mutex);
/* shuffle thread execution order every once in a while */
if (rand() % 97 == 0)
@ -73,14 +73,14 @@ thread_func (void *arg)
}
/* free a block associated with this thread */
g_mutex_lock (td->to_free_mutex);
g_mutex_lock (&td->to_free_mutex);
if (td->n_to_free > 0)
{
td->n_to_free--;
g_slice_free1 (td->bytes_to_free[td->n_to_free], td->to_free[td->n_to_free]);
td->n_freed++;
}
g_mutex_unlock (td->to_free_mutex);
g_mutex_unlock (&td->to_free_mutex);
}
return NULL;
@ -91,14 +91,11 @@ main()
{
int t;
g_thread_init (NULL);
for (t = 0; t < N_THREADS; t++)
{
tdata[t].thread_id = t + 1;
tdata[t].n_to_free = 0;
tdata[t].n_freed = 0;
tdata[t].to_free_mutex = g_mutex_new();
}
g_print ("Starting %d threads for concurrent GSlice usage...\n", N_THREADS);
for (t = 0; t < N_THREADS; t++)

View File

@ -29,7 +29,6 @@ static guint number_of_repetitions = 10000; /* number of alloc+free repet
static gboolean want_corruption = FALSE;
/* --- old memchunk prototypes (memchunks.c) --- */
void old_mem_chunks_init (void);
GMemChunk* old_mem_chunk_new (const gchar *name,
gint atom_size,
gulong area_size,
@ -285,7 +284,6 @@ main (int argc,
threads[i] = g_thread_create (test_sliced_mem_thread, seedp, TRUE, NULL);
else
{
old_mem_chunks_init();
for (i = 0; i < n_threads; i++)
threads[i] = g_thread_create (test_memchunk_thread, seedp, TRUE, NULL);
}

View File

@ -5,7 +5,7 @@
/* GMutex */
static GMutex* test_g_mutex_mutex = NULL;
static GMutex test_g_mutex_mutex;
static guint test_g_mutex_int = 0;
static gboolean test_g_mutex_thread_ready;
G_LOCK_DEFINE_STATIC (test_g_mutex);
@ -14,12 +14,12 @@ static gpointer
test_g_mutex_thread (gpointer data)
{
g_assert (GPOINTER_TO_INT (data) == 42);
g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
g_assert (g_mutex_trylock (&test_g_mutex_mutex) == FALSE);
g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
test_g_mutex_thread_ready = TRUE;
g_mutex_lock (test_g_mutex_mutex);
g_mutex_lock (&test_g_mutex_mutex);
g_assert (test_g_mutex_int == 42);
g_mutex_unlock (test_g_mutex_mutex);
g_mutex_unlock (&test_g_mutex_mutex);
return GINT_TO_POINTER (41);
}
@ -28,9 +28,8 @@ static void
test_g_mutex (void)
{
GThread *thread;
test_g_mutex_mutex = g_mutex_new ();
g_assert (g_mutex_trylock (test_g_mutex_mutex));
g_assert (g_mutex_trylock (&test_g_mutex_mutex));
g_assert (G_TRYLOCK (test_g_mutex));
test_g_mutex_thread_ready = FALSE;
thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
@ -41,9 +40,8 @@ test_g_mutex (void)
g_usleep (G_USEC_PER_SEC / 5);
test_g_mutex_int = 42;
G_UNLOCK (test_g_mutex);
g_mutex_unlock (test_g_mutex_mutex);
g_mutex_unlock (&test_g_mutex_mutex);
g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
g_mutex_free (test_g_mutex_mutex);
}
/* GStaticRecMutex */