[tracker/rss-enclosures] libtracker-data: Replace interrupt_thread by GCancellable



commit 2bfed3ad6eb1d26cba7f85bf82afd4ef1b8d216f
Author: Jürg Billeter <j bitron ch>
Date:   Tue Jul 6 17:55:26 2010 +0200

    libtracker-data: Replace interrupt_thread by GCancellable

 src/libtracker-data/tracker-data-manager.c        |   30 ++++---------
 src/libtracker-data/tracker-data-manager.h        |    3 +-
 src/libtracker-data/tracker-data-query.c          |    4 +-
 src/libtracker-data/tracker-data-update.c         |    4 +-
 src/libtracker-data/tracker-db-interface-sqlite.c |   34 ++++++--------
 src/libtracker-data/tracker-db-interface.h        |    4 +-
 src/libtracker-data/tracker-db-manager.c          |   49 +--------------------
 src/libtracker-data/tracker-db-manager.h          |    3 -
 src/tracker-store/tracker-resources.c             |    4 +-
 src/tracker-store/tracker-steroids.c              |    3 +-
 src/tracker-store/tracker-store.c                 |   37 ++++++++-------
 src/tracker-store/tracker-store.h                 |    1 +
 12 files changed, 56 insertions(+), 120 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index cabb45c..ebb74e1 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -1426,7 +1426,7 @@ class_add_super_classes_from_db (TrackerDBInterface *iface,
 	g_object_unref (stmt);
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, NULL)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, NULL)) {
 			TrackerClass *super_class;
 			const gchar *super_class_uri;
 
@@ -1464,7 +1464,7 @@ class_add_domain_indexes_from_db (TrackerDBInterface *iface,
 	g_object_unref (stmt);
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, NULL)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, NULL)) {
 			TrackerProperty *domain_index;
 			const gchar *domain_index_uri;
 
@@ -1502,7 +1502,7 @@ property_add_super_properties_from_db (TrackerDBInterface *iface,
 	g_object_unref (stmt);
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, NULL)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, NULL)) {
 			TrackerProperty *super_property;
 			const gchar *super_property_uri;
 
@@ -1536,7 +1536,7 @@ db_get_static_data (TrackerDBInterface *iface)
 	}
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, &error)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			TrackerOntology *ontology;
 			const gchar     *uri;
 			time_t           last_mod;
@@ -1574,7 +1574,7 @@ db_get_static_data (TrackerDBInterface *iface)
 	}
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, &error)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			TrackerNamespace *namespace;
 			const gchar      *uri, *prefix;
 
@@ -1613,7 +1613,7 @@ db_get_static_data (TrackerDBInterface *iface)
 	}
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, &error)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			TrackerClass *class;
 			const gchar  *uri;
 			gint          id;
