sr: session: Moves sources to session struct
The sources really belong to the session, so move them into the session struct. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
parent
e6e8f8e053
commit
b7e941113f
11
libsigrok.h
11
libsigrok.h
|
@ -487,6 +487,17 @@ struct sr_session {
|
||||||
GSList *datafeed_callbacks;
|
GSList *datafeed_callbacks;
|
||||||
GTimeVal starttime;
|
GTimeVal starttime;
|
||||||
gboolean running;
|
gboolean running;
|
||||||
|
|
||||||
|
unsigned int num_sources;
|
||||||
|
|
||||||
|
/* Both "sources" and "pollfds" are of the same size and contain pairs of
|
||||||
|
* descriptor and callback function. We can not embed the GPollFD into the
|
||||||
|
* source struct since we want to be able to pass the array of all poll
|
||||||
|
* descriptors to g_poll.
|
||||||
|
*/
|
||||||
|
struct source *sources;
|
||||||
|
GPollFD *pollfds;
|
||||||
|
int source_timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
|
|
86
session.c
86
session.c
|
@ -39,16 +39,6 @@ struct source {
|
||||||
/* There can only be one session at a time. */
|
/* There can only be one session at a time. */
|
||||||
/* 'session' is not static, it's used elsewhere (via 'extern'). */
|
/* 'session' is not static, it's used elsewhere (via 'extern'). */
|
||||||
struct sr_session *session;
|
struct sr_session *session;
|
||||||
static int num_sources = 0;
|
|
||||||
|
|
||||||
/* Both "sources" and "pollfds" are of the same size and contain pairs of
|
|
||||||
* descriptor and callback function. We can not embed the GPollFD into the
|
|
||||||
* source struct since we want to be able to pass the array of all poll
|
|
||||||
* descriptors to g_poll.
|
|
||||||
*/
|
|
||||||
static struct source *sources = NULL;
|
|
||||||
static GPollFD *pollfds;
|
|
||||||
static int source_timeout = -1;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new session.
|
* Create a new session.
|
||||||
|
@ -65,6 +55,8 @@ SR_API struct sr_session *sr_session_new(void)
|
||||||
return NULL; /* TODO: SR_ERR_MALLOC? */
|
return NULL; /* TODO: SR_ERR_MALLOC? */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session->source_timeout = -1;
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,22 +205,23 @@ SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
|
||||||
*/
|
*/
|
||||||
static int sr_session_run_poll(void)
|
static int sr_session_run_poll(void)
|
||||||
{
|
{
|
||||||
int ret, i;
|
unsigned int i;
|
||||||
|
int ret;
|
||||||
|
|
||||||
while (session->running) {
|
while (session->running) {
|
||||||
ret = g_poll(pollfds, num_sources, source_timeout);
|
ret = g_poll(session->pollfds, session->num_sources, session->source_timeout);
|
||||||
|
|
||||||
for (i = 0; i < num_sources; i++) {
|
for (i = 0; i < session->num_sources; i++) {
|
||||||
if (pollfds[i].revents > 0 || (ret == 0
|
if (session->pollfds[i].revents > 0 || (ret == 0
|
||||||
&& source_timeout == sources[i].timeout)) {
|
&& session->source_timeout == session->sources[i].timeout)) {
|
||||||
/*
|
/*
|
||||||
* Invoke the source's callback on an event,
|
* Invoke the source's callback on an event,
|
||||||
* or if the poll timeout out and this source
|
* or if the poll timeout out and this source
|
||||||
* asked for that timeout.
|
* asked for that timeout.
|
||||||
*/
|
*/
|
||||||
if (!sources[i].cb(pollfds[i].fd, pollfds[i].revents,
|
if (!session->sources[i].cb(session->pollfds[i].fd, session->pollfds[i].revents,
|
||||||
sources[i].cb_data))
|
session->sources[i].cb_data))
|
||||||
sr_session_source_remove(pollfds[i].fd);
|
sr_session_source_remove(session->sources[i].poll_object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -308,10 +301,10 @@ SR_API int sr_session_run(void)
|
||||||
session->running = TRUE;
|
session->running = TRUE;
|
||||||
|
|
||||||
/* Do we have real sources? */
|
/* Do we have real sources? */
|
||||||
if (num_sources == 1 && pollfds[0].fd == -1) {
|
if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
|
||||||
/* Dummy source, freewheel over it. */
|
/* Dummy source, freewheel over it. */
|
||||||
while (session->running)
|
while (session->running)
|
||||||
sources[0].cb(-1, 0, sources[0].cb_data);
|
session->sources[0].cb(-1, 0, session->sources[0].cb_data);
|
||||||
} else {
|
} else {
|
||||||
/* Real sources, use g_poll() main loop. */
|
/* Real sources, use g_poll() main loop. */
|
||||||
sr_session_run_poll();
|
sr_session_run_poll();
|
||||||
|
@ -474,30 +467,31 @@ static int _sr_session_source_add(GPollFD *pollfd, int timeout,
|
||||||
|
|
||||||
/* Note: cb_data can be NULL, that's not a bug. */
|
/* Note: cb_data can be NULL, that's not a bug. */
|
||||||
|
|
||||||
new_pollfds = g_try_realloc(pollfds, sizeof(GPollFD) * (num_sources + 1));
|
new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * (session->num_sources + 1));
|
||||||
if (!new_pollfds) {
|
if (!new_pollfds) {
|
||||||
sr_err("session: %s: new_pollfds malloc failed", __func__);
|
sr_err("session: %s: new_pollfds malloc failed", __func__);
|
||||||
return SR_ERR_MALLOC;
|
return SR_ERR_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_sources = g_try_realloc(sources, sizeof(struct source) * (num_sources + 1));
|
new_sources = g_try_realloc(session->sources, sizeof(struct source) *
|
||||||
|
(session->num_sources + 1));
|
||||||
if (!new_sources) {
|
if (!new_sources) {
|
||||||
sr_err("session: %s: new_sources malloc failed", __func__);
|
sr_err("session: %s: new_sources malloc failed", __func__);
|
||||||
return SR_ERR_MALLOC;
|
return SR_ERR_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_pollfds[num_sources] = *pollfd;
|
new_pollfds[session->num_sources] = *pollfd;
|
||||||
s = &new_sources[num_sources++];
|
s = &new_sources[session->num_sources++];
|
||||||
s->timeout = timeout;
|
s->timeout = timeout;
|
||||||
s->cb = cb;
|
s->cb = cb;
|
||||||
s->cb_data = cb_data;
|
s->cb_data = cb_data;
|
||||||
s->poll_object = poll_object;
|
s->poll_object = poll_object;
|
||||||
pollfds = new_pollfds;
|
session->pollfds = new_pollfds;
|
||||||
sources = new_sources;
|
session->sources = new_sources;
|
||||||
|
|
||||||
if (timeout != source_timeout && timeout > 0
|
if (timeout != session->source_timeout && timeout > 0
|
||||||
&& (source_timeout == -1 || timeout < source_timeout))
|
&& (session->source_timeout == -1 || timeout < session->source_timeout))
|
||||||
source_timeout = timeout;
|
session->source_timeout = timeout;
|
||||||
|
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
@ -580,45 +574,45 @@ static int _sr_session_source_remove(gintptr poll_object)
|
||||||
{
|
{
|
||||||
struct source *new_sources;
|
struct source *new_sources;
|
||||||
GPollFD *new_pollfds;
|
GPollFD *new_pollfds;
|
||||||
int old;
|
unsigned int old;
|
||||||
|
|
||||||
if (!sources) {
|
if (!session->sources || !session->num_sources) {
|
||||||
sr_err("session: %s: sources was NULL", __func__);
|
sr_err("session: %s: sources was NULL", __func__);
|
||||||
return SR_ERR_BUG;
|
return SR_ERR_BUG;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (old = 0; old < num_sources; old++) {
|
for (old = 0; old < session->num_sources; old++) {
|
||||||
if (sources[old].poll_object == poll_object)
|
if (session->sources[old].poll_object == poll_object)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fd not found, nothing to do */
|
/* fd not found, nothing to do */
|
||||||
if (old == num_sources)
|
if (old == session->num_sources)
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
|
|
||||||
num_sources -= 1;
|
session->num_sources -= 1;
|
||||||
|
|
||||||
if (old != num_sources) {
|
if (old != session->num_sources) {
|
||||||
memmove(&pollfds[old], &pollfds[old+1],
|
memmove(&session->pollfds[old], &session->pollfds[old+1],
|
||||||
(num_sources - old) * sizeof(GPollFD));
|
(session->num_sources - old) * sizeof(GPollFD));
|
||||||
memmove(&sources[old], &sources[old+1],
|
memmove(&session->sources[old], &session->sources[old+1],
|
||||||
(num_sources - old) * sizeof(struct source));
|
(session->num_sources - old) * sizeof(struct source));
|
||||||
}
|
}
|
||||||
|
|
||||||
new_pollfds = g_try_realloc(pollfds, sizeof(GPollFD) * num_sources);
|
new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
|
||||||
if (!new_pollfds && num_sources > 0) {
|
if (!new_pollfds && session->num_sources > 0) {
|
||||||
sr_err("session: %s: new_pollfds malloc failed", __func__);
|
sr_err("session: %s: new_pollfds malloc failed", __func__);
|
||||||
return SR_ERR_MALLOC;
|
return SR_ERR_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_sources = g_try_realloc(sources, sizeof(struct source) * num_sources);
|
new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
|
||||||
if (!new_sources && num_sources > 0) {
|
if (!new_sources && session->num_sources > 0) {
|
||||||
sr_err("session: %s: new_sources malloc failed", __func__);
|
sr_err("session: %s: new_sources malloc failed", __func__);
|
||||||
return SR_ERR_MALLOC;
|
return SR_ERR_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
pollfds = new_pollfds;
|
session->pollfds = new_pollfds;
|
||||||
sources = new_sources;
|
session->sources = new_sources;
|
||||||
|
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue