+static unsigned int session_source_attach(struct sr_session *session,
+ GSource *source)
+{
+ unsigned int id = 0;
+
+ g_mutex_lock(&session->main_mutex);
+
+ if (session->main_context)
+ id = g_source_attach(source, session->main_context);
+ else
+ sr_err("Cannot add event source without main context.");
+
+ g_mutex_unlock(&session->main_mutex);
+
+ return id;
+}
+
+/* Idle handler; invoked when the number of registered event sources
+ * for a running session drops to zero.
+ */
+static gboolean delayed_stop_check(void *data)
+{
+ struct sr_session *session;
+
+ session = data;
+ session->stop_check_id = 0;
+
+ /* Session already ended? */
+ if (!session->running)
+ return G_SOURCE_REMOVE;
+
+ /* New event sources may have been installed in the meantime. */
+ if (g_hash_table_size(session->event_sources) != 0)
+ return G_SOURCE_REMOVE;
+
+ session->running = FALSE;
+ unset_main_context(session);
+
+ sr_info("Stopped.");
+
+ /* This indicates a bug in user code, since it is not valid to
+ * restart or destroy a session while it may still be running.
+ */
+ if (!session->main_loop && !session->stopped_callback) {
+ sr_err("BUG: Session stop left unhandled.");
+ return G_SOURCE_REMOVE;
+ }
+ if (session->main_loop)
+ g_main_loop_quit(session->main_loop);
+
+ if (session->stopped_callback)
+ (*session->stopped_callback)(session->stopped_cb_data);
+
+ return G_SOURCE_REMOVE;
+}
+
+static int stop_check_later(struct sr_session *session)
+{
+ GSource *source;
+ unsigned int source_id;
+
+ if (session->stop_check_id != 0)
+ return SR_OK; /* idle handler already installed */
+
+ source = g_idle_source_new();
+ g_source_set_callback(source, &delayed_stop_check, session, NULL);
+
+ source_id = session_source_attach(session, source);
+ session->stop_check_id = source_id;
+
+ g_source_unref(source);
+
+ return (source_id != 0) ? SR_OK : SR_ERR;
+}
+