[gnome-boxes] Add AsyncLauncher class



commit 7cc0eacc1359afa4e3721d31d0910020d458d795
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Mar 23 12:52:35 2016 +0000

    Add AsyncLauncher class
    
    Add a singleton that will be responsible for launching tasks in threads
    and then keep track of all launched threads so that at application exit,
    we can wait for all threads to finish.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=761479

 src/Makefile.am         |    1 +
 src/async-launcher.vala |   50 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8b9d211..4f3e43e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -49,6 +49,7 @@ AM_VALAFLAGS =                                                \
 
 noinst_LIBRARIES = libcommon.a
 libcommon_a_VALASOURCES =                      \
+       async-launcher.vala                     \
        box-config.vala                         \
        collection-source.vala                  \
        util.vala                               \
diff --git a/src/async-launcher.vala b/src/async-launcher.vala
new file mode 100644
index 0000000..57866d7
--- /dev/null
+++ b/src/async-launcher.vala
@@ -0,0 +1,50 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+
+public class Boxes.AsyncLauncher {
+    public delegate void RunInThreadFunc () throws  GLib.Error;
+
+    private static AsyncLauncher launcher = null;
+
+    private GLib.List<GLib.Thread<void *>> all_threads;
+
+    public static AsyncLauncher get_default () {
+        if (launcher == null)
+            launcher = new AsyncLauncher ();
+
+        return launcher;
+    }
+
+    private AsyncLauncher () {
+        all_threads = new GLib.List<GLib.Thread<void *>> ();
+    }
+
+    public async void launch (owned RunInThreadFunc func) throws GLib.Error {
+        GLib.Error e = null;
+        GLib.SourceFunc resume = launch.callback;
+        var thread = new GLib.Thread<void*> (null, () => {
+            try {
+                func ();
+            } catch (GLib.Error err) {
+                e = err;
+            }
+
+            Idle.add ((owned) resume);
+
+            return null;
+        });
+
+        all_threads.append (thread);
+
+        yield;
+
+        all_threads.remove (thread);
+
+        if (e != null)
+            throw e;
+    }
+
+    public void await_all () {
+        foreach (var thread in all_threads)
+            thread.join ();
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]