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

@ -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);