System: More work on making system functions pluggable

Move the epoll functions to the system functions and make the loop
use those. Use simple mask for events instead of enum.
Add the used system api in pw_loop.
Add System API to spa_support and use it where possible.
Pass the system API used in the realtime loops in spa_support as
well and use this in the realtime paths.
Improve bootstrapping, load only the log and cpu interfaces because
those can/need to be shared between instances. Let the core load
the other interfaces.
Add keys to configure the System and Loop implementations used in
pw_loop.
This commit is contained in:
Wim Taymans 2019-06-06 15:21:40 +02:00
parent 86dc0496a5
commit db88e9f954
22 changed files with 455 additions and 342 deletions

View file

@ -32,6 +32,7 @@ extern "C" {
#include <spa/utils/defs.h>
#include <spa/utils/hook.h>
#include <spa/utils/result.h>
#include <spa/support/system.h>
#define SPA_VERSION_LOOP 0
struct spa_loop { struct spa_interface iface; };
@ -41,13 +42,6 @@ struct spa_loop_control { struct spa_interface iface; };
struct spa_loop_utils { struct spa_interface iface; };
struct spa_source;
enum spa_io {
SPA_IO_IN = (1 << 0),
SPA_IO_OUT = (1 << 1),
SPA_IO_HUP = (1 << 2),
SPA_IO_ERR = (1 << 3),
};
typedef void (*spa_source_func_t) (struct spa_source *source);
struct spa_source {
@ -55,8 +49,8 @@ struct spa_source {
spa_source_func_t func;
void *data;
int fd;
enum spa_io mask;
enum spa_io rmask;
uint32_t mask;
uint32_t rmask;
};
typedef int (*spa_invoke_func_t) (struct spa_loop *loop,
@ -202,7 +196,7 @@ struct spa_loop_control_methods {
#define spa_loop_control_leave(l) spa_loop_control_method_v(l,leave,0)
#define spa_loop_control_iterate(l,...) spa_loop_control_method_r(l,iterate,0,__VA_ARGS__)
typedef void (*spa_source_io_func_t) (void *data, int fd, enum spa_io mask);
typedef void (*spa_source_io_func_t) (void *data, int fd, uint32_t mask);
typedef void (*spa_source_idle_func_t) (void *data);
typedef void (*spa_source_event_func_t) (void *data, uint64_t count);
typedef void (*spa_source_timer_func_t) (void *data, uint64_t expirations);
@ -219,11 +213,11 @@ struct spa_loop_utils_methods {
struct spa_source *(*add_io) (void *object,
int fd,
enum spa_io mask,
uint32_t mask,
bool close,
spa_source_io_func_t func, void *data);
int (*update_io) (void *object, struct spa_source *source, enum spa_io mask);
int (*update_io) (void *object, struct spa_source *source, uint32_t mask);
struct spa_source *(*add_idle) (void *object,
bool enabled,

View file

@ -29,23 +29,36 @@
extern "C" {
#endif
#include <sys/timerfd.h>
#include <spa/utils/defs.h>
#include <spa/utils/hook.h>
#include <spa/utils/result.h>
/**
* a collection of system functions
* a collection of core system functions
*/
#define SPA_VERSION_SYSTEM 0
struct spa_system { struct spa_interface iface; };
/* IO events */
#define SPA_IO_IN (1 << 0)
#define SPA_IO_OUT (1 << 1)
#define SPA_IO_HUP (1 << 2)
#define SPA_IO_ERR (1 << 3)
/* flags */
#define SPA_FD_CLOEXEC (1<<0)
#define SPA_FD_NONBLOCK (1<<1)
#define SPA_FD_EVENT_SEMAPHORE (1<<2)
#define SPA_FD_TIMER_ABSTIME (1<<3)
#define SPA_FD_TIMER_CANCEL_ON_SET (1<<4)
struct spa_poll_event {
uint32_t events;
void *data;
};
struct spa_system_methods {
#define SPA_VERSION_SYSTEM_METHODS 0
uint32_t version;
@ -62,6 +75,14 @@ struct spa_system_methods {
int (*clock_getres) (void *object,
int clockid, struct timespec *res);
/* poll */
int (*pollfd_create) (void *object, int flags);
int (*pollfd_add) (void *object, int pfd, int fd, uint32_t events, void *data);
int (*pollfd_mod) (void *object, int pfd, int fd, uint32_t events, void *data);
int (*pollfd_del) (void *object, int pfd, int fd);
int (*pollfd_wait) (void *object, int pfd,
struct spa_poll_event *ev, int n_ev, int timeout);
/* timers */
int (*timerfd_create) (void *object, int clockid, int flags);
int (*timerfd_settime) (void *object,
@ -101,6 +122,12 @@ struct spa_system_methods {
#define spa_system_clock_gettime(s,...) spa_system_method_r(s,clock_gettime,0,__VA_ARGS__)
#define spa_system_clock_getres(s,...) spa_system_method_r(s,clock_getres,0,__VA_ARGS__)
#define spa_system_pollfd_create(s,...) spa_system_method_r(s,pollfd_create,0,__VA_ARGS__)
#define spa_system_pollfd_add(s,...) spa_system_method_r(s,pollfd_add,0,__VA_ARGS__)
#define spa_system_pollfd_mod(s,...) spa_system_method_r(s,pollfd_mod,0,__VA_ARGS__)
#define spa_system_pollfd_del(s,...) spa_system_method_r(s,pollfd_del,0,__VA_ARGS__)
#define spa_system_pollfd_wait(s,...) spa_system_method_r(s,pollfd_wait,0,__VA_ARGS__)
#define spa_system_timerfd_create(s,...) spa_system_method_r(s,timerfd_create,0,__VA_ARGS__)
#define spa_system_timerfd_settime(s,...) spa_system_method_r(s,timerfd_settime,0,__VA_ARGS__)
#define spa_system_timerfd_gettime(s,...) spa_system_method_r(s,timerfd_gettime,0,__VA_ARGS__)

View file

@ -7,9 +7,9 @@
#include <sys/time.h>
#include <math.h>
#include <limits.h>
#include <sys/timerfd.h>
#include <spa/pod/filter.h>
#include <spa/support/system.h>
#include <spa/control/control.h>
#include "alsa-utils.h"
@ -34,7 +34,8 @@ static int spa_alsa_open(struct state *state)
SND_PCM_NO_AUTO_RESAMPLE |
SND_PCM_NO_AUTO_CHANNELS | SND_PCM_NO_AUTO_FORMAT), "open failed");
state->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
state->timerfd = spa_system_timerfd_create(state->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
state->opened = true;
state->sample_count = 0;
state->sample_time = 0;
@ -52,7 +53,7 @@ int spa_alsa_close(struct state *state)
spa_log_info(state->log, "%p: Device '%s' closing", state, state->props.device);
CHECK(snd_pcm_close(state->hndl), "close failed");
close(state->timerfd);
spa_system_close(state->data_system, state->timerfd);
state->opened = false;
return err;
@ -495,7 +496,8 @@ static int set_timeout(struct state *state, uint64_t time)
ts.it_value.tv_nsec = time % SPA_NSEC_PER_SEC;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(state->timerfd, TFD_TIMER_ABSTIME, &ts, NULL);
spa_system_timerfd_settime(state->data_system,
state->timerfd, SPA_FD_TIMER_ABSTIME, &ts, NULL);
}
return 0;
}
@ -998,13 +1000,13 @@ static void alsa_on_timeout_event(struct spa_source *source)
uint64_t expire;
int res;
if (state->started && read(state->timerfd, &expire, sizeof(uint64_t)) != sizeof(uint64_t))
if (state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0)
spa_log_warn(state->log, "error reading timerfd: %s", strerror(errno));
if (state->position)
state->threshold = state->position->size;
clock_gettime(CLOCK_MONOTONIC, &state->now);
spa_system_clock_gettime(state->data_system, CLOCK_MONOTONIC, &state->now);
if ((res = get_status(state, &delay)) < 0)
return;
@ -1049,7 +1051,7 @@ static int set_timers(struct state *state)
ts.it_value.tv_nsec = 1;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(state->timerfd, 0, &ts, NULL);
spa_system_timerfd_settime(state->data_system, state->timerfd, 0, &ts, NULL);
return 0;
}
@ -1160,7 +1162,7 @@ static int do_remove_source(struct spa_loop *loop,
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(state->timerfd, 0, &ts, NULL);
spa_system_timerfd_settime(state->data_system, state->timerfd, 0, &ts, NULL);
return 0;
}

View file

@ -27,9 +27,9 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/timerfd.h>
#include <spa/support/log.h>
#include <spa/support/system.h>
#include <spa/support/loop.h>
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
@ -113,6 +113,7 @@ struct impl {
struct spa_log *log;
struct spa_loop *data_loop;
struct spa_system *data_system;
uint64_t info_all;
struct spa_node_info info;
@ -295,7 +296,8 @@ static void set_timer(struct impl *this, bool enabled)
this->timerspec.it_value.tv_sec = 0;
this->timerspec.it_value.tv_nsec = 0;
}
timerfd_settime(this->timer_source.fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
spa_system_timerfd_settime(this->data_system,
this->timer_source.fd, SPA_FD_TIMER_ABSTIME, &this->timerspec, NULL);
}
}
@ -304,7 +306,7 @@ static void read_timer(struct impl *this)
uint64_t expirations;
if (this->async || this->props.live) {
if (read(this->timer_source.fd, &expirations, sizeof(uint64_t)) != sizeof(uint64_t))
if (spa_system_timerfd_read(this->data_system, this->timer_source.fd, &expirations) < 0)
perror("read timerfd");
}
}
@ -972,7 +974,7 @@ static int impl_clear(struct spa_handle *handle)
if (this->data_loop)
spa_loop_remove_source(this->data_loop, &this->timer_source);
close(this->timer_source.fd);
spa_system_close(this->data_system, this->timer_source.fd);
return 0;
}
@ -1004,10 +1006,17 @@ impl_init(const struct spa_handle_factory *factory,
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
switch (support[i].type) {
case SPA_TYPE_INTERFACE_Log:
this->log = support[i].data;
else if (support[i].type == SPA_TYPE_INTERFACE_DataLoop)
break;
case SPA_TYPE_INTERFACE_DataLoop:
this->data_loop = support[i].data;
break;
case SPA_TYPE_INTERFACE_DataSystem:
this->data_system = support[i].data;
break;
}
}
spa_hook_list_init(&this->hooks);
@ -1031,7 +1040,8 @@ impl_init(const struct spa_handle_factory *factory,
this->timer_source.func = on_output;
this->timer_source.data = this;
this->timer_source.fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
this->timer_source.fd = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC);
this->timer_source.mask = SPA_IO_IN;
this->timer_source.rmask = 0;
this->timerspec.it_value.tv_sec = 0;

View file

@ -25,12 +25,12 @@
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/timerfd.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <spa/support/loop.h>
#include <spa/support/log.h>
#include <spa/support/system.h>
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
#include <spa/monitor/device.h>
@ -90,8 +90,8 @@ struct impl {
struct spa_node node;
struct spa_log *log;
struct spa_loop *main_loop;
struct spa_loop *data_loop;
struct spa_system *data_system;
struct spa_hook_list hooks;
struct spa_callbacks callbacks;
@ -249,7 +249,7 @@ static int set_timers(struct impl *this)
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
res = timerfd_settime(this->timerfd, 0, &ts, NULL);
res = spa_system_timerfd_settime(this->data_system, this->timerfd, 0, &ts, NULL);
this->source.mask = SPA_IO_IN;
spa_loop_update_source(this->data_loop, &this->source);
return res;
@ -632,7 +632,7 @@ static int flush_data(struct impl *this, uint64_t now_time)
&this->now, &ts.it_value);
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(this->timerfd, TFD_TIMER_ABSTIME, &ts, NULL);
spa_system_timerfd_settime(this->data_system, this->timerfd, SPA_FD_TIMER_ABSTIME, &ts, NULL);
this->source.mask = SPA_IO_IN;
spa_loop_update_source(this->data_loop, &this->source);
} else {
@ -658,7 +658,7 @@ static void a2dp_on_flush(struct spa_source *source)
return;
}
clock_gettime(CLOCK_MONOTONIC, &this->now);
spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &this->now);
now_time = this->now.tv_sec * SPA_NSEC_PER_SEC + this->now.tv_nsec;
flush_data(this, now_time);
@ -672,10 +672,10 @@ static void a2dp_on_timeout(struct spa_source *source)
uint64_t exp, now_time;
struct spa_io_buffers *io = port->io;
if (this->started && read(this->timerfd, &exp, sizeof(uint64_t)) != sizeof(uint64_t))
if (this->started && spa_system_timerfd_read(this->data_system, this->timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
clock_gettime(CLOCK_MONOTONIC, &this->now);
spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &this->now);
now_time = SPA_TIMESPEC_TO_NSEC(&this->now);
spa_log_trace(this->log, NAME" %p: timeout %ld %ld", this,
@ -851,7 +851,7 @@ static int do_remove_source(struct spa_loop *loop,
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(this->timerfd, 0, &ts, NULL);
spa_system_timerfd_settime(this->data_system, this->timerfd, 0, &ts, NULL);
if (this->flush_source.loop)
spa_loop_remove_source(this->data_loop, &this->flush_source);
@ -1311,7 +1311,7 @@ static int impl_node_process(void *object)
io = port->io;
spa_return_val_if_fail(io != NULL, -EIO);
clock_gettime(CLOCK_MONOTONIC, &this->now);
spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &this->now);
now_time = SPA_TIMESPEC_TO_NSEC(&this->now);
if (!spa_list_is_empty(&port->ready))
@ -1393,6 +1393,8 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **i
static int impl_clear(struct spa_handle *handle)
{
struct impl *this = (struct impl *) handle;
spa_system_close(this->data_system, this->timerfd);
return 0;
}
@ -1423,19 +1425,24 @@ impl_init(const struct spa_handle_factory *factory,
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
switch (support[i].type) {
case SPA_TYPE_INTERFACE_Log:
this->log = support[i].data;
else if (support[i].type == SPA_TYPE_INTERFACE_DataLoop)
break;
case SPA_TYPE_INTERFACE_DataLoop:
this->data_loop = support[i].data;
else if (support[i].type == SPA_TYPE_INTERFACE_MainLoop)
this->main_loop = support[i].data;
break;
case SPA_TYPE_INTERFACE_DataSystem:
this->data_system = support[i].data;
break;
}
}
if (this->data_loop == NULL) {
spa_log_error(this->log, "a data loop is needed");
return -EINVAL;
}
if (this->main_loop == NULL) {
spa_log_error(this->log, "a main loop is needed");
if (this->data_system == NULL) {
spa_log_error(this->log, "a data system is needed");
return -EINVAL;
}
@ -1482,7 +1489,8 @@ impl_init(const struct spa_handle_factory *factory,
spa_bt_transport_add_listener(this->transport,
&this->transport_listener, &transport_events, this);
this->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
this->timerfd = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
return 0;
}

View file

@ -33,6 +33,7 @@
#include <spa/support/loop.h>
#include <spa/support/log.h>
#include <spa/support/system.h>
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
#include <spa/monitor/device.h>
@ -91,8 +92,8 @@ struct impl {
struct spa_node node;
struct spa_log *log;
struct spa_loop *main_loop;
struct spa_loop *data_loop;
struct spa_system *data_system;
struct spa_hook_list hooks;
struct spa_callbacks callbacks;
@ -392,7 +393,7 @@ static void a2dp_on_ready_read(struct spa_source *source)
}
/* update the current pts */
clock_gettime(CLOCK_MONOTONIC, &this->now);
spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &this->now);
again:
/* read data from socket */
@ -1113,19 +1114,24 @@ impl_init(const struct spa_handle_factory *factory,
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
switch (support[i].type) {
case SPA_TYPE_INTERFACE_Log:
this->log = support[i].data;
else if (support[i].type == SPA_TYPE_INTERFACE_DataLoop)
break;
case SPA_TYPE_INTERFACE_DataLoop:
this->data_loop = support[i].data;
else if (support[i].type == SPA_TYPE_INTERFACE_MainLoop)
this->main_loop = support[i].data;
break;
case SPA_TYPE_INTERFACE_DataSystem:
this->data_system = support[i].data;
break;
}
}
if (this->data_loop == NULL) {
spa_log_error(this->log, "a data loop is needed");
return -EINVAL;
}
if (this->main_loop == NULL) {
spa_log_error(this->log, "a main loop is needed");
if (this->data_system == NULL) {
spa_log_error(this->log, "a data system is needed");
return -EINVAL;
}

View file

@ -76,9 +76,9 @@ static void dispatch_status(DBusConnection *conn, DBusDispatchStatus status, voi
status == DBUS_DISPATCH_COMPLETE ? false : true);
}
static inline enum spa_io dbus_to_io(DBusWatch *watch)
static inline uint32_t dbus_to_io(DBusWatch *watch)
{
enum spa_io mask;
uint32_t mask;
unsigned int flags;
/* no watch flags for disabled watches */
@ -96,7 +96,7 @@ static inline enum spa_io dbus_to_io(DBusWatch *watch)
return mask;
}
static inline unsigned int io_to_dbus(enum spa_io mask)
static inline unsigned int io_to_dbus(uint32_t mask)
{
unsigned int flags = 0;
@ -112,7 +112,7 @@ static inline unsigned int io_to_dbus(enum spa_io mask)
}
static void
handle_io_event(void *userdata, int fd, enum spa_io mask)
handle_io_event(void *userdata, int fd, uint32_t mask)
{
DBusWatch *watch = userdata;

View file

@ -27,10 +27,10 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/eventfd.h>
#include <spa/support/log.h>
#include <spa/support/loop.h>
#include <spa/support/system.h>
#include <spa/support/plugin.h>
#include <spa/utils/ringbuffer.h>
#include <spa/utils/type.h>
@ -93,14 +93,13 @@ impl_log_logv(void *object,
if (SPA_UNLIKELY(do_trace)) {
uint32_t index;
uint64_t count = 1;
spa_ringbuffer_get_write_index(&impl->trace_rb, &index);
spa_ringbuffer_write_data(&impl->trace_rb, impl->trace_data, TRACE_BUFFER,
index & (TRACE_BUFFER - 1), location, size);
spa_ringbuffer_write_update(&impl->trace_rb, index + size);
if (write(impl->source.fd, &count, sizeof(uint64_t)) != sizeof(uint64_t))
if (spa_system_eventfd_write(impl->system, impl->source.fd, 1) < 0)
fprintf(impl->file, "error signaling eventfd: %s\n", strerror(errno));
} else
fputs(location, impl->file);
@ -129,7 +128,7 @@ static void on_trace_event(struct spa_source *source)
uint32_t index;
uint64_t count;
if (read(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t))
if (spa_system_eventfd_read(impl->system, source->fd, &count) < 0)
fprintf(impl->file, "failed to read event fd: %s", strerror(errno));
while ((avail = spa_ringbuffer_get_read_index(&impl->trace_rb, &index)) > 0) {
@ -247,10 +246,10 @@ impl_init(const struct spa_handle_factory *factory,
if (this->file == NULL)
this->file = stderr;
if (loop) {
if (loop != NULL && this->system != NULL) {
this->source.func = on_trace_event;
this->source.data = this;
this->source.fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
this->source.fd = spa_system_eventfd_create(this->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
this->source.mask = SPA_IO_IN;
this->source.rmask = 0;
spa_loop_add_source(loop, &this->source);

View file

@ -28,7 +28,6 @@
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <pthread.h>
#include <spa/support/loop.h>
@ -72,7 +71,7 @@ struct impl {
struct spa_list destroy_list;
struct spa_hook_list hooks_list;
int epoll_fd;
int poll_fd;
pthread_t thread;
struct spa_source *wakeup;
@ -100,83 +99,38 @@ struct source_impl {
};
/** \endcond */
static inline uint32_t spa_io_to_epoll(enum spa_io mask)
{
uint32_t events = 0;
if (mask & SPA_IO_IN)
events |= EPOLLIN;
if (mask & SPA_IO_OUT)
events |= EPOLLOUT;
if (mask & SPA_IO_ERR)
events |= EPOLLERR;
if (mask & SPA_IO_HUP)
events |= EPOLLHUP;
return events;
}
static inline enum spa_io spa_epoll_to_io(uint32_t events)
{
enum spa_io mask = 0;
if (events & EPOLLIN)
mask |= SPA_IO_IN;
if (events & EPOLLOUT)
mask |= SPA_IO_OUT;
if (events & EPOLLHUP)
mask |= SPA_IO_HUP;
if (events & EPOLLERR)
mask |= SPA_IO_ERR;
return mask;
}
static int loop_add_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
source->loop = &impl->loop;
if (source->fd != -1) {
struct epoll_event ep;
if (SPA_UNLIKELY(source->fd == -1))
return 0;
spa_zero(ep);
ep.events = spa_io_to_epoll(source->mask);
ep.data.ptr = source;
if (epoll_ctl(impl->epoll_fd, EPOLL_CTL_ADD, source->fd, &ep) < 0)
return errno;
}
return 0;
return spa_system_pollfd_add(impl->system, impl->poll_fd, source->fd, source->mask, source);
}
static int loop_update_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
if (source->fd != -1) {
struct epoll_event ep;
if (SPA_UNLIKELY(source->fd == -1))
return 0;
spa_zero(ep);
ep.events = spa_io_to_epoll(source->mask);
ep.data.ptr = source;
if (epoll_ctl(impl->epoll_fd, EPOLL_CTL_MOD, source->fd, &ep) < 0)
return errno;
}
return 0;
return spa_system_pollfd_mod(impl->system, impl->poll_fd, source->fd, source->mask, source);
}
static int loop_remove_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
if (source->fd != -1)
epoll_ctl(impl->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
source->loop = NULL;
return 0;
if (SPA_UNLIKELY(source->fd == -1))
return 0;
return spa_system_pollfd_del(impl->system, impl->poll_fd, source->fd);
}
static int
@ -288,7 +242,7 @@ static void wakeup_func(void *data, uint64_t count)
static int loop_get_fd(void *object)
{
struct impl *impl = object;
return impl->epoll_fd;
return impl->poll_fd;
}
static void
@ -325,12 +279,13 @@ static int loop_iterate(void *object, int timeout)
{
struct impl *impl = object;
struct spa_loop *loop = &impl->loop;
struct epoll_event ep[32];
struct spa_poll_event ep[32];
int i, nfds, save_errno = 0;
spa_loop_control_hook_before(&impl->hooks_list);
if (SPA_UNLIKELY((nfds = epoll_wait(impl->epoll_fd, ep, SPA_N_ELEMENTS(ep), timeout)) < 0))
nfds = spa_system_pollfd_wait(impl->system, impl->poll_fd, ep, SPA_N_ELEMENTS(ep), timeout);
if (SPA_UNLIKELY(nfds < 0))
save_errno = errno;
spa_loop_control_hook_after(&impl->hooks_list);
@ -342,11 +297,11 @@ static int loop_iterate(void *object, int timeout)
* some callback might also want to look at other sources it manages and
* can then reset the rmask to suppress the callback */
for (i = 0; i < nfds; i++) {
struct spa_source *s = ep[i].data.ptr;
s->rmask = spa_epoll_to_io(ep[i].events);
struct spa_source *s = ep[i].data;
s->rmask = ep[i].events;
}
for (i = 0; i < nfds; i++) {
struct spa_source *s = ep[i].data.ptr;
struct spa_source *s = ep[i].data;
if (s->rmask && s->fd != -1 && s->loop == loop)
s->func(s);
}
@ -363,7 +318,7 @@ static void source_io_func(struct spa_source *source)
static struct spa_source *loop_add_io(void *object,
int fd,
enum spa_io mask,
uint32_t mask,
bool close, spa_source_io_func_t func, void *data)
{
struct impl *impl = object;
@ -389,7 +344,7 @@ static struct spa_source *loop_add_io(void *object,
return &source->source;
}
static int loop_update_io(void *object, struct spa_source *source, enum spa_io mask)
static int loop_update_io(void *object, struct spa_source *source, uint32_t mask)
{
source->mask = mask;
return loop_update_source(object, source);
@ -694,7 +649,7 @@ static int impl_clear(struct spa_handle *handle)
process_destroy(impl);
spa_system_close(impl->system, impl->ack_fd);
close(impl->epoll_fd);
spa_system_close(impl->system, impl->poll_fd);
return 0;
}
@ -746,9 +701,13 @@ impl_init(const struct spa_handle_factory *factory,
break;
}
}
if (impl->system == NULL) {
spa_log_error(impl->log, NAME " %p: a System is needed", impl);
return -EINVAL;
}
impl->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (impl->epoll_fd == -1)
impl->poll_fd = spa_system_pollfd_create(impl->system, SPA_FD_CLOEXEC);
if (impl->poll_fd < 0)
return errno;
spa_list_init(&impl->source_list);

View file

@ -28,6 +28,7 @@
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/timerfd.h>
#include <sys/eventfd.h>
@ -43,7 +44,6 @@
struct impl {
struct spa_handle handle;
struct spa_system system;
struct spa_log *log;
};
@ -89,6 +89,90 @@ static int impl_clock_getres(void *object,
return clock_getres(clockid, res);
}
/* poll */
static inline uint32_t spa_io_to_epoll(uint32_t mask)
{
uint32_t events = 0;
if (mask & SPA_IO_IN)
events |= EPOLLIN;
if (mask & SPA_IO_OUT)
events |= EPOLLOUT;
if (mask & SPA_IO_ERR)
events |= EPOLLERR;
if (mask & SPA_IO_HUP)
events |= EPOLLHUP;
return events;
}
static inline uint32_t spa_epoll_to_io(uint32_t events)
{
uint32_t mask = 0;
if (events & EPOLLIN)
mask |= SPA_IO_IN;
if (events & EPOLLOUT)
mask |= SPA_IO_OUT;
if (events & EPOLLHUP)
mask |= SPA_IO_HUP;
if (events & EPOLLERR)
mask |= SPA_IO_ERR;
return mask;
}
static int impl_pollfd_create(void *object, int flags)
{
int fl = 0;
if (flags & SPA_FD_CLOEXEC)
fl |= EPOLL_CLOEXEC;
return epoll_create1(fl);
}
static int impl_pollfd_add(void *object, int pfd, int fd, uint32_t events, void *data)
{
struct epoll_event ep;
spa_zero(ep);
ep.events = spa_io_to_epoll(events);
ep.data.ptr = data;
return epoll_ctl(pfd, EPOLL_CTL_ADD, fd, &ep);
}
static int impl_pollfd_mod(void *object, int pfd, int fd, uint32_t events, void *data)
{
struct epoll_event ep;
spa_zero(ep);
ep.events = spa_io_to_epoll(events);
ep.data.ptr = data;
return epoll_ctl(pfd, EPOLL_CTL_MOD, fd, &ep);
}
static int impl_pollfd_del(void *object, int pfd, int fd)
{
return epoll_ctl(pfd, EPOLL_CTL_DEL, fd, NULL);
}
static int impl_pollfd_wait(void *object, int pfd,
struct spa_poll_event *ev, int n_ev, int timeout)
{
struct epoll_event ep[n_ev];
int i, nfds;
if (SPA_UNLIKELY((nfds = epoll_wait(pfd, ep, SPA_N_ELEMENTS(ep), timeout)) < 0))
return nfds;
for (i = 0; i < nfds; i++) {
ev[i].events = spa_epoll_to_io(ep[i].events);
ev[i].data = ep[i].data.ptr;
}
return nfds;
}
/* timers */
static int impl_timerfd_create(void *object, int clockid, int flags)
{
@ -121,8 +205,7 @@ static int impl_timerfd_gettime(void *object,
}
static int impl_timerfd_read(void *object, int fd, uint64_t *expirations)
{
int res;
if ((res = read(fd, expirations, sizeof(uint64_t))) != sizeof(uint64_t))
if (read(fd, expirations, sizeof(uint64_t)) != sizeof(uint64_t))
return -errno;
return 0;
}
@ -195,6 +278,11 @@ static const struct spa_system_methods impl_system = {
.close = impl_close,
.clock_gettime = impl_clock_gettime,
.clock_getres = impl_clock_getres,
.pollfd_create = impl_pollfd_create,
.pollfd_add = impl_pollfd_add,
.pollfd_mod = impl_pollfd_mod,
.pollfd_del = impl_pollfd_del,
.pollfd_wait = impl_pollfd_wait,
.timerfd_create = impl_timerfd_create,
.timerfd_settime = impl_timerfd_settime,
.timerfd_gettime = impl_timerfd_gettime,
@ -261,8 +349,11 @@ impl_init(const struct spa_handle_factory *factory,
&impl_system, impl);
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
switch (support[i].type) {
case SPA_TYPE_INTERFACE_Log:
impl->log = support[i].data;
break;
}
}
spa_log_debug(impl->log, NAME " %p: initialized", impl);