mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-14 19:55:12 +01:00
tests/mainloop: fix a race condition
Rather than depending on the machine's speed/load, just interlock between the two threads properly.
This commit is contained in:
parent
e0aa0ae8a2
commit
baf0ebf7b2
@ -253,6 +253,9 @@ call_func (gpointer data)
|
|||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static GMutex mutex;
|
||||||
|
static GCond cond;
|
||||||
|
|
||||||
static gpointer
|
static gpointer
|
||||||
thread_func (gpointer data)
|
thread_func (gpointer data)
|
||||||
{
|
{
|
||||||
@ -261,6 +264,10 @@ thread_func (gpointer data)
|
|||||||
|
|
||||||
g_main_context_push_thread_default (ctx);
|
g_main_context_push_thread_default (ctx);
|
||||||
|
|
||||||
|
g_mutex_lock (&mutex);
|
||||||
|
g_cond_signal (&cond);
|
||||||
|
g_mutex_unlock (&mutex);
|
||||||
|
|
||||||
source = g_timeout_source_new (500);
|
source = g_timeout_source_new (500);
|
||||||
g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
|
g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
|
||||||
g_source_attach (source, ctx);
|
g_source_attach (source, ctx);
|
||||||
@ -293,9 +300,10 @@ test_invoke (void)
|
|||||||
* to another thread
|
* to another thread
|
||||||
*/
|
*/
|
||||||
ctx = g_main_context_new ();
|
ctx = g_main_context_new ();
|
||||||
|
g_mutex_lock (&mutex);
|
||||||
thread = g_thread_new ("worker", thread_func, ctx);
|
thread = g_thread_new ("worker", thread_func, ctx);
|
||||||
|
|
||||||
g_usleep (1000); /* give some time to push the thread-default */
|
g_cond_wait (&cond, &mutex);
|
||||||
|
|
||||||
g_main_context_invoke (ctx, func, thread);
|
g_main_context_invoke (ctx, func, thread);
|
||||||
g_assert_cmpint (count, ==, 2);
|
g_assert_cmpint (count, ==, 2);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user