pull: Stop using GMainLoop
First of all, what we were doing with having GMainLoop in the internal APIs is wrong. Synchronous APIs should always create their own main context and not iterate the caller's. Doing the latter creates potential for evil reentrancy issues. Sync API should block, async API is for not blocking. Now that's out of the way, fix the pull code to do the clean ``` while (termination_condition (state)) g_main_context_iteration (mainctx, TRUE); ``` model for looping. This is a lot easier to understand and ultimately more reliable than having other code call `g_main_loop_quit()`, as the loop condition is in exactly one place. We can also remove the idle source which only fired once. Note we have to add a hack here to discard the synchronous session and create a new one which we only use async. https://bugzilla.gnome.org/show_bug.cgi?id=753336
This commit is contained in:
1 parent
5c20ea920e
commit
9f3d586993
6 files changed
+133
-243
No files matched your search
@@ -714,7 +714,7 @@ _ostree_fetcher_bytes_transferred (OstreeFetcher *self)
|
||||
typedef struct
|
||||
{
|
||||
GInputStream *result_stream;
|
||||
GMainLoop *loop;
|
||||
gboolean done;
|
||||
GError **error;
|
||||
}
|
||||
FetchUriSyncData;
|
||||
@@ -728,7 +728,7 @@ fetch_uri_sync_on_complete (GObject *object,
|
||||
|
||||
data->result_stream = ostree_fetcher_stream_uri_finish ((OstreeFetcher*)object,
|
||||
result, data->error);
|
||||
g_main_loop_quit (data->loop);
|
||||
data->done = TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
@@ -737,7 +737,6 @@ _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
|
||||
gboolean add_nul,
|
||||
gboolean allow_noent,
|
||||
GBytes **out_contents,
|
||||
GMainLoop *loop,
|
||||
guint64 max_size,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
@@ -746,6 +745,7 @@ _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
|
||||
const guint8 nulchar = 0;
|
||||
g_autofree char *ret_contents = NULL;
|
||||
g_autoptr(GMemoryOutputStream) buf = NULL;
|
||||
g_autoptr(GMainContext) mainctx = NULL;
|
||||
FetchUriSyncData data;
|
||||
g_assert (error != NULL);
|
||||
|
||||
@@ -754,7 +754,10 @@ _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
|
||||
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
||||
return FALSE;
|
||||
|
||||
data.loop = loop;
|
||||
mainctx = g_main_context_new ();
|
||||
g_main_context_push_thread_default (mainctx);
|
||||
|
||||
data.done = FALSE;
|
||||
data.error = error;
|
||||
|
||||
ostree_fetcher_stream_uri_async (fetcher, uri,
|
||||
@@ -762,8 +765,9 @@ _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
|
||||
OSTREE_FETCHER_DEFAULT_PRIORITY,
|
||||
cancellable,
|
||||
fetch_uri_sync_on_complete, &data);
|
||||
while (!data.done)
|
||||
g_main_context_iteration (mainctx, TRUE);
|
||||
|
||||
g_main_loop_run (loop);
|
||||
if (!data.result_stream)
|
||||
{
|
||||
if (allow_noent)
|
||||
@@ -796,6 +800,8 @@ _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
|
||||
ret = TRUE;
|
||||
*out_contents = g_memory_output_stream_steal_as_bytes (buf);
|
||||
out:
|
||||
if (mainctx)
|
||||
g_main_context_pop_thread_default (mainctx);
|
||||
g_clear_object (&(data.result_stream));
|
||||
return ret;
|
||||
}
|
||||
Reference in new issue
Block a user