[tracker/direct-access] libtracker-data: Replace interrupt_thread by GCancellable
- From: Jürg Billeter <juergbi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/direct-access] libtracker-data: Replace interrupt_thread by GCancellable
- Date: Tue, 6 Jul 2010 15:56:15 +0000 (UTC)
commit b5c6f815afd7bbaecfbceb688ce30a87c80f28dd
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 | 28 +++--------
src/libtracker-data/tracker-data-manager.h | 2 -
src/libtracker-data/tracker-data-query.c | 4 +-
src/libtracker-data/tracker-data-update.c | 4 +-
src/libtracker-data/tracker-db-interface-sqlite.c | 36 ++++++---------
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/libtracker-sparql/tracker-cursor.vala | 2 +-
src/tracker-store/tracker-resources.c | 4 +-
src/tracker-store/tracker-steroids.c | 3 +-
src/tracker-store/tracker-store.c | 36 ++++++++-------
src/tracker-store/tracker-store.h | 1 +
13 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 27d595f..2b0622e 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -1212,7 +1212,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;
@@ -1249,7 +1249,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;
@@ -1281,7 +1281,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;
@@ -1319,7 +1319,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;
@@ -1358,7 +1358,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;
@@ -1444,7 +1444,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;
@@ -2227,7 +2227,7 @@ get_new_service_id (TrackerDBInterface *iface)
}
if (cursor) {
- tracker_db_cursor_iter_next (cursor, &error);
+ tracker_db_cursor_iter_next (cursor, NULL, &error);
max_service_id = tracker_db_cursor_get_int (cursor, 0);
g_object_unref (cursor);
}
@@ -2484,7 +2484,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 */
@@ -2711,15 +2711,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 71d7e8b..3eec9ce 100644
--- a/src/libtracker-data/tracker-data-manager.h
+++ b/src/libtracker-data/tracker-data-manager.h
@@ -46,8 +46,6 @@ 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,
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 cc4e0d3..5e17841 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 7475a6f..66fb85b 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -49,7 +49,7 @@ struct TrackerDBInterface {
#if HAVE_TRACKER_FTS
guint fts_initialized : 1;
#endif
- volatile gint interrupt;
+ GCancellable *cancellable;
};
struct TrackerDBInterfaceClass {
@@ -451,7 +451,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)
{
@@ -1049,7 +1039,7 @@ tracker_db_cursor_class_init (TrackerDBCursorClass *class)
sparql_cursor_class->get_n_columns = (gint (*) (TrackerSparqlCursor *)) tracker_db_cursor_get_n_columns;
sparql_cursor_class->get_string = (const gchar * (*) (TrackerSparqlCursor *, gint, gint*)) tracker_db_cursor_get_string;
- sparql_cursor_class->iter_next = (gboolean (*) (TrackerSparqlCursor *, GError **)) tracker_db_cursor_iter_next;
+ sparql_cursor_class->iter_next = (gboolean (*) (TrackerSparqlCursor *, GCancellable *, GError **)) tracker_db_cursor_iter_next;
sparql_cursor_class->rewind = (void (*) (TrackerSparqlCursor *)) tracker_db_cursor_rewind;
}
@@ -1131,6 +1121,7 @@ tracker_db_cursor_rewind (TrackerDBCursor *cursor)
gboolean
tracker_db_cursor_iter_next (TrackerDBCursor *cursor,
+ GCancellable *cancellable,
GError **error)
{
TrackerDBStatement *stmt = cursor->ref_stmt;
@@ -1139,11 +1130,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) {
@@ -1233,7 +1227,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 1858eeb..14277ff 100644
--- a/src/libtracker-data/tracker-db-manager.c
+++ b/src/libtracker-data/tracker-db-manager.c
@@ -887,7 +887,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;
}
@@ -1452,50 +1452,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/libtracker-sparql/tracker-cursor.vala b/src/libtracker-sparql/tracker-cursor.vala
index c30861a..621e275 100644
--- a/src/libtracker-sparql/tracker-cursor.vala
+++ b/src/libtracker-sparql/tracker-cursor.vala
@@ -25,6 +25,6 @@ public abstract class Tracker.Sparql.Cursor : Object {
return false;
}
- public abstract bool iter_next () throws GLib.Error;
+ public abstract bool iter_next (Cancellable? cancellable = null) throws GLib.Error;
public abstract void rewind ();
}
diff --git a/src/tracker-store/tracker-resources.c b/src/tracker-store/tracker-resources.c
index 3742dcc..83367b8 100644
--- a/src/tracker-store/tracker-resources.c
+++ b/src/tracker-store/tracker-resources.c
@@ -242,7 +242,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;
@@ -271,7 +271,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 93a650c..e435f16 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 e928305..d2a4ab0 100644
--- a/src/tracker-store/tracker-store.c
+++ b/src/tracker-store/tracker-store.c
@@ -69,10 +69,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;
@@ -127,6 +127,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);
}
@@ -151,15 +154,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);
}
}
@@ -205,6 +208,8 @@ sched (TrackerStorePrivate *private)
break;
}
+ task->data.query.cancellable = g_cancellable_new ();
+
private->running_tasks = g_slist_prepend (private->running_tasks, task);
ensure_running_tasks_watchdog (private);
private->n_queries_running++;
@@ -240,6 +245,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);
}
@@ -327,7 +336,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 */
@@ -345,19 +353,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) {
tracker_data_update_sparql (task->data.update.query, &task->error);
@@ -616,9 +620,9 @@ tracker_store_unreg_batches (const gchar *client_id)
task = running->data;
- if (task->data.query.running_thread &&
+ if (task->data.query.cancellable &&
g_strcmp0 (task->client_id, client_id) == 0) {
- tracker_data_manager_interrupt_thread (task->data.query.running_thread);
+ g_cancellable_cancel (task->data.query.cancellable);
}
}
diff --git a/src/tracker-store/tracker-store.h b/src/tracker-store/tracker-store.h
index b0d26a9..6a507e0 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]