From: Bert Vermeulen Date: Mon, 15 Jul 2013 12:14:28 +0000 (+0200) Subject: Add sr_session_iteration() API function X-Git-Tag: libsigrok-0.2.1~29 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b483be7456fbc2e29efa50a49f6e8d60a8107527;p=libsigrok.git Add sr_session_iteration() API function --- diff --git a/proto.h b/proto.h index 821846f2..b1adb35f 100644 --- a/proto.h +++ b/proto.h @@ -97,6 +97,7 @@ SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb, void *cb_data); /* Session control */ +SR_API int sr_session_iteration(gboolean block); SR_API int sr_session_start(void); SR_API int sr_session_run(void); SR_API int sr_session_stop(void); diff --git a/session.c b/session.c index 4fa5dafe..720b5ae1 100644 --- a/session.c +++ b/session.c @@ -232,41 +232,48 @@ SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb, void *cb_ return SR_OK; } -static int sr_session_run_poll(void) +/** + * Call every device in the session's callback. + * + * For sessions not driven by select loops such as sr_session_run(), + * but driven by another scheduler, this can be used to poll the devices + * from within that scheduler. + * + * @return SR_OK upon success, SR_ERR on errors. + */ +SR_API int sr_session_iteration(gboolean block) { unsigned int i; int ret; - while (session->num_sources > 0) { - ret = g_poll(session->pollfds, session->num_sources, - session->source_timeout); - for (i = 0; i < session->num_sources; i++) { - if (session->pollfds[i].revents > 0 || (ret == 0 - && session->source_timeout == session->sources[i].timeout)) { - /* - * Invoke the source's callback on an event, - * or if the poll timed out and this source - * asked for that timeout. - */ - if (!session->sources[i].cb(session->pollfds[i].fd, - session->pollfds[i].revents, - session->sources[i].cb_data)) - sr_session_source_remove(session->sources[i].poll_object); - } + ret = g_poll(session->pollfds, session->num_sources, + block ? session->source_timeout : 0); + for (i = 0; i < session->num_sources; i++) { + if (session->pollfds[i].revents > 0 || (ret == 0 + && session->source_timeout == session->sources[i].timeout)) { /* - * We want to take as little time as possible to stop - * the session if we have been told to do so. Therefore, - * we check the flag after processing every source, not - * just once per main event loop. + * Invoke the source's callback on an event, + * or if the poll timed out and this source + * asked for that timeout. */ - g_mutex_lock(&session->stop_mutex); - if (session->abort_session) { - sr_session_stop_sync(); - /* But once is enough. */ - session->abort_session = FALSE; - } - g_mutex_unlock(&session->stop_mutex); + if (!session->sources[i].cb(session->pollfds[i].fd, + session->pollfds[i].revents, + session->sources[i].cb_data)) + sr_session_source_remove(session->sources[i].poll_object); + } + /* + * We want to take as little time as possible to stop + * the session if we have been told to do so. Therefore, + * we check the flag after processing every source, not + * just once per main event loop. + */ + g_mutex_lock(&session->stop_mutex); + if (session->abort_session) { + sr_session_stop_sync(); + /* But once is enough. */ + session->abort_session = FALSE; } + g_mutex_unlock(&session->stop_mutex); } return SR_OK; @@ -343,7 +350,8 @@ SR_API int sr_session_run(void) session->sources[0].cb(-1, 0, session->sources[0].cb_data); } else { /* Real sources, use g_poll() main loop. */ - sr_session_run_poll(); + while (session->num_sources) + sr_session_iteration(TRUE); } return SR_OK;