From: Daniel Elstner Date: Fri, 9 Oct 2015 13:49:12 +0000 (+0200) Subject: session: Keep reference to main context while running X-Git-Tag: libsigrok-0.4.0~217 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=2e5e3df4e4a18e3c539220c650f2e2ba188430c7;p=libsigrok.git session: Keep reference to main context while running --- diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index c3868b44..184c3831 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -706,15 +706,10 @@ struct sr_session { /** User data to be passed to the session stop callback. */ void *stopped_cb_data; - /** Mutex protecting the main context pointer and ownership flag. */ + /** Mutex protecting the main context pointer. */ GMutex main_mutex; /** Context of the session main loop. */ GMainContext *main_context; - /** Whether we are using the thread's default context. */ - gboolean main_context_is_default; - - /** Whether the session has been started. */ - gboolean running; /** Registered event sources for this session. */ GHashTable *event_sources; @@ -722,6 +717,8 @@ struct sr_session { GMainLoop *main_loop; /** ID of idle source for dispatching the session stop notification. */ unsigned int stop_check_id; + /** Whether the session has been started. */ + gboolean running; }; SR_PRIV int sr_session_source_add_internal(struct sr_session *session, diff --git a/src/session.c b/src/session.c index 0035a784..3750cdc0 100644 --- a/src/session.c +++ b/src/session.c @@ -556,41 +556,38 @@ static int verify_trigger(struct sr_trigger *trigger) */ static int set_main_context(struct sr_session *session) { - GMainContext *def_context; + GMainContext *main_context; + + g_mutex_lock(&session->main_mutex); /* May happen if sr_session_start() is called a second time * while the session is still running. */ if (session->main_context) { sr_err("Main context already set."); + + g_mutex_unlock(&session->main_mutex); return SR_ERR; } - - g_mutex_lock(&session->main_mutex); - - def_context = g_main_context_get_thread_default(); - - if (!def_context) - def_context = g_main_context_default(); + main_context = g_main_context_ref_thread_default(); /* * Try to use an existing main context if possible, but only if we * can make it owned by the current thread. Otherwise, create our * own main context so that event source callbacks can execute in * the session thread. */ - if (g_main_context_acquire(def_context)) { - g_main_context_release(def_context); + if (g_main_context_acquire(main_context)) { + g_main_context_release(main_context); sr_dbg("Using thread-default main context."); - - session->main_context = def_context; - session->main_context_is_default = TRUE; } else { - sr_dbg("Creating our own main context."); + g_main_context_unref(main_context); - session->main_context = g_main_context_new(); - session->main_context_is_default = FALSE; + sr_dbg("Creating our own main context."); + main_context = g_main_context_new(); } + session->main_context = main_context; + g_mutex_unlock(&session->main_mutex); return SR_OK; @@ -611,9 +608,7 @@ static int unset_main_context(struct sr_session *session) g_mutex_lock(&session->main_mutex); if (session->main_context) { - if (!session->main_context_is_default) - g_main_context_unref(session->main_context); - + g_main_context_unref(session->main_context); session->main_context = NULL; ret = SR_OK; } else {