Remove monitor poll and hook in mainloop

Provide a mainloop SpaPoll and let the monitors hook into it.
This commit is contained in:
Wim Taymans 2016-10-07 18:19:03 +02:00
parent 2905d91467
commit 50c3b40cd0
8 changed files with 133 additions and 149 deletions

View file

@ -49,11 +49,6 @@ struct _PinosSpaALSAMonitorPrivate
URI uri; URI uri;
GSource *watch_source;
unsigned int n_poll;
SpaPollItem poll[16];
GHashTable *nodes; GHashTable *nodes;
}; };
@ -165,30 +160,12 @@ remove_item (PinosSpaALSAMonitor *this, SpaMonitorItem *item)
} }
} }
static gboolean
poll_event (GIOChannel *source,
GIOCondition condition,
gpointer user_data)
{
PinosSpaALSAMonitor *this = user_data;
PinosSpaALSAMonitorPrivate *priv = this->priv;
SpaPollNotifyData data;
data.user_data = priv->poll[0].user_data;
data.fds = priv->poll[0].fds;
data.n_fds = priv->poll[0].n_fds;
priv->poll[0].after_cb (&data);
return TRUE;
}
static void static void
on_monitor_event (SpaMonitor *monitor, on_monitor_event (SpaMonitor *monitor,
SpaMonitorEvent *event, SpaMonitorEvent *event,
void *user_data) void *user_data)
{ {
PinosSpaALSAMonitor *this = user_data; PinosSpaALSAMonitor *this = user_data;
PinosSpaALSAMonitorPrivate *priv = this->priv;
switch (event->type) { switch (event->type) {
case SPA_MONITOR_EVENT_TYPE_ADDED: case SPA_MONITOR_EVENT_TYPE_ADDED:
@ -208,31 +185,6 @@ on_monitor_event (SpaMonitor *monitor,
g_debug ("alsa-monitor %p: changed: \"%s\"", this, item->name); g_debug ("alsa-monitor %p: changed: \"%s\"", this, item->name);
break; break;
} }
case SPA_MONITOR_EVENT_TYPE_ADD_POLL:
{
SpaPollItem *item = event->data;
GIOChannel *channel;
priv->poll[priv->n_poll] = *item;
priv->n_poll++;
channel = g_io_channel_unix_new (item->fds[0].fd);
priv->watch_source = g_io_create_watch (channel, G_IO_IN);
g_io_channel_unref (channel);
g_source_set_callback (priv->watch_source, (GSourceFunc) poll_event, this, NULL);
g_source_attach (priv->watch_source, g_main_context_get_thread_default ());
g_source_unref (priv->watch_source);
break;
}
case SPA_MONITOR_EVENT_TYPE_UPDATE_POLL:
break;
case SPA_MONITOR_EVENT_TYPE_REMOVE_POLL:
{
priv->n_poll--;
g_source_destroy (priv->watch_source);
priv->watch_source = NULL;
break;
}
default: default:
break; break;
} }

View file

@ -49,11 +49,6 @@ struct _PinosSpaV4l2MonitorPrivate
URI uri; URI uri;
GSource *watch_source;
unsigned int n_poll;
SpaPollItem poll[16];
GHashTable *nodes; GHashTable *nodes;
}; };
@ -165,30 +160,12 @@ remove_item (PinosSpaV4l2Monitor *this, SpaMonitorItem *item)
} }
} }
static gboolean
poll_event (GIOChannel *source,
GIOCondition condition,
gpointer user_data)
{
PinosSpaV4l2Monitor *this = user_data;
PinosSpaV4l2MonitorPrivate *priv = this->priv;
SpaPollNotifyData data;
data.user_data = priv->poll[0].user_data;
data.fds = priv->poll[0].fds;
data.n_fds = priv->poll[0].n_fds;
priv->poll[0].after_cb (&data);
return TRUE;
}
static void static void
on_monitor_event (SpaMonitor *monitor, on_monitor_event (SpaMonitor *monitor,
SpaMonitorEvent *event, SpaMonitorEvent *event,
void *user_data) void *user_data)
{ {
PinosSpaV4l2Monitor *this = user_data; PinosSpaV4l2Monitor *this = user_data;
PinosSpaV4l2MonitorPrivate *priv = this->priv;
switch (event->type) { switch (event->type) {
case SPA_MONITOR_EVENT_TYPE_ADDED: case SPA_MONITOR_EVENT_TYPE_ADDED:
@ -208,31 +185,6 @@ on_monitor_event (SpaMonitor *monitor,
g_debug ("v4l2-monitor %p: changed: \"%s\"", this, item->name); g_debug ("v4l2-monitor %p: changed: \"%s\"", this, item->name);
break; break;
} }
case SPA_MONITOR_EVENT_TYPE_ADD_POLL:
{
SpaPollItem *item = event->data;
GIOChannel *channel;
priv->poll[priv->n_poll] = *item;
priv->n_poll++;
channel = g_io_channel_unix_new (item->fds[0].fd);
priv->watch_source = g_io_create_watch (channel, G_IO_IN);
g_io_channel_unref (channel);
g_source_set_callback (priv->watch_source, (GSourceFunc) poll_event, this, NULL);
g_source_attach (priv->watch_source, g_main_context_get_thread_default ());
g_source_unref (priv->watch_source);
break;
}
case SPA_MONITOR_EVENT_TYPE_UPDATE_POLL:
break;
case SPA_MONITOR_EVENT_TYPE_REMOVE_POLL:
{
priv->n_poll--;
g_source_destroy (priv->watch_source);
priv->watch_source = NULL;
break;
}
default: default:
break; break;
} }

View file

@ -57,8 +57,9 @@ struct _PinosDaemonPrivate
GHashTable *node_factories; GHashTable *node_factories;
SpaSupport support[3]; SpaSupport support[4];
SpaLog log; SpaLog log;
SpaPoll main_loop;
}; };
enum enum
@ -868,6 +869,69 @@ do_log (SpaLog *log,
va_end (args); va_end (args);
} }
typedef struct {
PinosDaemonPrivate *priv;
SpaPollItem item;
} PollData;
static gboolean
poll_event (GIOChannel *source,
GIOCondition condition,
gpointer user_data)
{
PollData *data = user_data;
SpaPollNotifyData d;
d.user_data = data->item.user_data;
d.fds = data->item.fds;
d.n_fds = data->item.n_fds;
data->item.after_cb (&d);
return TRUE;
}
static SpaResult
do_add_item (SpaPoll *poll,
SpaPollItem *item)
{
PinosDaemonPrivate *priv = SPA_CONTAINER_OF (poll, PinosDaemonPrivate, main_loop);
GIOChannel *channel;
GSource *source;
PollData data;
channel = g_io_channel_unix_new (item->fds[0].fd);
source = g_io_create_watch (channel, G_IO_IN);
g_io_channel_unref (channel);
data.priv = priv;
data.item = *item;
g_source_set_callback (source, (GSourceFunc) poll_event, g_slice_dup (PollData, &data) , NULL);
item->id = g_source_attach (source, g_main_context_get_thread_default ());
g_source_unref (source);
return SPA_RESULT_OK;
}
static SpaResult
do_update_item (SpaPoll *poll,
SpaPollItem *item)
{
return SPA_RESULT_OK;
}
static SpaResult
do_remove_item (SpaPoll *poll,
SpaPollItem *item)
{
GSource *source;
source = g_main_context_find_source_by_id (g_main_context_get_thread_default (), item->id);
g_source_destroy (source);
return SPA_RESULT_OK;
}
static void static void
pinos_daemon_init (PinosDaemon * daemon) pinos_daemon_init (PinosDaemon * daemon)
{ {
@ -886,6 +950,14 @@ pinos_daemon_init (PinosDaemon * daemon)
g_object_unref); g_object_unref);
priv->loop = pinos_rtloop_new(); priv->loop = pinos_rtloop_new();
priv->main_loop.handle = NULL;
priv->main_loop.size = sizeof (SpaPoll);
priv->main_loop.info = NULL;
priv->main_loop.info = NULL;
priv->main_loop.add_item = do_add_item;
priv->main_loop.update_item = do_update_item;
priv->main_loop.remove_item = do_remove_item;
priv->log.handle = NULL; priv->log.handle = NULL;
priv->log.size = sizeof (SpaLog); priv->log.size = sizeof (SpaLog);
priv->log.info = NULL; priv->log.info = NULL;
@ -902,8 +974,10 @@ pinos_daemon_init (PinosDaemon * daemon)
priv->support[1].data = daemon->log; priv->support[1].data = daemon->log;
priv->support[2].uri = SPA_POLL__DataLoop; priv->support[2].uri = SPA_POLL__DataLoop;
priv->support[2].data = &priv->loop->poll; priv->support[2].data = &priv->loop->poll;
priv->support[3].uri = SPA_POLL__MainLoop;
priv->support[3].data = &priv->main_loop;
daemon->support = priv->support; daemon->support = priv->support;
daemon->n_support = 3; daemon->n_support = 4;
} }
/** /**

View file

@ -65,18 +65,12 @@ typedef struct {
* @SPA_MONITOR_EVENT_TYPE_ADDED: an item was added, data points to #SpaMonitorItem * @SPA_MONITOR_EVENT_TYPE_ADDED: an item was added, data points to #SpaMonitorItem
* @SPA_MONITOR_EVENT_TYPE_REMOVED: an item was removed, data points to #SpaMonitorItem * @SPA_MONITOR_EVENT_TYPE_REMOVED: an item was removed, data points to #SpaMonitorItem
* @SPA_MONITOR_EVENT_TYPE_CHANGED: an item was changed, data points to #SpaMonitorItem * @SPA_MONITOR_EVENT_TYPE_CHANGED: an item was changed, data points to #SpaMonitorItem
* @SPA_MONITOR_EVENT_TYPE_ADD_POLL: add fd for polling, data points to #SpaPollItem
* @SPA_MONITOR_EVENT_TYPE_UPDATE_POLL: update fd for polling, data points to #SpaPollItem
* @SPA_MONITOR_EVENT_TYPE_REMOVE_POLL: remov fd for polling, data points to #SpaPollItem
*/ */
typedef enum { typedef enum {
SPA_MONITOR_EVENT_TYPE_INVALID = 0, SPA_MONITOR_EVENT_TYPE_INVALID = 0,
SPA_MONITOR_EVENT_TYPE_ADDED, SPA_MONITOR_EVENT_TYPE_ADDED,
SPA_MONITOR_EVENT_TYPE_REMOVED, SPA_MONITOR_EVENT_TYPE_REMOVED,
SPA_MONITOR_EVENT_TYPE_CHANGED, SPA_MONITOR_EVENT_TYPE_CHANGED,
SPA_MONITOR_EVENT_TYPE_ADD_POLL,
SPA_MONITOR_EVENT_TYPE_UPDATE_POLL,
SPA_MONITOR_EVENT_TYPE_REMOVE_POLL,
} SpaMonitorEventType; } SpaMonitorEventType;
typedef struct { typedef struct {

View file

@ -41,6 +41,7 @@ typedef struct _SpaNodeEvent SpaNodeEvent;
#define SPA_NODE_EVENT__Marker SPA_NODE_EVENT_PREFIX "Marker" #define SPA_NODE_EVENT__Marker SPA_NODE_EVENT_PREFIX "Marker"
#define SPA_NODE_EVENT__Error SPA_NODE_EVENT_PREFIX "Error" #define SPA_NODE_EVENT__Error SPA_NODE_EVENT_PREFIX "Error"
#define SPA_NODE_EVENT__Buffering SPA_NODE_EVENT_PREFIX "Buffering" #define SPA_NODE_EVENT__Buffering SPA_NODE_EVENT_PREFIX "Buffering"
#define SPA_NODE_EVENT__RequestRefresh SPA_NODE_EVENT_PREFIX "RequestRefresh"
#define SPA_NODE_EVENT__RequestClockUpdate SPA_NODE_EVENT_PREFIX "RequestClockUpdate" #define SPA_NODE_EVENT__RequestClockUpdate SPA_NODE_EVENT_PREFIX "RequestClockUpdate"
/** /**
@ -82,10 +83,6 @@ typedef struct {
SpaResult res; SpaResult res;
} SpaNodeEventAsyncComplete; } SpaNodeEventAsyncComplete;
typedef struct {
SpaNodeState state;
} SpaNodeEventStateChange;
typedef struct { typedef struct {
uint32_t port_id; uint32_t port_id;
} SpaNodeEventHaveOutput; } SpaNodeEventHaveOutput;

View file

@ -54,6 +54,7 @@ struct _SpaALSAMonitor {
URI uri; URI uri;
SpaIDMap *map; SpaIDMap *map;
SpaLog *log; SpaLog *log;
SpaPoll *main_loop;
SpaMonitorEventCallback event_cb; SpaMonitorEventCallback event_cb;
void *user_data; void *user_data;
@ -245,7 +246,6 @@ spa_alsa_monitor_set_event_callback (SpaMonitor *monitor,
{ {
SpaResult res; SpaResult res;
SpaALSAMonitor *this; SpaALSAMonitor *this;
SpaMonitorEvent event;
if (monitor == NULL || monitor->handle == NULL) if (monitor == NULL || monitor->handle == NULL)
return SPA_RESULT_INVALID_ARGUMENTS; return SPA_RESULT_INVALID_ARGUMENTS;
@ -269,11 +269,6 @@ spa_alsa_monitor_set_event_callback (SpaMonitor *monitor,
udev_monitor_enable_receiving (this->umonitor); udev_monitor_enable_receiving (this->umonitor);
this->fd = udev_monitor_get_fd (this->umonitor);; this->fd = udev_monitor_get_fd (this->umonitor);;
event.type = SPA_MONITOR_EVENT_TYPE_ADD_POLL;
event.data = &this->poll;
event.size = sizeof (this->poll);
this->fds[0].fd = this->fd; this->fds[0].fd = this->fd;
this->fds[0].events = POLLIN | POLLPRI | POLLERR; this->fds[0].events = POLLIN | POLLPRI | POLLERR;
this->fds[0].revents = 0; this->fds[0].revents = 0;
@ -286,12 +281,9 @@ spa_alsa_monitor_set_event_callback (SpaMonitor *monitor,
this->poll.before_cb = NULL; this->poll.before_cb = NULL;
this->poll.after_cb = alsa_on_fd_events; this->poll.after_cb = alsa_on_fd_events;
this->poll.user_data = this; this->poll.user_data = this;
this->event_cb (&this->monitor, &event, this->user_data); spa_poll_add_item (this->main_loop, &this->poll);
} else { } else {
event.type = SPA_MONITOR_EVENT_TYPE_REMOVE_POLL; spa_poll_remove_item (this->main_loop, &this->poll);
event.data = &this->poll;
event.size = sizeof (this->poll);
this->event_cb (&this->monitor, &event, this->user_data);
} }
return SPA_RESULT_OK; return SPA_RESULT_OK;
@ -407,11 +399,17 @@ alsa_monitor_init (const SpaHandleFactory *factory,
this->map = support[i].data; this->map = support[i].data;
else if (strcmp (support[i].uri, SPA_LOG_URI) == 0) else if (strcmp (support[i].uri, SPA_LOG_URI) == 0)
this->log = support[i].data; this->log = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__MainLoop) == 0)
this->main_loop = support[i].data;
} }
if (this->map == NULL) { if (this->map == NULL) {
spa_log_error (this->log, "an id-map is needed"); spa_log_error (this->log, "an id-map is needed");
return SPA_RESULT_ERROR; return SPA_RESULT_ERROR;
} }
if (this->main_loop == NULL) {
spa_log_error (this->log, "a main-loop is needed");
return SPA_RESULT_ERROR;
}
this->uri.monitor = spa_id_map_get_id (this->map, SPA_MONITOR_URI); this->uri.monitor = spa_id_map_get_id (this->map, SPA_MONITOR_URI);
this->monitor = alsamonitor; this->monitor = alsamonitor;

View file

@ -54,6 +54,7 @@ struct _SpaV4l2Monitor {
URI uri; URI uri;
SpaIDMap *map; SpaIDMap *map;
SpaLog *log; SpaLog *log;
SpaPoll *main_loop;
SpaMonitorEventCallback event_cb; SpaMonitorEventCallback event_cb;
void *user_data; void *user_data;
@ -216,7 +217,6 @@ spa_v4l2_monitor_set_event_callback (SpaMonitor *monitor,
{ {
SpaResult res; SpaResult res;
SpaV4l2Monitor *this; SpaV4l2Monitor *this;
SpaMonitorEvent event;
if (monitor == NULL || monitor->handle == NULL) if (monitor == NULL || monitor->handle == NULL)
return SPA_RESULT_INVALID_ARGUMENTS; return SPA_RESULT_INVALID_ARGUMENTS;
@ -241,10 +241,6 @@ spa_v4l2_monitor_set_event_callback (SpaMonitor *monitor,
udev_monitor_enable_receiving (this->umonitor); udev_monitor_enable_receiving (this->umonitor);
this->fd = udev_monitor_get_fd (this->umonitor);; this->fd = udev_monitor_get_fd (this->umonitor);;
event.type = SPA_MONITOR_EVENT_TYPE_ADD_POLL;
event.data = &this->poll;
event.size = sizeof (this->poll);
this->fds[0].fd = this->fd; this->fds[0].fd = this->fd;
this->fds[0].events = POLLIN | POLLPRI | POLLERR; this->fds[0].events = POLLIN | POLLPRI | POLLERR;
this->fds[0].revents = 0; this->fds[0].revents = 0;
@ -257,12 +253,9 @@ spa_v4l2_monitor_set_event_callback (SpaMonitor *monitor,
this->poll.before_cb = NULL; this->poll.before_cb = NULL;
this->poll.after_cb = v4l2_on_fd_events; this->poll.after_cb = v4l2_on_fd_events;
this->poll.user_data = this; this->poll.user_data = this;
this->event_cb (&this->monitor, &event, this->user_data); spa_poll_add_item (this->main_loop, &this->poll);
} else { } else {
event.type = SPA_MONITOR_EVENT_TYPE_REMOVE_POLL; spa_poll_remove_item (this->main_loop, &this->poll);
event.data = &this->poll;
event.size = sizeof (this->poll);
this->event_cb (&this->monitor, &event, this->user_data);
} }
return SPA_RESULT_OK; return SPA_RESULT_OK;
@ -378,11 +371,17 @@ v4l2_monitor_init (const SpaHandleFactory *factory,
this->map = support[i].data; this->map = support[i].data;
else if (strcmp (support[i].uri, SPA_LOG_URI) == 0) else if (strcmp (support[i].uri, SPA_LOG_URI) == 0)
this->log = support[i].data; this->log = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__MainLoop) == 0)
this->main_loop = support[i].data;
} }
if (this->map == NULL) { if (this->map == NULL) {
spa_log_error (this->log, "an id-map is needed"); spa_log_error (this->log, "an id-map is needed");
return SPA_RESULT_ERROR; return SPA_RESULT_ERROR;
} }
if (this->main_loop == NULL) {
spa_log_error (this->log, "a main-loop is needed");
return SPA_RESULT_ERROR;
}
this->uri.monitor = spa_id_map_get_id (this->map, SPA_MONITOR_URI); this->uri.monitor = spa_id_map_get_id (this->map, SPA_MONITOR_URI);
this->monitor = v4l2monitor; this->monitor = v4l2monitor;

View file

@ -40,8 +40,9 @@ typedef struct {
SpaIDMap *map; SpaIDMap *map;
SpaLog *log; SpaLog *log;
SpaPoll main_loop;
SpaSupport support[2]; SpaSupport support[3];
unsigned int n_support; unsigned int n_support;
unsigned int n_poll; unsigned int n_poll;
@ -67,8 +68,6 @@ on_monitor_event (SpaMonitor *monitor,
SpaMonitorEvent *event, SpaMonitorEvent *event,
void *user_data) void *user_data)
{ {
AppData *data = user_data;
switch (event->type) { switch (event->type) {
case SPA_MONITOR_EVENT_TYPE_ADDED: case SPA_MONITOR_EVENT_TYPE_ADDED:
{ {
@ -91,26 +90,37 @@ on_monitor_event (SpaMonitor *monitor,
inspect_item (item); inspect_item (item);
break; break;
} }
case SPA_MONITOR_EVENT_TYPE_ADD_POLL:
{
SpaPollItem *item = event->data;
data->poll[data->n_poll] = *item;
data->n_poll++;
if (item->n_fds)
data->rebuild_fds = true;
break;
}
case SPA_MONITOR_EVENT_TYPE_UPDATE_POLL:
break;
case SPA_MONITOR_EVENT_TYPE_REMOVE_POLL:
break;
default: default:
break; break;
} }
} }
static SpaResult
do_add_item (SpaPoll *poll,
SpaPollItem *item)
{
AppData *data = SPA_CONTAINER_OF (poll, AppData, main_loop);
data->poll[data->n_poll] = *item;
data->n_poll++;
if (item->n_fds)
data->rebuild_fds = true;
return SPA_RESULT_OK;
}
static SpaResult
do_update_item (SpaPoll *poll,
SpaPollItem *item)
{
return SPA_RESULT_OK;
}
static SpaResult
do_remove_item (SpaPoll *poll,
SpaPollItem *item)
{
return SPA_RESULT_OK;
}
static void static void
handle_monitor (AppData *data, SpaMonitor *monitor) handle_monitor (AppData *data, SpaMonitor *monitor)
{ {
@ -182,7 +192,7 @@ handle_monitor (AppData *data, SpaMonitor *monitor)
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
AppData data; AppData data = { 0 };
SpaResult res; SpaResult res;
void *handle; void *handle;
SpaEnumHandleFactoryFunc enum_func; SpaEnumHandleFactoryFunc enum_func;
@ -190,12 +200,20 @@ main (int argc, char *argv[])
data.map = spa_id_map_get_default (); data.map = spa_id_map_get_default ();
data.log = NULL; data.log = NULL;
data.main_loop.handle = NULL;
data.main_loop.size = sizeof (SpaPoll);
data.main_loop.info = NULL;
data.main_loop.add_item = do_add_item;
data.main_loop.update_item = do_update_item;
data.main_loop.remove_item = do_remove_item;
data.support[0].uri = SPA_ID_MAP_URI; data.support[0].uri = SPA_ID_MAP_URI;
data.support[0].data = data.map; data.support[0].data = data.map;
data.support[1].uri = SPA_LOG_URI; data.support[1].uri = SPA_LOG_URI;
data.support[1].data = data.log; data.support[1].data = data.log;
data.n_support = 2; data.support[2].uri = SPA_POLL__MainLoop;
data.support[2].data = &data.main_loop;
data.n_support = 3;
data.uri.monitor = spa_id_map_get_id (data.map, SPA_MONITOR_URI); data.uri.monitor = spa_id_map_get_id (data.map, SPA_MONITOR_URI);