support: abstract some system functions

Make a new API to hide some the implementation of eventfd, timerfd
and signalfd along with clock and read/write/ioctl/close functions.
We would like to have plugins use the abstractions so that we
can switch them to something else when needed.
This commit is contained in:
Wim Taymans 2019-06-04 17:07:34 +02:00
parent 98602f0343
commit 81c7dd4433
12 changed files with 492 additions and 50 deletions

View file

@ -89,6 +89,7 @@ spa_support_headers = [
'support/log-impl.h', 'support/log-impl.h',
'support/loop.h', 'support/loop.h',
'support/plugin.h', 'support/plugin.h',
'support/system.h',
] ]
install_headers(spa_support_headers, install_headers(spa_support_headers,

View file

@ -202,7 +202,6 @@ struct spa_loop_control_methods {
#define spa_loop_control_leave(l) spa_loop_control_method_v(l,leave,0) #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__) #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, enum spa_io mask);
typedef void (*spa_source_idle_func_t) (void *data); 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_event_func_t) (void *data, uint64_t count);

View file

@ -0,0 +1,120 @@
/* Simple Plugin API
*
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_SYSTEM_H
#define SPA_SYSTEM_H
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/utils/defs.h>
#include <spa/utils/hook.h>
#include <spa/utils/result.h>
/**
* a collection of system functions
*/
#define SPA_VERSION_SYSTEM 0
struct spa_system { struct spa_interface iface; };
#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_system_methods {
#define SPA_VERSION_SYSTEM_METHODS 0
uint32_t version;
/* read/write/ioctl */
ssize_t (*read) (void *object, int fd, void *buf, size_t count);
ssize_t (*write) (void *object, int fd, const void *buf, size_t count);
int (*ioctl) (void *object, int fd, unsigned long request, ...);
int (*close) (void *object, int fd);
/* clock */
int (*clock_gettime) (void *object,
int clockid, struct timespec *value);
int (*clock_getres) (void *object,
int clockid, struct timespec *res);
/* timers */
int (*timerfd_create) (void *object, int clockid, int flags);
int (*timerfd_settime) (void *object,
int fd, int flags,
const struct itimerspec *new_value,
struct itimerspec *old_value);
int (*timerfd_gettime) (void *object,
int fd, struct itimerspec *curr_value);
int (*timerfd_read) (void *object, int fd, uint64_t *expirations);
/* events */
int (*eventfd_create) (void *object, int flags);
int (*eventfd_write) (void *object, int fd, uint64_t count);
int (*eventfd_read) (void *object, int fd, uint64_t *count);
/* signals */
int (*signalfd_create) (void *object, int signal, int flags);
int (*signalfd_read) (void *object, int fd, int *signal);
};
#define spa_system_method_r(o,method,version,...) \
({ \
int _res = -ENOTSUP; \
struct spa_system *_o = o; \
spa_interface_call_res(&_o->iface, \
struct spa_system_methods, _res, \
method, version, ##__VA_ARGS__); \
_res; \
})
#define spa_system_read(s,...) spa_system_method_r(s,read,0,__VA_ARGS__)
#define spa_system_write(s,...) spa_system_method_r(s,write,0,__VA_ARGS__)
#define spa_system_ioctl(s,...) spa_system_method_r(s,ioctl,0,__VA_ARGS__)
#define spa_system_close(s,...) spa_system_method_r(s,close,0,__VA_ARGS__)
#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_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__)
#define spa_system_timerfd_read(s,...) spa_system_method_r(s,timerfd_read,0,__VA_ARGS__)
#define spa_system_eventfd_create(s,...) spa_system_method_r(s,eventfd_create,0,__VA_ARGS__)
#define spa_system_eventfd_write(s,...) spa_system_method_r(s,eventfd_write,0,__VA_ARGS__)
#define spa_system_eventfd_read(s,...) spa_system_method_r(s,eventfd_read,0,__VA_ARGS__)
#define spa_system_signalfd_create(s,...) spa_system_method_r(s,signalfd_create,0,__VA_ARGS__)
#define spa_system_signalfd_read(s,...) spa_system_method_r(s,signalfd_read,0,__VA_ARGS__)
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_SYSTEM_H */

View file

@ -103,6 +103,7 @@ static const struct spa_type_info spa_types[] = {
{ SPA_TYPE_INTERFACE_Handle, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "Handle", NULL }, { SPA_TYPE_INTERFACE_Handle, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "Handle", NULL },
{ SPA_TYPE_INTERFACE_HandleFactory, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "HandleFactory", NULL }, { SPA_TYPE_INTERFACE_HandleFactory, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "HandleFactory", NULL },
{ SPA_TYPE_INTERFACE_Log, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "Log", NULL }, { SPA_TYPE_INTERFACE_Log, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "Log", NULL },
{ SPA_TYPE_INTERFACE_System, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "System", NULL },
{ SPA_TYPE_INTERFACE_Loop, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "Loop", NULL }, { SPA_TYPE_INTERFACE_Loop, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "Loop", NULL },
{ SPA_TYPE_INTERFACE_LoopControl, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "LoopControl", NULL }, { SPA_TYPE_INTERFACE_LoopControl, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "LoopControl", NULL },
{ SPA_TYPE_INTERFACE_LoopUtils, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "LoopUtils", NULL }, { SPA_TYPE_INTERFACE_LoopUtils, SPA_TYPE_Pointer, SPA_TYPE_INFO_INTERFACE_BASE "LoopUtils", NULL },

View file

@ -68,6 +68,7 @@ enum {
SPA_TYPE_INTERFACE_Handle, /**< object handle */ SPA_TYPE_INTERFACE_Handle, /**< object handle */
SPA_TYPE_INTERFACE_HandleFactory, /**< factory for object handles */ SPA_TYPE_INTERFACE_HandleFactory, /**< factory for object handles */
SPA_TYPE_INTERFACE_Log, /**< log interface */ SPA_TYPE_INTERFACE_Log, /**< log interface */
SPA_TYPE_INTERFACE_System, /**< System functions */
SPA_TYPE_INTERFACE_Loop, /**< poll loop support */ SPA_TYPE_INTERFACE_Loop, /**< poll loop support */
SPA_TYPE_INTERFACE_LoopControl, /**< control of loops */ SPA_TYPE_INTERFACE_LoopControl, /**< control of loops */
SPA_TYPE_INTERFACE_LoopUtils, /**< loop utilities */ SPA_TYPE_INTERFACE_LoopUtils, /**< loop utilities */

View file

@ -29,12 +29,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/eventfd.h>
#include <sys/signalfd.h>
#include <pthread.h> #include <pthread.h>
#include <spa/support/loop.h> #include <spa/support/loop.h>
#include <spa/support/system.h>
#include <spa/support/log.h> #include <spa/support/log.h>
#include <spa/support/plugin.h> #include <spa/support/plugin.h>
#include <spa/utils/list.h> #include <spa/utils/list.h>
@ -68,6 +66,7 @@ struct impl {
struct spa_loop_utils utils; struct spa_loop_utils utils;
struct spa_log *log; struct spa_log *log;
struct spa_system *system;
struct spa_list source_list; struct spa_list source_list;
struct spa_list destroy_list; struct spa_list destroy_list;
@ -97,7 +96,6 @@ struct source_impl {
spa_source_timer_func_t timer; spa_source_timer_func_t timer;
spa_source_signal_func_t signal; spa_source_signal_func_t signal;
} func; } func;
int signal_number;
bool enabled; bool enabled;
}; };
/** \endcond */ /** \endcond */
@ -242,9 +240,9 @@ loop_invoke(void *object,
spa_loop_control_hook_before(&impl->hooks_list); spa_loop_control_hook_before(&impl->hooks_list);
if (read(impl->ack_fd, &count, sizeof(uint64_t)) != sizeof(uint64_t)) if ((res = spa_system_eventfd_read(impl->system, impl->ack_fd, &count)) < 0)
spa_log_warn(impl->log, NAME " %p: failed to read event fd: %s", spa_log_warn(impl->log, NAME " %p: failed to read event fd: %s",
impl, strerror(errno)); impl, spa_strerror(res));
spa_loop_control_hook_after(&impl->hooks_list); spa_loop_control_hook_after(&impl->hooks_list);
@ -264,6 +262,8 @@ static void wakeup_func(void *data, uint64_t count)
{ {
struct impl *impl = data; struct impl *impl = data;
uint32_t index; uint32_t index;
int res;
while (spa_ringbuffer_get_read_index(&impl->buffer, &index) > 0) { while (spa_ringbuffer_get_read_index(&impl->buffer, &index) > 0) {
struct invoke_item *item; struct invoke_item *item;
bool block; bool block;
@ -278,10 +278,9 @@ static void wakeup_func(void *data, uint64_t count)
spa_ringbuffer_read_update(&impl->buffer, index + item->item_size); spa_ringbuffer_read_update(&impl->buffer, index + item->item_size);
if (block) { if (block) {
uint64_t c = 1; if ((res = spa_system_eventfd_write(impl->system, impl->ack_fd, 1)) < 0)
if (write(impl->ack_fd, &c, sizeof(uint64_t)) != sizeof(uint64_t))
spa_log_warn(impl->log, NAME " %p: failed to write event fd: %s", spa_log_warn(impl->log, NAME " %p: failed to write event fd: %s",
impl, strerror(errno)); impl, spa_strerror(res));
} }
} }
} }
@ -407,17 +406,17 @@ static void source_idle_func(struct spa_source *source)
static void loop_enable_idle(void *object, struct spa_source *source, bool enabled) static void loop_enable_idle(void *object, struct spa_source *source, bool enabled)
{ {
struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source); struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source);
uint64_t count; int res;
if (enabled && !impl->enabled) { if (enabled && !impl->enabled) {
count = 1; if ((res = spa_system_eventfd_write(impl->impl->system, source->fd, 1)) < 0)
if (write(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t))
spa_log_warn(impl->impl->log, NAME " %p: failed to write idle fd %d: %s", spa_log_warn(impl->impl->log, NAME " %p: failed to write idle fd %d: %s",
source, source->fd, strerror(errno)); source, source->fd, spa_strerror(res));
} else if (!enabled && impl->enabled) { } else if (!enabled && impl->enabled) {
if (read(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t)) uint64_t count;
if ((res = spa_system_eventfd_read(impl->impl->system, source->fd, &count)) < 0)
spa_log_warn(impl->impl->log, NAME " %p: failed to read idle fd %d: %s", spa_log_warn(impl->impl->log, NAME " %p: failed to read idle fd %d: %s",
source, source->fd, strerror(errno)); source, source->fd, spa_strerror(res));
} }
impl->enabled = enabled; impl->enabled = enabled;
} }
@ -434,7 +433,7 @@ static struct spa_source *loop_add_idle(void *object,
source->source.loop = &impl->loop; source->source.loop = &impl->loop;
source->source.func = source_idle_func; source->source.func = source_idle_func;
source->source.data = data; source->source.data = data;
source->source.fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); source->source.fd = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
source->impl = impl; source->impl = impl;
source->close = true; source->close = true;
source->source.mask = SPA_IO_IN; source->source.mask = SPA_IO_IN;
@ -454,10 +453,11 @@ static void source_event_func(struct spa_source *source)
{ {
struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source); struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source);
uint64_t count; uint64_t count;
int res;
if (read(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t)) if ((res = spa_system_eventfd_read(impl->impl->system, source->fd, &count)) < 0)
spa_log_warn(impl->impl->log, NAME " %p: failed to read event fd %d: %s", spa_log_warn(impl->impl->log, NAME " %p: failed to read event fd %d: %s",
source, source->fd, strerror(errno)); source, source->fd, spa_strerror(res));
impl->func.event(source->data, count); impl->func.event(source->data, count);
} }
@ -475,7 +475,7 @@ static struct spa_source *loop_add_event(void *object,
source->source.loop = &impl->loop; source->source.loop = &impl->loop;
source->source.func = source_event_func; source->source.func = source_event_func;
source->source.data = data; source->source.data = data;
source->source.fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); source->source.fd = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
source->source.mask = SPA_IO_IN; source->source.mask = SPA_IO_IN;
source->impl = impl; source->impl = impl;
source->close = true; source->close = true;
@ -491,21 +491,23 @@ static struct spa_source *loop_add_event(void *object,
static void loop_signal_event(void *object, struct spa_source *source) static void loop_signal_event(void *object, struct spa_source *source)
{ {
struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source); struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source);
uint64_t count = 1; int res;
if (write(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t)) if ((res = spa_system_eventfd_write(impl->impl->system, source->fd, 1)) < 0)
spa_log_warn(impl->impl->log, NAME " %p: failed to write event fd %d: %s", spa_log_warn(impl->impl->log, NAME " %p: failed to write event fd %d: %s",
source, source->fd, strerror(errno)); source, source->fd, spa_strerror(res));
} }
static void source_timer_func(struct spa_source *source) static void source_timer_func(struct spa_source *source)
{ {
struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source); struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source);
uint64_t expirations; uint64_t expirations;
int res;
if (read(source->fd, &expirations, sizeof(uint64_t)) != sizeof(uint64_t)) if ((res = spa_system_timerfd_read(impl->impl->system,
source->fd, &expirations)) < 0)
spa_log_warn(impl->impl->log, NAME " %p: failed to read timer fd %d: %s", spa_log_warn(impl->impl->log, NAME " %p: failed to read timer fd %d: %s",
source, source->fd, strerror(errno)); source, source->fd, spa_strerror(res));
impl->func.timer(source->data, expirations); impl->func.timer(source->data, expirations);
} }
@ -523,7 +525,8 @@ static struct spa_source *loop_add_timer(void *object,
source->source.loop = &impl->loop; source->source.loop = &impl->loop;
source->source.func = source_timer_func; source->source.func = source_timer_func;
source->source.data = data; source->source.data = data;
source->source.fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); source->source.fd = spa_system_timerfd_create(impl->system, CLOCK_MONOTONIC,
SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
source->source.mask = SPA_IO_IN; source->source.mask = SPA_IO_IN;
source->impl = impl; source->impl = impl;
source->close = true; source->close = true;
@ -540,6 +543,7 @@ static int
loop_update_timer(void *object, struct spa_source *source, loop_update_timer(void *object, struct spa_source *source,
struct timespec *value, struct timespec *interval, bool absolute) struct timespec *value, struct timespec *interval, bool absolute)
{ {
struct impl *impl = object;
struct itimerspec its; struct itimerspec its;
int flags = 0; int flags = 0;
@ -553,9 +557,9 @@ loop_update_timer(void *object, struct spa_source *source,
if (interval) if (interval)
its.it_interval = *interval; its.it_interval = *interval;
if (absolute) if (absolute)
flags |= TFD_TIMER_ABSTIME; flags |= SPA_FD_TIMER_ABSTIME;
if (timerfd_settime(source->fd, flags, &its, NULL) < 0) if (spa_system_timerfd_settime(impl->system, source->fd, flags, &its, NULL) < 0)
return errno; return errno;
return 0; return 0;
@ -564,15 +568,13 @@ loop_update_timer(void *object, struct spa_source *source,
static void source_signal_func(struct spa_source *source) static void source_signal_func(struct spa_source *source)
{ {
struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source); struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source);
struct signalfd_siginfo signal_info; int res, signal_number;
int len;
len = read(source->fd, &signal_info, sizeof signal_info); if ((res = spa_system_signalfd_read(impl->impl->system, source->fd, &signal_number)) < 0)
if (!(len == -1 && errno == EAGAIN) && len != sizeof signal_info)
spa_log_warn(impl->impl->log, NAME " %p: failed to read signal fd %d: %s", spa_log_warn(impl->impl->log, NAME " %p: failed to read signal fd %d: %s",
source, source->fd, strerror(errno)); source, source->fd, spa_strerror(res));
impl->func.signal(source->data, impl->signal_number); impl->func.signal(source->data, signal_number);
} }
static struct spa_source *loop_add_signal(void *object, static struct spa_source *loop_add_signal(void *object,
@ -581,7 +583,6 @@ static struct spa_source *loop_add_signal(void *object,
{ {
struct impl *impl = object; struct impl *impl = object;
struct source_impl *source; struct source_impl *source;
sigset_t mask;
source = calloc(1, sizeof(struct source_impl)); source = calloc(1, sizeof(struct source_impl));
if (source == NULL) if (source == NULL)
@ -591,16 +592,13 @@ static struct spa_source *loop_add_signal(void *object,
source->source.func = source_signal_func; source->source.func = source_signal_func;
source->source.data = data; source->source.data = data;
sigemptyset(&mask); source->source.fd = spa_system_signalfd_create(impl->system,
sigaddset(&mask, signal_number); signal_number, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
source->source.fd = signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK);
sigprocmask(SIG_BLOCK, &mask, NULL);
source->source.mask = SPA_IO_IN; source->source.mask = SPA_IO_IN;
source->impl = impl; source->impl = impl;
source->close = true; source->close = true;
source->func.signal = func; source->func.signal = func;
source->signal_number = signal_number;
loop_add_source(impl, &source->source); loop_add_source(impl, &source->source);
@ -695,7 +693,7 @@ static int impl_clear(struct spa_handle *handle)
process_destroy(impl); process_destroy(impl);
close(impl->ack_fd); spa_system_close(impl->system, impl->ack_fd);
close(impl->epoll_fd); close(impl->epoll_fd);
return 0; return 0;
@ -739,8 +737,14 @@ impl_init(const struct spa_handle_factory *factory,
&impl_loop_utils, impl); &impl_loop_utils, impl);
for (i = 0; i < n_support; i++) { 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; impl->log = support[i].data;
break;
case SPA_TYPE_INTERFACE_System:
impl->system = support[i].data;
break;
}
} }
impl->epoll_fd = epoll_create1(EPOLL_CLOEXEC); impl->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
@ -754,7 +758,8 @@ impl_init(const struct spa_handle_factory *factory,
spa_ringbuffer_init(&impl->buffer); spa_ringbuffer_init(&impl->buffer);
impl->wakeup = loop_add_event(impl, wakeup_func, impl); impl->wakeup = loop_add_event(impl, wakeup_func, impl);
impl->ack_fd = eventfd(0, EFD_SEMAPHORE | EFD_CLOEXEC); impl->ack_fd = spa_system_eventfd_create(impl->system,
SPA_FD_EVENT_SEMAPHORE | SPA_FD_CLOEXEC);
spa_log_debug(impl->log, NAME " %p: initialized", impl); spa_log_debug(impl->log, NAME " %p: initialized", impl);

View file

@ -1,7 +1,8 @@
spa_support_sources = ['cpu.c', spa_support_sources = ['cpu.c',
'logger.c', 'logger.c',
'loop.c', 'loop.c',
'plugin.c'] 'plugin.c',
'system.c']
spa_support_lib = shared_library('spa-support', spa_support_lib = shared_library('spa-support',
spa_support_sources, spa_support_sources,

View file

@ -28,8 +28,9 @@
#include <spa/support/plugin.h> #include <spa/support/plugin.h>
extern const struct spa_handle_factory spa_support_logger_factory; extern const struct spa_handle_factory spa_support_logger_factory;
extern const struct spa_handle_factory spa_support_loop_factory; extern const struct spa_handle_factory spa_support_system_factory;
extern const struct spa_handle_factory spa_support_cpu_factory; extern const struct spa_handle_factory spa_support_cpu_factory;
extern const struct spa_handle_factory spa_support_loop_factory;
SPA_EXPORT SPA_EXPORT
int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t *index) int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t *index)
@ -42,11 +43,14 @@ int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t
*factory = &spa_support_logger_factory; *factory = &spa_support_logger_factory;
break; break;
case 1: case 1:
*factory = &spa_support_loop_factory; *factory = &spa_support_system_factory;
break; break;
case 2: case 2:
*factory = &spa_support_cpu_factory; *factory = &spa_support_cpu_factory;
break; break;
case 3:
*factory = &spa_support_loop_factory;
break;
default: default:
return 0; return 0;
} }

View file

@ -0,0 +1,300 @@
/* Spa
*
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/timerfd.h>
#include <sys/eventfd.h>
#include <sys/signalfd.h>
#include <spa/support/log.h>
#include <spa/support/system.h>
#include <spa/support/plugin.h>
#include <spa/utils/type.h>
#define NAME "system"
struct impl {
struct spa_handle handle;
struct spa_system system;
struct spa_log *log;
};
static ssize_t impl_read(void *object, int fd, void *buf, size_t count)
{
return read(fd, buf, count);
}
static ssize_t impl_write(void *object, int fd, const void *buf, size_t count)
{
return write(fd, buf, count);
}
static int impl_ioctl(void *object, int fd, unsigned long request, ...)
{
int res;
va_list ap;
long arg;
va_start(ap, request);
arg = va_arg(ap, long);
res = ioctl(fd, request, arg);
va_end(ap);
return res;
}
static int impl_close(void *object, int fd)
{
return close(fd);
}
/* clock */
static int impl_clock_gettime(void *object,
int clockid, struct timespec *value)
{
return clock_gettime(clockid, value);
}
static int impl_clock_getres(void *object,
int clockid, struct timespec *res)
{
return clock_getres(clockid, res);
}
/* timers */
static int impl_timerfd_create(void *object, int clockid, int flags)
{
int fl = 0;
if (flags & SPA_FD_CLOEXEC)
fl |= TFD_CLOEXEC;
if (flags & SPA_FD_NONBLOCK)
fl |= TFD_NONBLOCK;
return timerfd_create(clockid, flags);
}
static int impl_timerfd_settime(void *object,
int fd, int flags,
const struct itimerspec *new_value,
struct itimerspec *old_value)
{
int fl = 0;
if (flags & SPA_FD_TIMER_ABSTIME)
fl |= TFD_TIMER_ABSTIME;
if (flags & SPA_FD_TIMER_CANCEL_ON_SET)
fl |= TFD_TIMER_CANCEL_ON_SET;
return timerfd_settime(fd, fl, new_value, old_value);
}
static int impl_timerfd_gettime(void *object,
int fd, struct itimerspec *curr_value)
{
return timerfd_gettime(fd, curr_value);
}
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))
return -errno;
return 0;
}
/* events */
static int impl_eventfd_create(void *object, int flags)
{
int fl = 0;
if (flags & SPA_FD_CLOEXEC)
fl |= EFD_CLOEXEC;
if (flags & SPA_FD_NONBLOCK)
fl |= EFD_NONBLOCK;
if (flags & SPA_FD_EVENT_SEMAPHORE)
fl |= EFD_SEMAPHORE;
return eventfd(0, fl);
}
static int impl_eventfd_write(void *object, int fd, uint64_t count)
{
if (write(fd, &count, sizeof(uint64_t)) != sizeof(uint64_t))
return -errno;
return 0;
}
static int impl_eventfd_read(void *object, int fd, uint64_t *count)
{
if (read(fd, count, sizeof(uint64_t)) != sizeof(uint64_t))
return -errno;
return 0;
}
/* signals */
static int impl_signalfd_create(void *object, int signal, int flags)
{
sigset_t mask;
int res, fl = 0;
if (flags & SPA_FD_CLOEXEC)
fl |= SFD_CLOEXEC;
if (flags & SPA_FD_NONBLOCK)
fl |= SFD_NONBLOCK;
sigemptyset(&mask);
sigaddset(&mask, signal);
res = signalfd(-1, &mask, fl);
sigprocmask(SIG_BLOCK, &mask, NULL);
return res;
}
static int impl_signalfd_read(void *object, int fd, int *signal)
{
struct signalfd_siginfo signal_info;
int len;
len = read(fd, &signal_info, sizeof signal_info);
if (!(len == -1 && errno == EAGAIN) && len != sizeof signal_info)
return -errno;
*signal = signal_info.ssi_signo;
return 0;
}
static const struct spa_system_methods impl_system = {
SPA_VERSION_SYSTEM_METHODS,
.read = impl_read,
.write = impl_write,
.ioctl = impl_ioctl,
.close = impl_close,
.clock_gettime = impl_clock_gettime,
.clock_getres = impl_clock_getres,
.timerfd_create = impl_timerfd_create,
.timerfd_settime = impl_timerfd_settime,
.timerfd_gettime = impl_timerfd_gettime,
.timerfd_read = impl_timerfd_read,
.eventfd_create = impl_eventfd_create,
.eventfd_write = impl_eventfd_write,
.eventfd_read = impl_eventfd_read,
.signalfd_create = impl_signalfd_create,
.signalfd_read = impl_signalfd_read,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
{
struct impl *impl;
spa_return_val_if_fail(handle != NULL, -EINVAL);
spa_return_val_if_fail(interface != NULL, -EINVAL);
impl = (struct impl *) handle;
switch (type) {
case SPA_TYPE_INTERFACE_System:
*interface = &impl->system;
break;
default:
return -ENOENT;
}
return 0;
}
static int impl_clear(struct spa_handle *handle)
{
spa_return_val_if_fail(handle != NULL, -EINVAL);
return 0;
}
static size_t
impl_get_size(const struct spa_handle_factory *factory,
const struct spa_dict *params)
{
return sizeof(struct impl);
}
static int
impl_init(const struct spa_handle_factory *factory,
struct spa_handle *handle,
const struct spa_dict *info,
const struct spa_support *support,
uint32_t n_support)
{
struct impl *impl;
uint32_t i;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
handle->get_interface = impl_get_interface;
handle->clear = impl_clear;
impl = (struct impl *) handle;
impl->system.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_System,
SPA_VERSION_SYSTEM,
&impl_system, impl);
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
impl->log = support[i].data;
}
spa_log_debug(impl->log, NAME " %p: initialized", impl);
return 0;
}
static const struct spa_interface_info impl_interfaces[] = {
{SPA_TYPE_INTERFACE_System,},
};
static int
impl_enum_interface_info(const struct spa_handle_factory *factory,
const struct spa_interface_info **info,
uint32_t *index)
{
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(info != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
if (*index >= SPA_N_ELEMENTS(impl_interfaces))
return 0;
*info = &impl_interfaces[(*index)++];
return 1;
}
const struct spa_handle_factory spa_support_system_factory = {
SPA_VERSION_HANDLE_FACTORY,
NAME,
NULL,
impl_get_size,
impl_init,
impl_enum_interface_info
};

View file

@ -1030,9 +1030,8 @@ static int collect_nodes(struct pw_node *driver)
} }
quantum = SPA_MAX(quantum, MIN_QUANTUM); quantum = SPA_MAX(quantum, MIN_QUANTUM);
if (driver->rt.position && quantum != driver->rt.position->size) { if (driver->rt.position && quantum != driver->rt.position->size)
driver->rt.position->size = quantum; driver->rt.position->size = quantum;
}
return 0; return 0;
} }