@@ -1702,7 +1702,7 @@ db_get_static_data (TrackerDBInterface *iface)
 	}
 
 	if (cursor) {
-		while (tracker_db_cursor_iter_next (cursor, &error)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			GValue value = { 0 };
 			TrackerProperty *property;
 			const gchar     *uri, *domain_uri, *range_uri, *secondary_index_uri, *default_value;
@@ -2652,7 +2652,7 @@ get_new_service_id (TrackerDBInterface *iface)
 	}
 
 	if (cursor) {
-		if (tracker_db_cursor_iter_next (cursor, &error)) {
+		if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			max_service_id = tracker_db_cursor_get_int (cursor, 0);
 		}
 		g_object_unref (cursor);
@@ -2913,7 +2913,7 @@ tracker_data_manager_init (TrackerDBManagerFlags  flags,
 		                                     NULL);
 
 		if (cursor) {
-			while (tracker_db_cursor_iter_next (cursor, &error)) {
+			while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 				const gchar *onto_uri = tracker_db_cursor_get_string (cursor, 0, NULL);
 				/* It's stored as an int in the db anyway. This is caused by
 				 * string_to_gvalue in tracker-data-update.c */
@@ -3145,15 +3145,3 @@ tracker_data_manager_set_db_option_int64 (const gchar *option,
 		return;
 	}
 }
-
-gboolean
-tracker_data_manager_interrupt_thread (GThread *thread)
-{
-	return tracker_db_manager_interrupt_thread (thread);
-}
-
-void
-tracker_data_manager_interrupt_thread_reset (GThread *thread)
-{
-	tracker_db_manager_interrupt_thread_reset (thread);
-}
diff --git a/src/libtracker-data/tracker-data-manager.h b/src/libtracker-data/tracker-data-manager.h
index 8b8a4fa..7383cf4 100644
--- a/src/libtracker-data/tracker-data-manager.h
+++ b/src/libtracker-data/tracker-data-manager.h
@@ -46,8 +46,7 @@ gboolean tracker_data_manager_init                   (TrackerDBManagerFlags  fla
                                                       gpointer               busy_user_data,
                                                       const gchar           *busy_status);
 void     tracker_data_manager_shutdown               (void);
-gboolean tracker_data_manager_interrupt_thread       (GThread               *thread);
-void     tracker_data_manager_interrupt_thread_reset (GThread               *thread);
+
 gint64   tracker_data_manager_get_db_option_int64    (const gchar           *option);
 void     tracker_data_manager_set_db_option_int64    (const gchar           *option,
                                                       gint64                 value);
diff --git a/src/libtracker-data/tracker-data-query.c b/src/libtracker-data/tracker-data-query.c
index 310d7a5..b89819f 100644
--- a/src/libtracker-data/tracker-data-query.c
+++ b/src/libtracker-data/tracker-data-query.c
@@ -65,7 +65,7 @@ tracker_data_query_rdf_type (gint id)
 		 * function is called fairly often) */
 
 		ret = g_ptr_array_sized_new (20);
-		while (tracker_db_cursor_iter_next (cursor, &error)) {
+		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			const gchar *class_uri;
 			TrackerClass *cl;
 
@@ -116,7 +116,7 @@ tracker_data_query_resource_id (const gchar *uri)
 	}
 
 	if (cursor) {
-		if (tracker_db_cursor_iter_next (cursor, &error)) {
+		if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			id = tracker_db_cursor_get_int (cursor, 0);
 		}
 
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 86971e1..ab2dd08 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -360,7 +360,7 @@ tracker_data_update_get_new_service_id (void)
 	}
 
 	if (cursor) {
-		if (tracker_db_cursor_iter_next (cursor, &error)) {
+		if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			max_service_id = MAX (tracker_db_cursor_get_int (cursor, 0), max_service_id);
 		}
 
@@ -405,7 +405,7 @@ tracker_data_update_get_next_modseq (void)
 	}
 
 	if (cursor) {
-		if (tracker_db_cursor_iter_next (cursor, &error)) {
+		if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 			max_modseq = MAX (tracker_db_cursor_get_int (cursor, 0), max_modseq);
 		}
 
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c b/src/libtracker-data/tracker-db-interface-sqlite.c
index 1684746..3ed2d0c 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -47,7 +47,7 @@ struct TrackerDBInterface {
 #if HAVE_TRACKER_FTS
 	guint fts_initialized : 1;
 #endif
-	volatile gint interrupt;
+	GCancellable *cancellable;
 };
 
 struct TrackerDBInterfaceClass {
@@ -449,7 +449,7 @@ static int
 check_interrupt (void *user_data)
 {
 	TrackerDBInterface *db_interface = user_data;
-	return g_atomic_int_get (&db_interface->interrupt);
+	return g_cancellable_is_cancelled (db_interface->cancellable) ? 1 : 0;
 }
 
 static void
@@ -784,6 +784,7 @@ tracker_db_interface_create_statement (TrackerDBInterface  *db_interface,
 static TrackerDBResultSet *
 create_result_set_from_stmt (TrackerDBInterface  *interface,
                              sqlite3_stmt        *stmt,
+                             GCancellable        *cancellable,
                              GError             **error)
 {
 	TrackerDBResultSet *result_set = NULL;
@@ -795,11 +796,14 @@ create_result_set_from_stmt (TrackerDBInterface  *interface,
 	while (result == SQLITE_OK  ||
 	       result == SQLITE_ROW) {
 
-		if (g_atomic_int_get (&interface->interrupt) == 1) {
+		if (g_cancellable_is_cancelled (cancellable)) {
 			result = SQLITE_INTERRUPT;
 			sqlite3_reset (stmt);
 		} else {
+			/* only one statement can be active at the same time per interface */
+			interface->cancellable = cancellable;
 			result = sqlite3_step (stmt);
+			interface->cancellable = NULL;
 		}
 
 		switch (result) {
@@ -905,27 +909,13 @@ tracker_db_interface_execute_vquery (TrackerDBInterface  *db_interface,
 		return NULL;
 	}
 
-	result_set = create_result_set_from_stmt (db_interface, stmt, error);
+	result_set = create_result_set_from_stmt (db_interface, stmt, NULL, error);
 	sqlite3_finalize (stmt);
 
 	g_free (full_query);
 	return result_set;
 }
 
-gboolean
-tracker_db_interface_interrupt (TrackerDBInterface *iface)
-{
-	g_atomic_int_set (&iface->interrupt, 1);
-
-	return TRUE;
-}
-
-void
-tracker_db_interface_reset_interrupt (TrackerDBInterface *iface)
-{
-	iface->interrupt = 0;
-}
-
 TrackerDBInterface *
 tracker_db_interface_sqlite_new (const gchar *filename)
 {
@@ -1125,6 +1115,7 @@ tracker_db_cursor_rewind (TrackerDBCursor *cursor)
 
 gboolean
 tracker_db_cursor_iter_next (TrackerDBCursor *cursor,
+                             GCancellable    *cancellable,
                              GError         **error)
 {
 	TrackerDBStatement *stmt = cursor->ref_stmt;
@@ -1133,11 +1124,14 @@ tracker_db_cursor_iter_next (TrackerDBCursor *cursor,
 	if (!cursor->finished) {
 		guint result;
 
-		if (g_atomic_int_get (&iface->interrupt) == 1) {
+		if (g_cancellable_is_cancelled (cancellable)) {
 			result = SQLITE_INTERRUPT;
 			sqlite3_reset (cursor->stmt);
 		} else {
+			/* only one statement can be active at the same time per interface */
+			iface->cancellable = cancellable;
 			result = sqlite3_step (cursor->stmt);
+			iface->cancellable = NULL;
 		}
 
 		if (result == SQLITE_INTERRUPT) {
@@ -1227,7 +1221,7 @@ tracker_db_statement_execute (TrackerDBStatement          *stmt,
 {
 	g_return_val_if_fail (!stmt->stmt_is_sunk, NULL);
 
-	return create_result_set_from_stmt (stmt->db_interface, stmt->stmt, error);
+	return create_result_set_from_stmt (stmt->db_interface, stmt->stmt, NULL, error);
 }
 
 TrackerDBCursor *
diff --git a/src/libtracker-data/tracker-db-interface.h b/src/libtracker-data/tracker-db-interface.h
index ee5a278..743efc3 100644
--- a/src/libtracker-data/tracker-db-interface.h
+++ b/src/libtracker-data/tracker-db-interface.h
@@ -21,6 +21,7 @@
 #define __LIBTRACKER_DB_INTERFACE_H__
 
 #include <glib-object.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -95,8 +96,6 @@ TrackerDBResultSet *tracker_db_interface_execute_query     (TrackerDBInterface
                                                             const gchar          *query,
                                                             ...) G_GNUC_PRINTF (3, 4);
 
-gboolean            tracker_db_interface_interrupt         (TrackerDBInterface  *interface);
-void                tracker_db_interface_reset_interrupt   (TrackerDBInterface  *interface);
 gboolean            tracker_db_interface_start_transaction (TrackerDBInterface  *interface);
 gboolean            tracker_db_interface_end_db_transaction(TrackerDBInterface  *interface);
 void                tracker_db_statement_bind_double       (TrackerDBStatement  *stmt,
@@ -136,6 +135,7 @@ guint               tracker_db_result_set_get_n_rows       (TrackerDBResultSet
 /* Functions to deal with a cursor */
 void                tracker_db_cursor_rewind               (TrackerDBCursor     *cursor);
 gboolean            tracker_db_cursor_iter_next            (TrackerDBCursor     *cursor,
+                                                            GCancellable        *cancellable,
                                                             GError             **error);
 guint               tracker_db_cursor_get_n_columns        (TrackerDBCursor     *cursor);
 void                tracker_db_cursor_get_value            (TrackerDBCursor     *cursor,
diff --git a/src/libtracker-data/tracker-db-manager.c b/src/libtracker-data/tracker-db-manager.c
index 0aa3492..f61b41a 100644
--- a/src/libtracker-data/tracker-db-manager.c
+++ b/src/libtracker-data/tracker-db-manager.c
@@ -949,7 +949,7 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 				}
 
 				if (cursor) {
-					if (tracker_db_cursor_iter_next (cursor, NULL)) {
+					if (tracker_db_cursor_iter_next (cursor, NULL, NULL)) {
 						if (g_strcmp0 (tracker_db_cursor_get_string (cursor, 0, NULL), "ok") != 0) {
 							must_recreate = TRUE;
 						}
@@ -1639,50 +1639,3 @@ tracker_db_manager_set_last_crawl_done (gboolean done)
 
 	g_free (filename);
 }
-
-/**
- * tracker_db_manager_interrupt_thread:
- * @thread: a #GThread to be interrupted
- *
- * Interrupts any ongoing DB operation going on on @thread.
- *
- * Returns: %TRUE if DB operations were interrupted, %FALSE otherwise.
- **/
-gboolean
-tracker_db_manager_interrupt_thread (GThread *thread)
-{
-	TrackerDBInterface *interface;
-
-	g_static_mutex_lock (&thread_ifaces_mutex);
-	interface = g_hash_table_lookup (thread_ifaces, thread);
-	g_static_mutex_unlock (&thread_ifaces_mutex);
-
-	if (!interface) {
-		return FALSE;
-	}
-
-	return tracker_db_interface_interrupt (interface);
-}
-
-/**
- * tracker_db_manager_interrupt_thread_reset:
- * @thread: a #GThread to be reset
- *
- * Reset @thread's interrupt state
- *
- **/
-void
-tracker_db_manager_interrupt_thread_reset (GThread *thread)
-{
-	TrackerDBInterface *interface;
-
-	g_static_mutex_lock (&thread_ifaces_mutex);
-	interface = g_hash_table_lookup (thread_ifaces, thread);
-	g_static_mutex_unlock (&thread_ifaces_mutex);
-
-	if (!interface) {
-		return;
-	}
-
-	tracker_db_interface_reset_interrupt (interface);
-}
diff --git a/src/libtracker-data/tracker-db-manager.h b/src/libtracker-data/tracker-db-manager.h
index c69b48d..99da6c0 100644
--- a/src/libtracker-data/tracker-db-manager.h
+++ b/src/libtracker-data/tracker-db-manager.h
@@ -65,9 +65,6 @@ gboolean            tracker_db_manager_has_enough_space       (void);
 TrackerDBManagerFlags
                     tracker_db_manager_get_flags              (void);
 
-gboolean            tracker_db_manager_interrupt_thread       (GThread *thread);
-void                tracker_db_manager_interrupt_thread_reset (GThread *thread);
-
 gboolean            tracker_db_manager_get_first_index_done   (void);
 guint64             tracker_db_manager_get_last_crawl_done    (void);
 
diff --git a/src/tracker-store/tracker-resources.c b/src/tracker-store/tracker-resources.c
index 4a3d346..3d33bb0 100644
--- a/src/tracker-store/tracker-resources.c
+++ b/src/tracker-store/tracker-resources.c
@@ -238,7 +238,7 @@ query_callback (gpointer inthread_data, GError *error, gpointer user_data)
 }
 
 static gpointer
-query_inthread (TrackerDBCursor *cursor, GError *error, gpointer user_data)
+query_inthread (TrackerDBCursor *cursor, GCancellable *cancellable, GError *error, gpointer user_data)
 {
 	InThreadPtr *ptr = g_slice_new0 (InThreadPtr);
 	TrackerDBusMethodInfo *info = user_data;
@@ -267,7 +267,7 @@ query_inthread (TrackerDBCursor *cursor, GError *error, gpointer user_data)
 
 	cont = TRUE;
 
-	while (tracker_db_cursor_iter_next (cursor, &loop_error) && cont) {
+	while (tracker_db_cursor_iter_next (cursor, cancellable, &loop_error) && cont) {
 		DBusMessageIter cols_iter;
 		guint i;
 
diff --git a/src/tracker-store/tracker-steroids.c b/src/tracker-store/tracker-steroids.c
index 6104787..8fae74b 100644
--- a/src/tracker-store/tracker-steroids.c
+++ b/src/tracker-store/tracker-steroids.c
@@ -232,6 +232,7 @@ update_blank_callback (GPtrArray *blank_nodes,
 
 static gpointer
 query_inthread (TrackerDBCursor *cursor,
+                GCancellable    *cancellable,
                 GError          *error,
                 gpointer         user_data)
 {
@@ -268,7 +269,7 @@ query_inthread (TrackerDBCursor *cursor,
 	column_offsets = g_malloc (n_columns * sizeof (gint));
 	column_data = g_malloc (n_columns * sizeof (gchar*));
 
-	while (tracker_db_cursor_iter_next (cursor, &loop_error)) {
+	while (tracker_db_cursor_iter_next (cursor, cancellable, &loop_error)) {
 		gint i;
 		guint last_offset = -1;
 
diff --git a/src/tracker-store/tracker-store.c b/src/tracker-store/tracker-store.c
index 8f3150c..44ae231 100644
--- a/src/tracker-store/tracker-store.c
+++ b/src/tracker-store/tracker-store.c
@@ -74,10 +74,10 @@ typedef struct {
 	TrackerStoreTaskType  type;
 	union {
 		struct {
-			gchar       *query;
-			GThread     *running_thread;
-			GTimer      *timer;
-			gpointer     thread_data;
+			gchar        *query;
+			GCancellable *cancellable;
+			GTimer       *timer;
+			gpointer      thread_data;
 		} query;
 		struct {
 			gchar        *query;
@@ -138,6 +138,9 @@ store_task_free (TrackerStoreTask *task)
 		if (task->data.query.timer) {
 			g_timer_destroy (task->data.query.timer);
 		}
+		if (task->data.query.cancellable) {
+			g_object_unref (task->data.query.cancellable);
+		}
 	} else {
 		g_free (task->data.update.query);
 	}
@@ -309,15 +312,15 @@ watchdog_cb (gpointer user_data)
 
 	while (running) {
 		TrackerStoreTask *task;
-		GThread *thread;
+		GCancellable *cancellable;
 
 		task = running->data;
 		running = running->next;
-		thread = task->data.query.running_thread;
+		cancellable = task->data.query.cancellable;
 
-		if (thread && private->max_task_time &&
+		if (cancellable && private->max_task_time &&
 		    g_timer_elapsed (task->data.query.timer, NULL) > private->max_task_time) {
-			tracker_data_manager_interrupt_thread (task->data.query.running_thread);
+			g_cancellable_cancel (cancellable);
 		}
 	}
 
@@ -353,6 +356,10 @@ task_finish_cb (gpointer data)
 	task = data;
 
 	if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
+		if (!task->error) {
+			g_cancellable_set_error_if_cancelled (task->data.query.cancellable, &task->error);
+		}
+
 		if (task->callback.query.query_callback) {
 			task->callback.query.query_callback (task->data.query.thread_data, task->error, task->user_data);
 		}
@@ -361,6 +368,7 @@ task_finish_cb (gpointer data)
 			g_clear_error (&task->error);
 		}
 
+		task->data.query.cancellable = g_cancellable_new ();
 		private->running_tasks = g_slist_remove (private->running_tasks, task);
 		check_running_tasks_watchdog (private);
 		private->n_queries_running--;
@@ -453,7 +461,6 @@ pool_dispatch_cb (gpointer data,
 {
 	TrackerStorePrivate *private;
 	TrackerStoreTask *task;
-	GThread *running_thread = g_thread_self ();
 
 #ifdef __USE_GNU
 	/* special task, only ever sent to main pool */
@@ -471,19 +478,15 @@ pool_dispatch_cb (gpointer data,
 	private = user_data;
 	task = data;
 
-	tracker_data_manager_interrupt_thread_reset (running_thread);
-
 	if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
 		TrackerDBCursor *cursor;
 
-		task->data.query.running_thread = running_thread;
 		cursor = tracker_data_query_sparql_cursor (task->data.query.query, &task->error);
 
-		task->data.query.thread_data = task->callback.query.in_thread (cursor, task->error, task->user_data);
+		task->data.query.thread_data = task->callback.query.in_thread (cursor, task->data.query.cancellable, task->error, task->user_data);
 
 		if (cursor)
 			g_object_unref (cursor);
-		task->data.query.running_thread = NULL;
 
 	} else if (task->type == TRACKER_STORE_TASK_TYPE_UPDATE) {
 		if (task->data.update.batch) {
@@ -883,9 +886,9 @@ tracker_store_unreg_batches (const gchar *client_id)
 
 		task = running->data;
 
-		if (task->data.query.running_thread &&
-                    g_strcmp0 (task->client_id, client_id) == 0) {
-			tracker_data_manager_interrupt_thread (task->data.query.running_thread);
+		if (task->data.query.cancellable &&
+		    g_strcmp0 (task->client_id, client_id) == 0) {
+			g_cancellable_cancel (task->data.query.cancellable);
 		}
 	}
 
diff --git a/src/tracker-store/tracker-store.h b/src/tracker-store/tracker-store.h
index d6a29f0..7b1b5a9 100644
--- a/src/tracker-store/tracker-store.h
+++ b/src/tracker-store/tracker-store.h
@@ -41,6 +41,7 @@ typedef void (* TrackerStoreSparqlQueryCallback)       (gpointer         data,
                                                         gpointer         user_data);
 typedef gpointer
              (* TrackerStoreSparqlQueryInThread)       (TrackerDBCursor *cursor,
+                                                        GCancellable    *cancellable,
                                                         GError          *error,
                                                         gpointer         user_data);
 typedef void (* TrackerStoreSparqlUpdateCallback)      (GError          *error,



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