Commit 2086e731 authored by Jürg Billeter's avatar Jürg Billeter

libtracker-sparql: Use separate thread for sync init if necessary

tracker_locale_init must run with the thread-default context set to the
global default context. As pushing a new thread-default context
requires the current thread to be the owner of the passed context, we
cannot always push the global default context as a thread-default
context.

This commit uses a threadpool job in the case that sync initialization
is requestesd with the thread-default context not set to the global
default context.
parent d3f6f533
......@@ -199,7 +199,7 @@ class Tracker.Sparql.Backend : Connection {
static bool log_initialized;
static StaticMutex door;
public static new Connection get_internal (bool is_direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError, DBusError, SpawnError {
static new Connection get (bool is_direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError, DBusError, SpawnError {
door.lock ();
// assign to owned variable to ensure it doesn't get freed between unlock and return
......@@ -230,6 +230,31 @@ class Tracker.Sparql.Backend : Connection {
return singleton;
}
public static new Connection get_internal (bool is_direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError, DBusError, SpawnError {
if (MainContext.get_thread_default () == null) {
// ok to initialize without extra thread
return get (is_direct_only, cancellable);
}
// run with separate main context to be able to wait for async method
var context = new MainContext ();
var loop = new MainLoop (context);
AsyncResult async_result = null;
context.push_thread_default ();
get_internal_async.begin (is_direct_only, cancellable, (obj, res) => {
async_result = res;
loop.quit ();
});
loop.run ();
context.pop_thread_default ();
return get_internal_async.end (async_result);
}
public async static new Connection get_internal_async (bool is_direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError, DBusError, SpawnError {
// fast path: avoid extra thread if connection is already available
if (door.trylock ()) {
......@@ -254,7 +279,7 @@ class Tracker.Sparql.Backend : Connection {
g_io_scheduler_push_job (job => {
try {
result = get_internal (is_direct_only, cancellable);
result = get (is_direct_only, cancellable);
} catch (IOError e_io) {
io_error = e_io;
} catch (Sparql.Error e_spql) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment