mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-03 14:23:07 +02:00
GThreadedResolver: port to embedded GMutex/GCond
This is the only case that was non trivial to port, due to some of the logic being based on checking the GCond* for being non-%NULL. https://bugzilla.gnome.org/show_bug.cgi?id=660739
This commit is contained in:
parent
518feb45eb
commit
8bcdabf25a
@ -158,10 +158,10 @@ struct _GThreadedResolverRequest {
|
|||||||
GCancellable *cancellable;
|
GCancellable *cancellable;
|
||||||
GError *error;
|
GError *error;
|
||||||
|
|
||||||
GMutex *mutex;
|
GMutex mutex;
|
||||||
guint ref_count;
|
guint ref_count;
|
||||||
|
|
||||||
GCond *cond;
|
GCond cond;
|
||||||
GSimpleAsyncResult *async_result;
|
GSimpleAsyncResult *async_result;
|
||||||
gboolean complete;
|
gboolean complete;
|
||||||
|
|
||||||
@ -185,9 +185,10 @@ g_threaded_resolver_request_new (GThreadedResolverResolveFunc resolve_func,
|
|||||||
/* Initial refcount is 2; one for the caller and one for resolve_func */
|
/* Initial refcount is 2; one for the caller and one for resolve_func */
|
||||||
req->ref_count = 2;
|
req->ref_count = 2;
|
||||||
|
|
||||||
req->mutex = g_mutex_new ();
|
g_mutex_init (&req->mutex);
|
||||||
|
g_cond_init (&req->cond);
|
||||||
/* Initially locked; caller must unlock */
|
/* Initially locked; caller must unlock */
|
||||||
g_mutex_lock (req->mutex);
|
g_mutex_lock (&req->mutex);
|
||||||
|
|
||||||
if (cancellable)
|
if (cancellable)
|
||||||
{
|
{
|
||||||
@ -206,16 +207,14 @@ g_threaded_resolver_request_unref (GThreadedResolverRequest *req)
|
|||||||
{
|
{
|
||||||
guint ref_count;
|
guint ref_count;
|
||||||
|
|
||||||
g_mutex_lock (req->mutex);
|
g_mutex_lock (&req->mutex);
|
||||||
ref_count = --req->ref_count;
|
ref_count = --req->ref_count;
|
||||||
g_mutex_unlock (req->mutex);
|
g_mutex_unlock (&req->mutex);
|
||||||
if (ref_count > 0)
|
if (ref_count > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
g_mutex_free (req->mutex);
|
g_mutex_clear (&req->mutex);
|
||||||
|
g_cond_clear (&req->cond);
|
||||||
if (req->cond)
|
|
||||||
g_cond_free (req->cond);
|
|
||||||
|
|
||||||
if (req->error)
|
if (req->error)
|
||||||
g_error_free (req->error);
|
g_error_free (req->error);
|
||||||
@ -235,19 +234,19 @@ static void
|
|||||||
g_threaded_resolver_request_complete (GThreadedResolverRequest *req,
|
g_threaded_resolver_request_complete (GThreadedResolverRequest *req,
|
||||||
GError *error)
|
GError *error)
|
||||||
{
|
{
|
||||||
g_mutex_lock (req->mutex);
|
g_mutex_lock (&req->mutex);
|
||||||
if (req->complete)
|
if (req->complete)
|
||||||
{
|
{
|
||||||
/* The req was cancelled, and now it has finished resolving as
|
/* The req was cancelled, and now it has finished resolving as
|
||||||
* well. But we have nowhere to send the result, so just return.
|
* well. But we have nowhere to send the result, so just return.
|
||||||
*/
|
*/
|
||||||
g_mutex_unlock (req->mutex);
|
g_mutex_unlock (&req->mutex);
|
||||||
g_clear_error (&error);
|
g_clear_error (&error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
req->complete = TRUE;
|
req->complete = TRUE;
|
||||||
g_mutex_unlock (req->mutex);
|
g_mutex_unlock (&req->mutex);
|
||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
g_propagate_error (&req->error, error);
|
g_propagate_error (&req->error, error);
|
||||||
@ -260,9 +259,7 @@ g_threaded_resolver_request_complete (GThreadedResolverRequest *req,
|
|||||||
req->cancellable = NULL;
|
req->cancellable = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req->cond)
|
if (req->async_result)
|
||||||
g_cond_signal (req->cond);
|
|
||||||
else if (req->async_result)
|
|
||||||
{
|
{
|
||||||
if (req->error)
|
if (req->error)
|
||||||
g_simple_async_result_set_from_error (req->async_result, req->error);
|
g_simple_async_result_set_from_error (req->async_result, req->error);
|
||||||
@ -274,6 +271,9 @@ g_threaded_resolver_request_complete (GThreadedResolverRequest *req,
|
|||||||
g_object_unref (req->async_result);
|
g_object_unref (req->async_result);
|
||||||
req->async_result = NULL;
|
req->async_result = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
g_cond_signal (&req->cond);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -319,18 +319,17 @@ resolve_sync (GThreadedResolver *gtr,
|
|||||||
if (!req->cancellable || !gtr->thread_pool)
|
if (!req->cancellable || !gtr->thread_pool)
|
||||||
{
|
{
|
||||||
req->resolve_func (req, error);
|
req->resolve_func (req, error);
|
||||||
g_mutex_unlock (req->mutex);
|
g_mutex_unlock (&req->mutex);
|
||||||
|
|
||||||
g_threaded_resolver_request_complete (req, FALSE);
|
g_threaded_resolver_request_complete (req, FALSE);
|
||||||
g_threaded_resolver_request_unref (req);
|
g_threaded_resolver_request_unref (req);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
req->cond = g_cond_new ();
|
|
||||||
g_thread_pool_push (gtr->thread_pool, req, &req->error);
|
g_thread_pool_push (gtr->thread_pool, req, &req->error);
|
||||||
if (!req->error)
|
if (!req->error)
|
||||||
g_cond_wait (req->cond, req->mutex);
|
g_cond_wait (&req->cond, &req->mutex);
|
||||||
g_mutex_unlock (req->mutex);
|
g_mutex_unlock (&req->mutex);
|
||||||
|
|
||||||
if (req->error)
|
if (req->error)
|
||||||
{
|
{
|
||||||
@ -351,7 +350,7 @@ resolve_async (GThreadedResolver *gtr,
|
|||||||
g_simple_async_result_set_op_res_gpointer (req->async_result, req,
|
g_simple_async_result_set_op_res_gpointer (req->async_result, req,
|
||||||
(GDestroyNotify)g_threaded_resolver_request_unref);
|
(GDestroyNotify)g_threaded_resolver_request_unref);
|
||||||
g_thread_pool_push (gtr->thread_pool, req, NULL);
|
g_thread_pool_push (gtr->thread_pool, req, NULL);
|
||||||
g_mutex_unlock (req->mutex);
|
g_mutex_unlock (&req->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GThreadedResolverRequest *
|
static GThreadedResolverRequest *
|
||||||
|
Loading…
x
Reference in New Issue
Block a user