session: Keep reference to main context while running

This commit is contained in:
Daniel Elstner 2015-10-09 15:49:12 +02:00
parent 5de0fc55a6
commit 2e5e3df4e4
2 changed files with 17 additions and 25 deletions

View File

@ -706,15 +706,10 @@ struct sr_session {
/** User data to be passed to the session stop callback. */ /** User data to be passed to the session stop callback. */
void *stopped_cb_data; void *stopped_cb_data;
/** Mutex protecting the main context pointer and ownership flag. */ /** Mutex protecting the main context pointer. */
GMutex main_mutex; GMutex main_mutex;
/** Context of the session main loop. */ /** Context of the session main loop. */
GMainContext *main_context; 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. */ /** Registered event sources for this session. */
GHashTable *event_sources; GHashTable *event_sources;
@ -722,6 +717,8 @@ struct sr_session {
GMainLoop *main_loop; GMainLoop *main_loop;
/** ID of idle source for dispatching the session stop notification. */ /** ID of idle source for dispatching the session stop notification. */
unsigned int stop_check_id; 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, SR_PRIV int sr_session_source_add_internal(struct sr_session *session,

View File

@ -556,41 +556,38 @@ static int verify_trigger(struct sr_trigger *trigger)
*/ */
static int set_main_context(struct sr_session *session) 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 /* May happen if sr_session_start() is called a second time
* while the session is still running. * while the session is still running.
*/ */
if (session->main_context) { if (session->main_context) {
sr_err("Main context already set."); sr_err("Main context already set.");
g_mutex_unlock(&session->main_mutex);
return SR_ERR; return SR_ERR;
} }
main_context = g_main_context_ref_thread_default();
g_mutex_lock(&session->main_mutex);
def_context = g_main_context_get_thread_default();
if (!def_context)
def_context = g_main_context_default();
/* /*
* Try to use an existing main context if possible, but only if we * Try to use an existing main context if possible, but only if we
* can make it owned by the current thread. Otherwise, create our * can make it owned by the current thread. Otherwise, create our
* own main context so that event source callbacks can execute in * own main context so that event source callbacks can execute in
* the session thread. * the session thread.
*/ */
if (g_main_context_acquire(def_context)) { if (g_main_context_acquire(main_context)) {
g_main_context_release(def_context); g_main_context_release(main_context);
sr_dbg("Using thread-default main context."); sr_dbg("Using thread-default main context.");
session->main_context = def_context;
session->main_context_is_default = TRUE;
} else { } else {
sr_dbg("Creating our own main context."); g_main_context_unref(main_context);
session->main_context = g_main_context_new(); sr_dbg("Creating our own main context.");
session->main_context_is_default = FALSE; main_context = g_main_context_new();
} }
session->main_context = main_context;
g_mutex_unlock(&session->main_mutex); g_mutex_unlock(&session->main_mutex);
return SR_OK; return SR_OK;
@ -611,9 +608,7 @@ static int unset_main_context(struct sr_session *session)
g_mutex_lock(&session->main_mutex); g_mutex_lock(&session->main_mutex);
if (session->main_context) { 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; session->main_context = NULL;
ret = SR_OK; ret = SR_OK;
} else { } else {