Index: rhythmdb/rhythmdb.c
===================================================================
RCS file: /cvs/gnome/rhythmbox/rhythmdb/rhythmdb.c,v
retrieving revision 1.143
diff -u -u -r1.143 rhythmdb.c
--- rhythmdb/rhythmdb.c	6 Dec 2005 02:23:08 -0000	1.143
+++ rhythmdb/rhythmdb.c	6 Dec 2005 06:49:24 -0000
@@ -220,6 +220,7 @@
 static void rhythmdb_monitor_uri_path (RhythmDB *db,
 				       const char *uri,
 				       GError **error);
+static void add_commit_timeout (RhythmDB *db);
 
 enum
 {
@@ -519,8 +520,6 @@
 	db->priv->changed_files = g_hash_table_new_full (g_str_hash, g_str_equal,
 							 (GDestroyNotify) g_free,
 							 NULL);
-	db->priv->changed_files_id = g_timeout_add (RHYTHMDB_FILE_MODIFY_PROCESS_TIME * 1000,
-						    (GSourceFunc) rhythmdb_process_changed_files, db);
 
 	db->priv->changed_entries = g_hash_table_new (NULL, NULL);
 	
@@ -816,13 +815,26 @@
 	g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_CHANGED], 0, entry, changes);
 }
 
-static void
-rhythmdb_commit_internal (RhythmDB *db, gboolean signal_changed)
+/**
+ * rhythmdb_commit:
+ * @db: a #RhythmDB.
+ *
+ * Apply all database changes, and send notification of changes and new entries.
+ * This needs to be called after any changes have been made, such as a group of
+ * rhythmdb_entry_set() calls, or a new entry has been added.
+ **/
+void
+rhythmdb_commit (RhythmDB *db)
 {
 	GList *tem;
 
-	if (signal_changed)
-		g_hash_table_foreach (db->priv->changed_entries, (GHFunc) emit_entry_changed, db);
+	/* only commit now if we're in the main thread */
+	if (!rb_is_main_thread ()) {
+		add_commit_timeout (db);
+		return;
+	}
+
+	g_hash_table_foreach (db->priv->changed_entries, (GHFunc) emit_entry_changed, db);
 	g_hash_table_foreach_remove (db->priv->changed_entries, (GHRFunc) free_entry_changes, db);
 
 	for (tem = db->priv->added_entries; tem; tem = tem->next) {
@@ -844,29 +856,26 @@
 }
 
 
-/**
- * rhythmdb_commit:
- * @db: a #RhythmDB.
- *
- * Apply all database changes, and send notification of changes and new entries.
- * This needs to be called after any changes have been made, such as a group of
- * rhythmdb_entry_set() calls, or a new entry has been added.
- **/
-void
-rhythmdb_commit (RhythmDB *db)
-{
-	rhythmdb_commit_internal (db, TRUE);
-}
-
 static gboolean
 timeout_rhythmdb_commit (RhythmDB *db)
 {
 	db->priv->commit_timeout_id = 0;
-	rhythmdb_commit_internal (RHYTHMDB (db), TRUE);
+	rhythmdb_commit (RHYTHMDB (db));
 
 	return FALSE;
 }
 
+static void
+add_commit_timeout (RhythmDB *db)
+{
+	static GStaticMutex commit_timeout_mutex = G_STATIC_MUTEX_INIT;
+
+	g_static_mutex_lock (&commit_timeout_mutex);
+	if (!db->priv->commit_timeout_id)
+		db->priv->commit_timeout_id = g_timeout_add (100, (GSourceFunc)timeout_rhythmdb_commit, db);
+	g_static_mutex_unlock (&commit_timeout_mutex);
+}
+
 
 GQuark
 rhythmdb_error_quark (void)
@@ -1547,7 +1557,7 @@
 	/* FIXME: watch for errors */
 	rhythmdb_monitor_uri_path (db, entry->location, NULL);
 
-	rhythmdb_commit_internal (db, TRUE);
+	rhythmdb_commit (db);
 	
 	return TRUE;
 }
@@ -1564,9 +1574,7 @@
 	 * we can run a single commit for several queued 
 	 * entry_set
 	 */
-	if (!db->priv->commit_timeout_id) {
-		db->priv->commit_timeout_id = g_timeout_add (100, (GSourceFunc)timeout_rhythmdb_commit, db);
-	}
+	add_commit_timeout (db);
 }
 
 static void
@@ -2042,7 +2050,39 @@
 	}
 }
 
-/**
+static gpointer 	 
+rhythmdb_load_thread_main (RhythmDB *db) 	 
+{ 	 
+	RhythmDBEvent *result; 	 
+	RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db); 	 
+
+	g_mutex_lock (db->priv->saving_mutex);
+	klass->impl_load (db, &db->priv->exiting); 	 
+	g_mutex_unlock (db->priv->saving_mutex);
+
+	/* begin monitoring the for new tracks */ 	 
+	db->priv->library_location_notify_id = 	 
+		eel_gconf_notification_add (CONF_LIBRARY_LOCATION, 	 
+					    (GConfClientNotifyFunc) library_location_changed_cb, 	 
+					    db); 	 
+	rhythmdb_sync_library_location (db); 	 
+	db->priv->changed_files_id = g_timeout_add (RHYTHMDB_FILE_MODIFY_PROCESS_TIME * 1000,
+						    (GSourceFunc) rhythmdb_process_changed_files, db);
+
+	rb_debug ("queuing db load complete signal"); 	 
+	result = g_new0 (RhythmDBEvent, 1); 	 
+	result->type = RHYTHMDB_EVENT_DB_LOAD; 	 
+	g_async_queue_push (db->priv->event_queue, result); 	 
+  	 
+	rb_debug ("exiting"); 	 
+	result = g_new0 (RhythmDBEvent, 1); 	 
+	result->type = RHYTHMDB_EVENT_THREAD_EXITED; 	 
+	g_async_queue_push (db->priv->event_queue, result); 	 
+
+	return NULL; 	 
+}
+
+/*
  * rhythmdb_load:
  * @db: a #RhythmDB.
  *
@@ -2051,6 +2091,8 @@
 void
 rhythmdb_load (RhythmDB *db)
 {
+	rhythmdb_thread_create (db, (GThreadFunc) rhythmdb_load_thread_main, db);
+#if 0	
 	RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
 	RhythmDBEvent *result;
 
@@ -2065,12 +2107,15 @@
 					    (GConfClientNotifyFunc) library_location_changed_cb,
 					    db);
 	rhythmdb_sync_library_location (db);
+	db->priv->changed_files_id = g_timeout_add (RHYTHMDB_FILE_MODIFY_PROCESS_TIME * 1000,
+						    (GSourceFunc) rhythmdb_process_changed_files, db);
 
 	rb_debug ("queuing db load complete signal");
 	result = g_new0 (RhythmDBEvent, 1);
 	result->db = db;
 	result->type = RHYTHMDB_EVENT_DB_LOAD;
 	g_async_queue_push (db->priv->event_queue, result);
+#endif
 }
 
 static gpointer
