Disable threading - turn update thread into async method
authorPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 12 Nov 2009 10:42:57 +0000 (11:42 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 12 Nov 2009 18:23:24 +0000 (19:23 +0100)
Among other things, this makes code flow easier to follow,
cinaest won't hang anymore when started from the SDK GUI,
and hopefully avoids the strange problems I had with issuing
D-Bus calls from a thread.

Makefile.am
src/imdb/imdb-sqlite.vala
src/main.vala
src/movie-list-store.vala
src/plugin-interface.vala
src/plugins/google-parser.vala
src/plugins/google-plugin.vala
src/plugins/imdb-plugin.vala

index 7a9277a..4bb1c09 100644 (file)
@@ -57,7 +57,7 @@ cinaest_VALASOURCES = \
 ${cinaest_SOURCES}: ${cinaest_VALASOURCES}
        ${VALAC} -C ${cinaest_VALASOURCES} ${cinaest_VALAFLAGS}
 
-cinaest_VALAFLAGS = --thread --vapidir ./vapi --pkg config --pkg hildon-1 --pkg libosso --pkg gmodule-2.0
+cinaest_VALAFLAGS = --vapidir ./vapi --pkg config --pkg hildon-1 --pkg libosso --pkg gmodule-2.0
 cinaest_CFLAGS = ${HILDON_CFLAGS} ${OSSO_CFLAGS} ${GMODULE_CFLAGS} \
        -DGETTEXT_PACKAGE=\"@GETTEXT_PACKAGE@\"
 cinaest_LDADD = ${HILDON_LIBS} ${OSSO_LIBS} ${GMODULE_LIBS}
index a48a3f5..40eddfc 100644 (file)
@@ -162,7 +162,7 @@ class IMDbSqlite : Object {
                return 0;
        }
 
-       public int query (MovieFilter filter, ReceiveMovieFunction receive_movie) {
+       public async int query (MovieFilter filter, ReceiveMovieFunction receive_movie) {
                var sql = "SELECT Title, Year, Rating, Genres FROM Movies";
                var sep = " WHERE ";
                Statement stmt;
@@ -201,6 +201,8 @@ class IMDbSqlite : Object {
                }
 
                do {
+                       Idle.add (query.callback);
+                       yield;
                        rc = stmt.step ();
                        if (rc == Sqlite.ROW) {
                                int year = stmt.column_int (1);
index a61c122..f580ccf 100644 (file)
@@ -72,7 +72,6 @@ public class CinaestProgram : Hildon.Program {
 
        static int main (string[] args) {
                Gtk.init (ref args);
-               Gdk.threads_init ();
 
                Intl.setlocale (LocaleCategory.ALL, "");
                Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
index 1ad805f..b47fa20 100644 (file)
@@ -41,7 +41,6 @@ public class MovieListStore : ListStore, TreeModel {
        public MovieSource source;
        private MovieFilter filter;
        public bool update_running { get; set; }
-       private weak Thread thread;
        private Cancellable cancellable;
 
        construct {
@@ -86,43 +85,42 @@ public class MovieListStore : ListStore, TreeModel {
 
        public bool start_search (MovieFilter _filter) {
                if (update_running) {
-                       stdout.printf ("aborting search thread (%p)...\n", thread);
+                       stdout.printf ("aborting search ...\n");
                        cancellable.cancel ();
                //      poster_factory.clear_queue ();
-                       thread.join ();
-                       stdout.printf ("search thread aborted\n");
+                       return false;
                }
                if (cancellable == null || cancellable.is_cancelled ())
                        cancellable = new Cancellable ();
 
                filter = _filter;
-               try {
-                       thread = Thread.create (search_thread, true);
-                       update_running = true;
-               } catch (ThreadError e) {
-                       warning ("Failed to start search thread: %s", e.message);
-               }
-               return update_running;
+               stdout.printf ("begin search\n");
+               search_async.begin ();
+               update_running = true;
+               return true;
        }
 
-       // Update thread
-       private void* search_thread () {
-               stdout.printf ("search thread started: \"%s\"\n", filter.title);
+       // Asynchronous update method
+       private async void search_async () {
+               stdout.printf ("search started: \"%s\"\n", filter.title);
 
-               Gdk.threads_enter ();
                clear ();
-               Gdk.threads_leave ();
 
                if (source != null)
                        // FIXME - arbitrary limit
-                       source.get_movies (filter, receive_movie, 100, cancellable);
+                       yield source.get_movies (filter, receive_movie, 100, cancellable);
 
-               Gdk.threads_enter ();
                update_running = false;
-               Gdk.threads_leave ();
-
-               stdout.printf ("search thread stopped\n");
-               return null;
+               if (cancellable.is_cancelled ()) {
+                       stdout.printf ("search aborted, starting new\n");
+                       cancellable.reset ();
+                       if (cancellable.is_cancelled ()) {
+                               stdout.printf ("OW WEY\n");
+                       }
+                       start_search (filter);
+               } else {
+                       stdout.printf ("search stopped\n");
+               }
        }
 
        private void receive_movie (Movie movie) {
@@ -131,9 +129,7 @@ public class MovieListStore : ListStore, TreeModel {
                if (cancellable.is_cancelled ())
                        return;
 
-               Gdk.threads_enter ();
                add (movie, out iter);
-               Gdk.threads_leave ();
        }
 
        // Implement TreeModel interface
index 2d14e4a..d0ba953 100644 (file)
@@ -29,7 +29,7 @@ public abstract class Plugin : Object {
 public abstract class MovieSource : Object {
        public delegate void ReceiveMovieFunction (Movie movie);
 
-       public abstract void get_movies (MovieFilter filter, ReceiveMovieFunction callback, int limit, GLib.Cancellable? cancellable);
+       public abstract async void get_movies (MovieFilter filter, ReceiveMovieFunction callback, int limit, GLib.Cancellable? cancellable);
 
        public abstract void add_movie (Movie movie);
 
index 5c36ceb..5ccdcb9 100644 (file)
@@ -264,7 +264,7 @@ public class GoogleParser : Object {
                }
        }
 
-       public GoogleParser (MovieFilter filter, string? location, MovieSource.ReceiveMovieFunction callback, Cancellable? cancellable) {
+       public async void query (MovieFilter filter, string? location, MovieSource.ReceiveMovieFunction callback, Cancellable? cancellable) {
                _get_callback = callback;
                _filter = filter;
                if (filter.title.chr(filter.title.length, '*') != null) {
index 7a165f1..534c139 100644 (file)
@@ -74,8 +74,10 @@ class GoogleSource : MovieSource {
        public string location;
        public string description;
 
-       public override void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
-               var parser = new GoogleParser (filter, location, callback, cancellable);
+       public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
+               var parser = new GoogleParser ();
+
+               yield parser.query (filter, location, callback, cancellable);
        }
 
        public override void add_movie (Movie movie) {
index e9cf4f9..4132c8d 100644 (file)
@@ -125,7 +125,7 @@ class IMDbPlugin : Plugin {
 
 class IMDBSource : MovieSource {
        MovieSource.ReceiveMovieFunction _get_callback;
-       public override void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
+       public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
                // IMDb has too many movies
                if (filter.title == "")
                        return;
@@ -133,7 +133,7 @@ class IMDBSource : MovieSource {
                                             "cinaest", "imdb.db", null));
 
                _get_callback = callback;
-               sqlite.query (filter, receive_movie);
+               yield sqlite.query (filter, receive_movie);
        }
 
        private void receive_movie (string title, int year, int rating, int genres) {