mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-20 15:48:54 +02:00
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:
@@ -52,24 +52,19 @@ opened_for_read (GObject *source, GAsyncResult *result, gpointer loop)
|
|||||||
static gboolean idle_start_test1_thread (gpointer loop);
|
static gboolean idle_start_test1_thread (gpointer loop);
|
||||||
static gpointer test1_thread (gpointer user_data);
|
static gpointer test1_thread (gpointer user_data);
|
||||||
|
|
||||||
static GCond *test1_cond;
|
static gboolean test1_done;
|
||||||
static GMutex *test1_mutex;
|
static GCond test1_cond;
|
||||||
|
static GMutex test1_mutex;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_thread_independence (void)
|
test_thread_independence (void)
|
||||||
{
|
{
|
||||||
GMainLoop *loop;
|
GMainLoop *loop;
|
||||||
|
|
||||||
test1_cond = g_cond_new ();
|
|
||||||
test1_mutex = g_mutex_new ();
|
|
||||||
|
|
||||||
loop = g_main_loop_new (NULL, FALSE);
|
loop = g_main_loop_new (NULL, FALSE);
|
||||||
g_idle_add (idle_start_test1_thread, loop);
|
g_idle_add (idle_start_test1_thread, loop);
|
||||||
g_main_loop_run (loop);
|
g_main_loop_run (loop);
|
||||||
g_main_loop_unref (loop);
|
g_main_loop_unref (loop);
|
||||||
|
|
||||||
g_mutex_free (test1_mutex);
|
|
||||||
g_cond_free (test1_cond);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -79,16 +74,19 @@ idle_start_test1_thread (gpointer loop)
|
|||||||
GThread *thread;
|
GThread *thread;
|
||||||
gboolean io_completed;
|
gboolean io_completed;
|
||||||
|
|
||||||
g_mutex_lock (test1_mutex);
|
g_mutex_lock (&test1_mutex);
|
||||||
thread = g_thread_create (test1_thread, NULL, TRUE, NULL);
|
thread = g_thread_create (test1_thread, NULL, TRUE, NULL);
|
||||||
|
|
||||||
g_get_current_time (&time);
|
g_get_current_time (&time);
|
||||||
time.tv_sec += 2;
|
time.tv_sec += 2;
|
||||||
io_completed = g_cond_timed_wait (test1_cond, test1_mutex, &time);
|
while (!test1_done)
|
||||||
g_assert (io_completed);
|
{
|
||||||
|
io_completed = g_cond_timed_wait (&test1_cond, &test1_mutex, &time);
|
||||||
|
g_assert (io_completed);
|
||||||
|
}
|
||||||
g_thread_join (thread);
|
g_thread_join (thread);
|
||||||
|
|
||||||
g_mutex_unlock (test1_mutex);
|
g_mutex_unlock (&test1_mutex);
|
||||||
g_main_loop_quit (loop);
|
g_main_loop_quit (loop);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
@@ -101,8 +99,7 @@ test1_thread (gpointer user_data)
|
|||||||
GFile *file;
|
GFile *file;
|
||||||
|
|
||||||
/* Wait for main thread to be waiting on test1_cond */
|
/* Wait for main thread to be waiting on test1_cond */
|
||||||
g_mutex_lock (test1_mutex);
|
g_mutex_lock (&test1_mutex);
|
||||||
g_mutex_unlock (test1_mutex);
|
|
||||||
|
|
||||||
context = g_main_context_new ();
|
context = g_main_context_new ();
|
||||||
g_assert (g_main_context_get_thread_default () == NULL);
|
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_run (loop);
|
||||||
g_main_loop_unref (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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -391,8 +391,9 @@ teardown_without_loop (Test *test,
|
|||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GMutex *loop_mutex;
|
GMutex loop_mutex;
|
||||||
GCond *loop_started;
|
GCond loop_started;
|
||||||
|
gboolean started;
|
||||||
Test *test;
|
Test *test;
|
||||||
} ThreadLoop;
|
} ThreadLoop;
|
||||||
|
|
||||||
@@ -403,15 +404,16 @@ thread_loop (gpointer user_data)
|
|||||||
ThreadLoop *closure = user_data;
|
ThreadLoop *closure = user_data;
|
||||||
Test *test = closure->test;
|
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_thread == g_thread_self ());
|
||||||
g_assert (test->loop == NULL);
|
g_assert (test->loop == NULL);
|
||||||
test->loop = g_main_loop_new (context, TRUE);
|
test->loop = g_main_loop_new (context, TRUE);
|
||||||
|
|
||||||
g_main_context_acquire (context);
|
g_main_context_acquire (context);
|
||||||
g_cond_signal (closure->loop_started);
|
closure->started = TRUE;
|
||||||
g_mutex_unlock (closure->loop_mutex);
|
g_cond_signal (&closure->loop_started);
|
||||||
|
g_mutex_unlock (&closure->loop_mutex);
|
||||||
|
|
||||||
while (g_main_loop_is_running (test->loop))
|
while (g_main_loop_is_running (test->loop))
|
||||||
g_main_context_iteration (context, TRUE);
|
g_main_context_iteration (context, TRUE);
|
||||||
@@ -429,14 +431,16 @@ setup_with_thread_loop (Test *test,
|
|||||||
|
|
||||||
setup_without_loop (test, user_data);
|
setup_without_loop (test, user_data);
|
||||||
|
|
||||||
closure.loop_mutex = g_mutex_new ();
|
g_mutex_init (&closure.loop_mutex);
|
||||||
closure.loop_started = g_cond_new ();
|
g_cond_init (&closure.loop_started);
|
||||||
|
closure.started = FALSE;
|
||||||
closure.test = test;
|
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);
|
test->loop_thread = g_thread_create (thread_loop, &closure, TRUE, &error);
|
||||||
g_cond_wait (closure.loop_started, closure.loop_mutex);
|
while (!closure.started)
|
||||||
g_mutex_unlock (closure.loop_mutex);
|
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
|
* 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;
|
test->interaction_thread = test->loop_thread;
|
||||||
|
|
||||||
g_mutex_free (closure.loop_mutex);
|
g_mutex_clear (&closure.loop_mutex);
|
||||||
g_cond_free (closure.loop_started);
|
g_cond_clear (&closure.loop_started);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@@ -25,7 +25,7 @@
|
|||||||
/* This test tests the macros for defining dynamic types.
|
/* This test tests the macros for defining dynamic types.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static GMutex *sync_mutex = NULL;
|
static GMutex sync_mutex;
|
||||||
static gboolean loaded = FALSE;
|
static gboolean loaded = FALSE;
|
||||||
|
|
||||||
/* MODULE */
|
/* MODULE */
|
||||||
@@ -169,8 +169,8 @@ ref_unref_thread (gpointer data)
|
|||||||
*/
|
*/
|
||||||
if (g_test_verbose())
|
if (g_test_verbose())
|
||||||
g_print ("WAITING!\n");
|
g_print ("WAITING!\n");
|
||||||
g_mutex_lock (sync_mutex);
|
g_mutex_lock (&sync_mutex);
|
||||||
g_mutex_unlock (sync_mutex);
|
g_mutex_unlock (&sync_mutex);
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
g_print ("STARTING\n");
|
g_print ("STARTING\n");
|
||||||
|
|
||||||
@@ -206,7 +206,7 @@ test_multithreaded_dynamic_type_init (void)
|
|||||||
g_assert (!loaded);
|
g_assert (!loaded);
|
||||||
|
|
||||||
/* pause newly created threads */
|
/* pause newly created threads */
|
||||||
g_mutex_lock (sync_mutex);
|
g_mutex_lock (&sync_mutex);
|
||||||
|
|
||||||
/* create threads */
|
/* create threads */
|
||||||
for (i = 0; i < N_THREADS; i++) {
|
for (i = 0; i < N_THREADS; i++) {
|
||||||
@@ -214,7 +214,7 @@ test_multithreaded_dynamic_type_init (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* execute threads */
|
/* execute threads */
|
||||||
g_mutex_unlock (sync_mutex);
|
g_mutex_unlock (&sync_mutex);
|
||||||
|
|
||||||
for (i = 0; i < N_THREADS; i++) {
|
for (i = 0; i < N_THREADS; i++) {
|
||||||
g_thread_join (threads[i]);
|
g_thread_join (threads[i]);
|
||||||
@@ -356,8 +356,6 @@ main (int argc,
|
|||||||
g_test_init (&argc, &argv, NULL);
|
g_test_init (&argc, &argv, NULL);
|
||||||
g_type_init ();
|
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/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);
|
||||||
g_test_add_func ("/GObject/dynamic-interface-properties", test_dynamic_interface_properties);
|
g_test_add_func ("/GObject/dynamic-interface-properties", test_dynamic_interface_properties);
|
||||||
|
|
||||||
|
@@ -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_init (MyTester2*t) {}
|
||||||
static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
|
static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
|
||||||
|
|
||||||
static GCond *sync_cond = NULL;
|
static GCond sync_cond;
|
||||||
static GMutex *sync_mutex = NULL;
|
static GMutex sync_mutex;
|
||||||
|
|
||||||
static gpointer
|
static gpointer
|
||||||
tester_init_thread (gpointer data)
|
tester_init_thread (gpointer data)
|
||||||
@@ -91,8 +91,8 @@ tester_init_thread (gpointer data)
|
|||||||
* then run interface and class initializers,
|
* then run interface and class initializers,
|
||||||
* using unsafe_call_counter concurrently
|
* using unsafe_call_counter concurrently
|
||||||
*/
|
*/
|
||||||
g_mutex_lock (sync_mutex);
|
g_mutex_lock (&sync_mutex);
|
||||||
g_mutex_unlock (sync_mutex);
|
g_mutex_unlock (&sync_mutex);
|
||||||
/* test default interface initialization for face0 */
|
/* test default interface initialization for face0 */
|
||||||
g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
|
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 */
|
/* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
|
||||||
@@ -108,13 +108,13 @@ static void
|
|||||||
test_threaded_class_init (void)
|
test_threaded_class_init (void)
|
||||||
{
|
{
|
||||||
/* pause newly created threads */
|
/* pause newly created threads */
|
||||||
g_mutex_lock (sync_mutex);
|
g_mutex_lock (&sync_mutex);
|
||||||
/* create threads */
|
/* create threads */
|
||||||
g_thread_create (tester_init_thread, (gpointer) my_tester0_get_type(), TRUE, NULL);
|
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_tester1_get_type(), TRUE, NULL);
|
||||||
g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
|
g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
|
||||||
/* execute threads */
|
/* 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)
|
while (g_atomic_int_get (&mtsafe_call_counter) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS)
|
||||||
{
|
{
|
||||||
if (g_test_verbose())
|
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 */
|
gobject_class->set_property = prop_tester_set_property; /* silence GObject checks */
|
||||||
|
|
||||||
g_mutex_lock (sync_mutex);
|
g_mutex_lock (&sync_mutex);
|
||||||
g_cond_signal (sync_cond);
|
g_cond_signal (&sync_cond);
|
||||||
g_mutex_unlock (sync_mutex);
|
g_mutex_unlock (&sync_mutex);
|
||||||
|
|
||||||
for (i = 0; i < 100; i++) /* wait a bit. */
|
for (i = 0; i < 100; i++) /* wait a bit. */
|
||||||
g_thread_yield();
|
g_thread_yield();
|
||||||
@@ -184,14 +184,14 @@ static void
|
|||||||
test_threaded_object_init (void)
|
test_threaded_object_init (void)
|
||||||
{
|
{
|
||||||
GThread *creator;
|
GThread *creator;
|
||||||
g_mutex_lock (sync_mutex);
|
g_mutex_lock (&sync_mutex);
|
||||||
|
|
||||||
creator = g_thread_create (object_create, NULL, TRUE, NULL);
|
creator = g_thread_create (object_create, NULL, TRUE, NULL);
|
||||||
/* really provoke the race */
|
/* really provoke the race */
|
||||||
g_cond_wait (sync_cond, sync_mutex);
|
g_cond_wait (&sync_cond, &sync_mutex);
|
||||||
|
|
||||||
object_create (NULL);
|
object_create (NULL);
|
||||||
g_mutex_unlock (sync_mutex);
|
g_mutex_unlock (&sync_mutex);
|
||||||
|
|
||||||
g_thread_join (creator);
|
g_thread_join (creator);
|
||||||
}
|
}
|
||||||
@@ -204,9 +204,6 @@ main (int argc,
|
|||||||
g_test_init (&argc, &argv, NULL);
|
g_test_init (&argc, &argv, NULL);
|
||||||
g_type_init ();
|
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-class-init", test_threaded_class_init);
|
||||||
g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);
|
g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);
|
||||||
|
|
||||||
|
@@ -27,8 +27,8 @@
|
|||||||
* be locked while the context array mutex is locked
|
* be locked while the context array mutex is locked
|
||||||
*/
|
*/
|
||||||
GPtrArray *context_array;
|
GPtrArray *context_array;
|
||||||
GMutex *context_array_mutex;
|
GMutex context_array_mutex;
|
||||||
GCond *context_array_cond;
|
GCond context_array_cond;
|
||||||
|
|
||||||
GMainLoop *main_loop;
|
GMainLoop *main_loop;
|
||||||
|
|
||||||
@@ -144,14 +144,14 @@ adder_thread (gpointer data)
|
|||||||
|
|
||||||
context = g_main_context_new ();
|
context = g_main_context_new ();
|
||||||
|
|
||||||
g_mutex_lock (context_array_mutex);
|
g_mutex_lock (&context_array_mutex);
|
||||||
|
|
||||||
g_ptr_array_add (context_array, context);
|
g_ptr_array_add (context_array, context);
|
||||||
|
|
||||||
if (context_array->len == NTHREADS)
|
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.dest = channels[1];
|
||||||
addr_data.loop = g_main_loop_new (context, FALSE);
|
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);
|
g_print ("Timeout run %d times\n", addr_data.count);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
g_mutex_lock (context_array_mutex);
|
g_mutex_lock (&context_array_mutex);
|
||||||
g_ptr_array_remove (context_array, context);
|
g_ptr_array_remove (context_array, context);
|
||||||
if (context_array->len == 0)
|
if (context_array->len == 0)
|
||||||
g_main_loop_quit (main_loop);
|
g_main_loop_quit (main_loop);
|
||||||
g_mutex_unlock (context_array_mutex);
|
g_mutex_unlock (&context_array_mutex);
|
||||||
|
|
||||||
cleanup_crawlers (context);
|
cleanup_crawlers (context);
|
||||||
|
|
||||||
@@ -343,10 +343,10 @@ create_crawler (void)
|
|||||||
G_LOCK (crawler_array_lock);
|
G_LOCK (crawler_array_lock);
|
||||||
g_ptr_array_add (crawler_array, source);
|
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_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
|
||||||
g_source_unref (source);
|
g_source_unref (source);
|
||||||
g_mutex_unlock (context_array_mutex);
|
g_mutex_unlock (&context_array_mutex);
|
||||||
|
|
||||||
G_UNLOCK (crawler_array_lock);
|
G_UNLOCK (crawler_array_lock);
|
||||||
}
|
}
|
||||||
@@ -386,14 +386,14 @@ recurser_start (gpointer data)
|
|||||||
GMainContext *context;
|
GMainContext *context;
|
||||||
GSource *source;
|
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)];
|
context = context_array->pdata[g_random_int_range (0, context_array->len)];
|
||||||
source = g_idle_source_new ();
|
source = g_idle_source_new ();
|
||||||
g_source_set_name (source, "Recursing idle source");
|
g_source_set_name (source, "Recursing idle source");
|
||||||
g_source_set_callback (source, recurser_idle, context, NULL);
|
g_source_set_callback (source, recurser_idle, context, NULL);
|
||||||
g_source_attach (source, context);
|
g_source_attach (source, context);
|
||||||
g_source_unref (source);
|
g_source_unref (source);
|
||||||
g_mutex_unlock (context_array_mutex);
|
g_mutex_unlock (&context_array_mutex);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
@@ -407,8 +407,6 @@ main (int argc,
|
|||||||
g_thread_init (NULL);
|
g_thread_init (NULL);
|
||||||
|
|
||||||
context_array = g_ptr_array_new ();
|
context_array = g_ptr_array_new ();
|
||||||
context_array_mutex = g_mutex_new ();
|
|
||||||
context_array_cond = g_cond_new ();
|
|
||||||
|
|
||||||
crawler_array = g_ptr_array_new ();
|
crawler_array = g_ptr_array_new ();
|
||||||
|
|
||||||
@@ -419,12 +417,12 @@ main (int argc,
|
|||||||
|
|
||||||
/* Wait for all threads to start
|
/* Wait for all threads to start
|
||||||
*/
|
*/
|
||||||
g_mutex_lock (context_array_mutex);
|
g_mutex_lock (&context_array_mutex);
|
||||||
|
|
||||||
if (context_array->len < NTHREADS)
|
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++)
|
for (i = 0; i < NCRAWLERS; i++)
|
||||||
create_crawler ();
|
create_crawler ();
|
||||||
|
@@ -50,7 +50,6 @@ static guint mem_chunk_recursion = 0;
|
|||||||
# define LEAVE_MEM_CHUNK_ROUTINE() (mem_chunk_recursion = MEM_CHUNK_ROUTINE_COUNT () - 1)
|
# define LEAVE_MEM_CHUNK_ROUTINE() (mem_chunk_recursion = MEM_CHUNK_ROUTINE_COUNT () - 1)
|
||||||
|
|
||||||
/* --- old memchunk prototypes --- */
|
/* --- old memchunk prototypes --- */
|
||||||
void old_mem_chunks_init (void);
|
|
||||||
GMemChunk* old_mem_chunk_new (const gchar *name,
|
GMemChunk* old_mem_chunk_new (const gchar *name,
|
||||||
gint atom_size,
|
gint atom_size,
|
||||||
gulong area_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
|
/* here we can't use StaticMutexes, as they depend upon a working
|
||||||
* g_malloc, the same holds true for StaticPrivate
|
* g_malloc, the same holds true for StaticPrivate
|
||||||
*/
|
*/
|
||||||
static GMutex *mem_chunks_lock = NULL;
|
static GMutex mem_chunks_lock;
|
||||||
static GMemChunk *mem_chunks = NULL;
|
static GMemChunk *mem_chunks = NULL;
|
||||||
|
|
||||||
void
|
|
||||||
old_mem_chunks_init (void)
|
|
||||||
{
|
|
||||||
mem_chunks_lock = g_mutex_new ();
|
|
||||||
}
|
|
||||||
|
|
||||||
GMemChunk*
|
GMemChunk*
|
||||||
old_mem_chunk_new (const gchar *name,
|
old_mem_chunk_new (const gchar *name,
|
||||||
gint atom_size,
|
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);
|
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);
|
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->next = mem_chunks;
|
||||||
mem_chunk->prev = NULL;
|
mem_chunk->prev = NULL;
|
||||||
if (mem_chunks)
|
if (mem_chunks)
|
||||||
mem_chunks->prev = mem_chunk;
|
mem_chunks->prev = mem_chunk;
|
||||||
mem_chunks = mem_chunk;
|
mem_chunks = mem_chunk;
|
||||||
g_mutex_unlock (mem_chunks_lock);
|
g_mutex_unlock (&mem_chunks_lock);
|
||||||
|
|
||||||
LEAVE_MEM_CHUNK_ROUTINE ();
|
LEAVE_MEM_CHUNK_ROUTINE ();
|
||||||
|
|
||||||
@@ -206,7 +199,7 @@ old_mem_chunk_destroy (GMemChunk *mem_chunk)
|
|||||||
g_free (temp_area);
|
g_free (temp_area);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_mutex_lock (mem_chunks_lock);
|
g_mutex_lock (&mem_chunks_lock);
|
||||||
if (mem_chunk->next)
|
if (mem_chunk->next)
|
||||||
mem_chunk->next->prev = mem_chunk->prev;
|
mem_chunk->next->prev = mem_chunk->prev;
|
||||||
if (mem_chunk->prev)
|
if (mem_chunk->prev)
|
||||||
@@ -214,7 +207,7 @@ old_mem_chunk_destroy (GMemChunk *mem_chunk)
|
|||||||
|
|
||||||
if (mem_chunk == mem_chunks)
|
if (mem_chunk == mem_chunks)
|
||||||
mem_chunks = mem_chunks->next;
|
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)
|
if (mem_chunk->type == G_ALLOC_AND_FREE)
|
||||||
g_tree_destroy (mem_chunk->mem_tree);
|
g_tree_destroy (mem_chunk->mem_tree);
|
||||||
@@ -545,20 +538,20 @@ old_mem_chunk_info (void)
|
|||||||
gint count;
|
gint count;
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
g_mutex_lock (mem_chunks_lock);
|
g_mutex_lock (&mem_chunks_lock);
|
||||||
mem_chunk = mem_chunks;
|
mem_chunk = mem_chunks;
|
||||||
while (mem_chunk)
|
while (mem_chunk)
|
||||||
{
|
{
|
||||||
count += 1;
|
count += 1;
|
||||||
mem_chunk = mem_chunk->next;
|
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_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;
|
mem_chunk = mem_chunks;
|
||||||
g_mutex_unlock (mem_chunks_lock);
|
g_mutex_unlock (&mem_chunks_lock);
|
||||||
|
|
||||||
while (mem_chunk)
|
while (mem_chunk)
|
||||||
{
|
{
|
||||||
|
@@ -23,8 +23,8 @@
|
|||||||
|
|
||||||
#define N_THREADS (13)
|
#define N_THREADS (13)
|
||||||
|
|
||||||
static GMutex *tmutex = NULL;
|
static GMutex tmutex;
|
||||||
static GCond *tcond = NULL;
|
static GCond tcond;
|
||||||
static volatile int thread_call_count = 0;
|
static volatile int thread_call_count = 0;
|
||||||
static char dummy_value = 'x';
|
static char dummy_value = 'x';
|
||||||
|
|
||||||
@@ -96,9 +96,9 @@ initializer3 (void)
|
|||||||
static gpointer
|
static gpointer
|
||||||
tmain_call_initializer3 (gpointer user_data)
|
tmain_call_initializer3 (gpointer user_data)
|
||||||
{
|
{
|
||||||
g_mutex_lock (tmutex);
|
g_mutex_lock (&tmutex);
|
||||||
g_cond_wait (tcond, tmutex);
|
g_cond_wait (&tcond, &tmutex);
|
||||||
g_mutex_unlock (tmutex);
|
g_mutex_unlock (&tmutex);
|
||||||
//g_printf ("[");
|
//g_printf ("[");
|
||||||
initializer3();
|
initializer3();
|
||||||
//g_printf ("]\n");
|
//g_printf ("]\n");
|
||||||
@@ -124,15 +124,13 @@ main (int argc,
|
|||||||
g_assert (p == &dummy_value);
|
g_assert (p == &dummy_value);
|
||||||
/* setup threads */
|
/* setup threads */
|
||||||
g_thread_init (NULL);
|
g_thread_init (NULL);
|
||||||
tmutex = g_mutex_new ();
|
|
||||||
tcond = g_cond_new ();
|
|
||||||
/* start multiple threads for initializer3() */
|
/* start multiple threads for initializer3() */
|
||||||
g_mutex_lock (tmutex);
|
g_mutex_lock (&tmutex);
|
||||||
for (i = 0; i < N_THREADS; i++)
|
for (i = 0; i < N_THREADS; i++)
|
||||||
threads[i] = g_thread_create (tmain_call_initializer3, 0, FALSE, NULL);
|
threads[i] = g_thread_create (tmain_call_initializer3, 0, FALSE, NULL);
|
||||||
g_mutex_unlock (tmutex);
|
g_mutex_unlock (&tmutex);
|
||||||
/* concurrently call initializer3() */
|
/* concurrently call initializer3() */
|
||||||
g_cond_broadcast (tcond);
|
g_cond_broadcast (&tcond);
|
||||||
/* loop until all threads passed the call to initializer3() */
|
/* loop until all threads passed the call to initializer3() */
|
||||||
while (g_atomic_int_get (&thread_call_count) < i)
|
while (g_atomic_int_get (&thread_call_count) < i)
|
||||||
{
|
{
|
||||||
@@ -140,14 +138,14 @@ main (int argc,
|
|||||||
g_thread_yield(); /* concurrent shuffling for single core */
|
g_thread_yield(); /* concurrent shuffling for single core */
|
||||||
else
|
else
|
||||||
g_usleep (1000); /* concurrent shuffling for multi core */
|
g_usleep (1000); /* concurrent shuffling for multi core */
|
||||||
g_cond_broadcast (tcond);
|
g_cond_broadcast (&tcond);
|
||||||
}
|
}
|
||||||
/* call multiple (unoptimized) initializers from multiple threads */
|
/* call multiple (unoptimized) initializers from multiple threads */
|
||||||
g_mutex_lock (tmutex);
|
g_mutex_lock (&tmutex);
|
||||||
g_atomic_int_set (&thread_call_count, 0);
|
g_atomic_int_set (&thread_call_count, 0);
|
||||||
for (i = 0; i < N_THREADS; i++)
|
for (i = 0; i < N_THREADS; i++)
|
||||||
g_thread_create (stress_concurrent_initializers, 0, FALSE, NULL);
|
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)
|
while (g_atomic_int_get (&thread_call_count) < 256 * 4 * N_THREADS)
|
||||||
g_usleep (50 * 1000); /* wait for all 5 threads to complete */
|
g_usleep (50 * 1000); /* wait for all 5 threads to complete */
|
||||||
return 0;
|
return 0;
|
||||||
@@ -157,7 +155,8 @@ main (int argc,
|
|||||||
* to uncover possible races in the g_once_init_enter_impl()/
|
* to uncover possible races in the g_once_init_enter_impl()/
|
||||||
* g_once_init_leave() implementations
|
* 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 16 * 16 simple initializers */
|
||||||
#define DEFINE_TEST_INITIALIZER(N) \
|
#define DEFINE_TEST_INITIALIZER(N) \
|
||||||
@@ -262,8 +261,8 @@ stress_concurrent_initializers (void *user_data)
|
|||||||
};
|
};
|
||||||
int i;
|
int i;
|
||||||
/* sync to main thread */
|
/* sync to main thread */
|
||||||
g_mutex_lock (tmutex);
|
g_mutex_lock (&tmutex);
|
||||||
g_mutex_unlock (tmutex);
|
g_mutex_unlock (&tmutex);
|
||||||
/* initialize concurrently */
|
/* initialize concurrently */
|
||||||
for (i = 0; i < G_N_ELEMENTS (initializers); i++)
|
for (i = 0; i < G_N_ELEMENTS (initializers); i++)
|
||||||
{
|
{
|
||||||
|
@@ -30,7 +30,7 @@ struct ThreadData
|
|||||||
int thread_id;
|
int thread_id;
|
||||||
GThread* gthread;
|
GThread* gthread;
|
||||||
|
|
||||||
GMutex* to_free_mutex;
|
GMutex to_free_mutex;
|
||||||
void* to_free [N_THREADS * N_ALLOCS];
|
void* to_free [N_THREADS * N_ALLOCS];
|
||||||
int bytes_to_free [N_THREADS * N_ALLOCS];
|
int bytes_to_free [N_THREADS * N_ALLOCS];
|
||||||
int n_to_free;
|
int n_to_free;
|
||||||
@@ -57,11 +57,11 @@ thread_func (void *arg)
|
|||||||
|
|
||||||
/* associate block with random thread */
|
/* associate block with random thread */
|
||||||
int t = rand() % N_THREADS;
|
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].to_free[tdata[t].n_to_free] = mem;
|
||||||
tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes;
|
tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes;
|
||||||
tdata[t].n_to_free++;
|
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 */
|
/* shuffle thread execution order every once in a while */
|
||||||
if (rand() % 97 == 0)
|
if (rand() % 97 == 0)
|
||||||
@@ -73,14 +73,14 @@ thread_func (void *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* free a block associated with this thread */
|
/* 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)
|
if (td->n_to_free > 0)
|
||||||
{
|
{
|
||||||
td->n_to_free--;
|
td->n_to_free--;
|
||||||
g_slice_free1 (td->bytes_to_free[td->n_to_free], td->to_free[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++;
|
td->n_freed++;
|
||||||
}
|
}
|
||||||
g_mutex_unlock (td->to_free_mutex);
|
g_mutex_unlock (&td->to_free_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -91,14 +91,11 @@ main()
|
|||||||
{
|
{
|
||||||
int t;
|
int t;
|
||||||
|
|
||||||
g_thread_init (NULL);
|
|
||||||
|
|
||||||
for (t = 0; t < N_THREADS; t++)
|
for (t = 0; t < N_THREADS; t++)
|
||||||
{
|
{
|
||||||
tdata[t].thread_id = t + 1;
|
tdata[t].thread_id = t + 1;
|
||||||
tdata[t].n_to_free = 0;
|
tdata[t].n_to_free = 0;
|
||||||
tdata[t].n_freed = 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);
|
g_print ("Starting %d threads for concurrent GSlice usage...\n", N_THREADS);
|
||||||
for (t = 0; t < N_THREADS; t++)
|
for (t = 0; t < N_THREADS; t++)
|
||||||
|
@@ -29,7 +29,6 @@ static guint number_of_repetitions = 10000; /* number of alloc+free repet
|
|||||||
static gboolean want_corruption = FALSE;
|
static gboolean want_corruption = FALSE;
|
||||||
|
|
||||||
/* --- old memchunk prototypes (memchunks.c) --- */
|
/* --- old memchunk prototypes (memchunks.c) --- */
|
||||||
void old_mem_chunks_init (void);
|
|
||||||
GMemChunk* old_mem_chunk_new (const gchar *name,
|
GMemChunk* old_mem_chunk_new (const gchar *name,
|
||||||
gint atom_size,
|
gint atom_size,
|
||||||
gulong area_size,
|
gulong area_size,
|
||||||
@@ -285,7 +284,6 @@ main (int argc,
|
|||||||
threads[i] = g_thread_create (test_sliced_mem_thread, seedp, TRUE, NULL);
|
threads[i] = g_thread_create (test_sliced_mem_thread, seedp, TRUE, NULL);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
old_mem_chunks_init();
|
|
||||||
for (i = 0; i < n_threads; i++)
|
for (i = 0; i < n_threads; i++)
|
||||||
threads[i] = g_thread_create (test_memchunk_thread, seedp, TRUE, NULL);
|
threads[i] = g_thread_create (test_memchunk_thread, seedp, TRUE, NULL);
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
/* GMutex */
|
/* GMutex */
|
||||||
|
|
||||||
static GMutex* test_g_mutex_mutex = NULL;
|
static GMutex test_g_mutex_mutex;
|
||||||
static guint test_g_mutex_int = 0;
|
static guint test_g_mutex_int = 0;
|
||||||
static gboolean test_g_mutex_thread_ready;
|
static gboolean test_g_mutex_thread_ready;
|
||||||
G_LOCK_DEFINE_STATIC (test_g_mutex);
|
G_LOCK_DEFINE_STATIC (test_g_mutex);
|
||||||
@@ -14,12 +14,12 @@ static gpointer
|
|||||||
test_g_mutex_thread (gpointer data)
|
test_g_mutex_thread (gpointer data)
|
||||||
{
|
{
|
||||||
g_assert (GPOINTER_TO_INT (data) == 42);
|
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);
|
g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
|
||||||
test_g_mutex_thread_ready = TRUE;
|
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_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);
|
return GINT_TO_POINTER (41);
|
||||||
}
|
}
|
||||||
@@ -28,9 +28,8 @@ static void
|
|||||||
test_g_mutex (void)
|
test_g_mutex (void)
|
||||||
{
|
{
|
||||||
GThread *thread;
|
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));
|
g_assert (G_TRYLOCK (test_g_mutex));
|
||||||
test_g_mutex_thread_ready = FALSE;
|
test_g_mutex_thread_ready = FALSE;
|
||||||
thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
|
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);
|
g_usleep (G_USEC_PER_SEC / 5);
|
||||||
test_g_mutex_int = 42;
|
test_g_mutex_int = 42;
|
||||||
G_UNLOCK (test_g_mutex);
|
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_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
|
||||||
g_mutex_free (test_g_mutex_mutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* GStaticRecMutex */
|
/* GStaticRecMutex */
|
||||||
|
Reference in New Issue
Block a user