config-policy: add timeout when waiting on conditional variables

This commit is contained in:
Julian Bouzas 2019-12-27 08:32:28 -05:00
parent c56209fac6
commit d3d977cbe4
2 changed files with 15 additions and 2 deletions

View file

@ -71,6 +71,8 @@ loop_thread_start (void *d)
static void
config_policy_setup (TestConfigPolicyFixture *self, gconstpointer user_data)
{
gint64 end_time;
/* Data */
g_mutex_init (&self->mutex);
g_cond_init (&self->cond);
@ -81,8 +83,13 @@ config_policy_setup (TestConfigPolicyFixture *self, gconstpointer user_data)
/* Wait for everything to be created */
g_mutex_lock (&self->mutex);
end_time = g_get_monotonic_time () + 3 * G_TIME_SPAN_SECOND;
while (!self->created)
g_cond_wait (&self->cond, &self->mutex);
if (!g_cond_wait_until (&self->cond, &self->mutex, end_time)) {
/* Abort when timeout has passed */
g_warning ("Aborting due to timeout when waiting for connection");
abort();
}
g_mutex_unlock (&self->mutex);
}

View file

@ -39,11 +39,17 @@ G_DEFINE_TYPE (WpConfigPolicyContext, wp_config_policy_context, G_TYPE_OBJECT);
static WpBaseEndpoint *
wait_for_endpoint (WpConfigPolicyContext *self, WpBaseEndpointLink **link)
{
gint64 end_time;
g_mutex_lock (&self->mutex);
/* Wait for endpoint to be set */
end_time = g_get_monotonic_time () + 3 * G_TIME_SPAN_SECOND;
while (!self->endpoint)
g_cond_wait (&self->cond, &self->mutex);
if (!g_cond_wait_until (&self->cond, &self->mutex, end_time)) {
/* Abort when timeout has passed */
g_warning ("Aborting due to timeout when waiting for endpoint");
abort();
}
/* Set endpoint to a local value and clear global value */
WpBaseEndpoint *endpoint = g_object_ref (self->endpoint);