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
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
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;
}
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);
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;
}
}
typedef struct {
- GMutex *loop_mutex;
- GCond *loop_started;
+ GMutex loop_mutex;
+ GCond loop_started;
+ gboolean started;
Test *test;
} ThreadLoop;
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);
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
*/
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
/* This test tests the macros for defining dynamic types.
*/
-static GMutex *sync_mutex = NULL;
+static GMutex sync_mutex;
static gboolean loaded = FALSE;
/* MODULE */
*/
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");
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++) {
}
/* execute threads */
- g_mutex_unlock (sync_mutex);
+ g_mutex_unlock (&sync_mutex);
for (i = 0; i < N_THREADS; i++) {
g_thread_join (threads[i]);
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);
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)
* 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 */
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())
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();
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);
}
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);
* 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;
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);
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);
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);
}
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;
}
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 ();
/* 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 ();
# 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,
/* 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,
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 ();
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)
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);
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)
{
#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';
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");
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)
{
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;
* 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) \
};
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++)
{
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;
/* 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)
}
/* 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;
{
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++)
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,
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);
}
/* 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);
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);
}
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),
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 */