View file

@ -576,6 +576,7 @@ int pw_node_set_driver(struct pw_node *node, struct pw_node *driver)
return 0; return 0;
node->master = node->driver && driver == node; node->master = node->driver && driver == node;
pw_log_info("node %p: driver %p master:%u", node, driver, node->master);
node->driver_node = driver; node->driver_node = driver;
pw_node_emit_driver_changed(node, old, driver); pw_node_emit_driver_changed(node, old, driver);
@ -1382,7 +1383,7 @@ static void node_activate(struct pw_node *this)
} }
} }
/** Set th node state /** Set the node state
* \param node a \ref pw_node * \param node a \ref pw_node
* \param state a \ref pw_node_state * \param state a \ref pw_node_state
* \return 0 on success < 0 on error * \return 0 on success < 0 on error

View file

@ -378,8 +378,18 @@ void pw_init(int *argc, char **argv[])
SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, iface); SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, iface);
pw_log_set(iface); pw_log_set(iface);
} }
handle = load_handle(plugin, "system", NULL, support->n_support, support->support);
if (handle == NULL ||
(res = spa_handle_get_interface(&handle->handle,
SPA_TYPE_INTERFACE_System, &iface)) < 0) {
fprintf(stderr, "can't get System interface %d\n", res);
}
else {
support->support[support->n_support++] =
SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_System, iface);
}
handle = load_handle(plugin, "cpu", &info, support->n_support, support->support); handle = load_handle(plugin, "cpu", NULL, support->n_support, support->support);
if (handle == NULL || if (handle == NULL ||
(res = spa_handle_get_interface(&handle->handle, (res = spa_handle_get_interface(&handle->handle,
SPA_TYPE_INTERFACE_CPU, &iface)) < 0) { SPA_TYPE_INTERFACE_CPU, &iface)) < 0) {