]> sigrok.org Git - libsigrok.git/commitdiff
Add sr_session_iteration() API function
authorBert Vermeulen <redacted>
Mon, 15 Jul 2013 12:14:28 +0000 (14:14 +0200)
committerBert Vermeulen <redacted>
Tue, 16 Jul 2013 14:08:38 +0000 (16:08 +0200)
proto.h
session.c

diff --git a/proto.h b/proto.h
index 821846f223abb66e91fc0e29049245a49572feb6..b1adb35fd234d1060f333d18d21bd350d952dc52 100644 (file)
--- 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);
index 4fa5dafea6ac48722dc6752cd5ae38519ce210fd..720b5ae10819908b63018e1c8095cbfceed5bc49 100644 (file)
--- 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;