Promote EAsyncClosure to GIO?
Submitted by Matthew Barnes
Link to original bug (#686669)
Description
Some time in the past, David Zeuthen showed me a pattern for implementing a synchronous I/O function in terms of its asynchronous counterpart.
The steps are (from memory):
-
Create a new GMainContext and push it as the thread-default.
-
Create a new GMainLoop to use the GMainContext.
-
Dispatch the asynchonous function, with a closure structure that includes a pointer to the GMainLoop, and an GAsyncReadyCallback function that calls g_main_loop_quit().
-
Run the GMainLoop. It will terminate when the async function completes.
-
Call the corresponding "finish" function to obtain the result.
-
Pop the thread-default GMainContext and free allocated resources.
-
Return the result.
This pattern is used throughout GDBus. Here's an example: http://git.gnome.org/browse/glib/tree/gio/gdbusconnection.c#n2208
I've had numerous occasions to use the pattern in Evolution and E-D-S, but it's rather laborious to type out each time. For that reason I managed to capture the pattern in a handy little API in libedataserver which I call EAsyncClosure.
Here's the API documentation for it: http://developer.gnome.org/libedataserver/stable/libedataserver-e-util.html#EAsyncClosure
Here's an example of its usage: http://git.gnome.org/browse/evolution-data-server/tree/libebackend/e-source-registry-server.c#n2068
Was wondering if there's any interest in promoting this up to GIO since it's probably useful to a larger audience.
Version: 2.35.x