mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-12-19 08:57:14 -05:00
Work on event loop
Make a new epoll based event loop and use it for the data tranport. Simplify the spa event api a little and rename to SpaLoop
This commit is contained in:
parent
0d2fa5ebc8
commit
ae93f15965
33 changed files with 1286 additions and 954 deletions
|
|
@ -28,39 +28,15 @@
|
|||
#include <sys/resource.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "spa/include/spa/ringbuffer.h"
|
||||
#include "pinos/client/log.h"
|
||||
#include "pinos/client/rtkit.h"
|
||||
#include "pinos/server/data-loop.h"
|
||||
|
||||
#define DATAS_SIZE (4096 * 8)
|
||||
|
||||
typedef struct {
|
||||
size_t item_size;
|
||||
SpaPollInvokeFunc func;
|
||||
uint32_t seq;
|
||||
size_t size;
|
||||
void *data;
|
||||
void *user_data;
|
||||
} InvokeItem;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PinosDataLoop this;
|
||||
|
||||
SpaRingbuffer buffer;
|
||||
uint8_t buffer_data[DATAS_SIZE];
|
||||
|
||||
unsigned int n_poll;
|
||||
SpaPollItem poll[16];
|
||||
int idx[16];
|
||||
|
||||
bool rebuild_fds;
|
||||
SpaPollFd fds[32];
|
||||
unsigned int n_fds;
|
||||
|
||||
uint32_t counter;
|
||||
uint32_t seq;
|
||||
PinosSource *event;
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
|
@ -111,266 +87,31 @@ make_realtime (PinosDataLoop *this)
|
|||
}
|
||||
|
||||
static void *
|
||||
loop (void *user_data)
|
||||
do_loop (void *user_data)
|
||||
{
|
||||
PinosDataLoopImpl *impl = user_data;
|
||||
PinosDataLoop *this = &impl->this;
|
||||
SpaPoll *p = &this->poll;
|
||||
unsigned int i, j;
|
||||
SpaResult res;
|
||||
|
||||
make_realtime (this);
|
||||
|
||||
pinos_log_debug ("data-loop %p: enter thread", this);
|
||||
while (impl->running) {
|
||||
SpaPollNotifyData ndata;
|
||||
unsigned int n_idle = 0;
|
||||
int r;
|
||||
|
||||
/* prepare */
|
||||
for (i = 0; i < impl->n_poll; i++) {
|
||||
SpaPollItem *p = &impl->poll[i];
|
||||
|
||||
if (p->enabled && p->idle_cb) {
|
||||
ndata.fds = NULL;
|
||||
ndata.n_fds = 0;
|
||||
ndata.user_data = p->user_data;
|
||||
if (SPA_RESULT_IS_ERROR (p->idle_cb (&ndata)))
|
||||
p->enabled = false;
|
||||
n_idle++;
|
||||
}
|
||||
}
|
||||
// if (n_idle > 0)
|
||||
// continue;
|
||||
|
||||
/* rebuild */
|
||||
if (impl->rebuild_fds) {
|
||||
impl->n_fds = 1;
|
||||
for (i = 0; i < impl->n_poll; i++) {
|
||||
SpaPollItem *p = &impl->poll[i];
|
||||
|
||||
if (!p->enabled)
|
||||
continue;
|
||||
|
||||
for (j = 0; j < p->n_fds; j++)
|
||||
impl->fds[impl->n_fds + j] = p->fds[j];
|
||||
impl->idx[i] = impl->n_fds;
|
||||
impl->n_fds += p->n_fds;
|
||||
}
|
||||
impl->rebuild_fds = false;
|
||||
}
|
||||
|
||||
/* before */
|
||||
for (i = 0; i < impl->n_poll; i++) {
|
||||
SpaPollItem *p = &impl->poll[i];
|
||||
|
||||
if (p->enabled && p->before_cb) {
|
||||
ndata.fds = &impl->fds[impl->idx[i]];
|
||||
ndata.n_fds = p->n_fds;
|
||||
ndata.user_data = p->user_data;
|
||||
if (SPA_RESULT_IS_ERROR (p->before_cb (&ndata)))
|
||||
p->enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
r = poll ((struct pollfd *) impl->fds, impl->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
pinos_log_warn ("data-loop %p: select timeout should not happen", this);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* check wakeup */
|
||||
if (impl->fds[0].revents & POLLIN) {
|
||||
uint64_t u;
|
||||
size_t offset;
|
||||
|
||||
if (read (impl->fds[0].fd, &u, sizeof(uint64_t)) != sizeof(uint64_t))
|
||||
pinos_log_warn ("data-loop %p: failed to read fd: %s", this, strerror (errno));
|
||||
|
||||
while (spa_ringbuffer_get_read_offset (&impl->buffer, &offset) > 0) {
|
||||
InvokeItem *item = SPA_MEMBER (impl->buffer_data, offset, InvokeItem);
|
||||
item->func (p, true, item->seq, item->size, item->data, item->user_data);
|
||||
spa_ringbuffer_read_advance (&impl->buffer, item->item_size);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < impl->n_poll; i++) {
|
||||
SpaPollItem *p = &impl->poll[i];
|
||||
|
||||
if (p->enabled && p->after_cb && (p->n_fds == 0 || impl->fds[impl->idx[i]].revents != 0)) {
|
||||
ndata.fds = &impl->fds[impl->idx[i]];
|
||||
ndata.n_fds = p->n_fds;
|
||||
ndata.user_data = p->user_data;
|
||||
if (SPA_RESULT_IS_ERROR (p->after_cb (&ndata)))
|
||||
p->enabled = false;
|
||||
}
|
||||
}
|
||||
if ((res = pinos_loop_iterate (this->loop, -1)) < 0)
|
||||
pinos_log_warn ("data-loop %p: iterate error %d", this, res);
|
||||
}
|
||||
pinos_log_debug ("data-loop %p: leave thread", this);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
wakeup_thread (PinosDataLoopImpl *impl)
|
||||
{
|
||||
uint64_t u = 1;
|
||||
|
||||
if (write (impl->fds[0].fd, &u, sizeof(uint64_t)) != sizeof(uint64_t))
|
||||
pinos_log_warn ("data-loop %p: failed to write fd: %s", impl, strerror (errno));
|
||||
}
|
||||
|
||||
static void
|
||||
start_thread (PinosDataLoopImpl *impl)
|
||||
do_stop (PinosSource *source,
|
||||
void *data)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!impl->running) {
|
||||
impl->running = true;
|
||||
if ((err = pthread_create (&impl->thread, NULL, loop, impl)) != 0) {
|
||||
pinos_log_warn ("data-loop %p: can't create thread: %s", impl, strerror (err));
|
||||
impl->running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
stop_thread (PinosDataLoopImpl *impl, bool in_thread)
|
||||
{
|
||||
if (impl->running) {
|
||||
impl->running = false;
|
||||
if (!in_thread) {
|
||||
wakeup_thread (impl);
|
||||
pthread_join (impl->thread, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static SpaResult
|
||||
do_add_item (SpaPoll *poll,
|
||||
SpaPollItem *item)
|
||||
{
|
||||
PinosDataLoop *this = SPA_CONTAINER_OF (poll, PinosDataLoop, poll);
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (this, PinosDataLoopImpl, this);
|
||||
bool in_thread = pthread_equal (impl->thread, pthread_self());
|
||||
|
||||
item->id = ++impl->counter;
|
||||
impl->poll[impl->n_poll] = *item;
|
||||
impl->n_poll++;
|
||||
if (item->n_fds)
|
||||
impl->rebuild_fds = true;
|
||||
|
||||
if (!in_thread) {
|
||||
wakeup_thread (impl);
|
||||
start_thread (impl);
|
||||
}
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
|
||||
static SpaResult
|
||||
do_update_item (SpaPoll *poll,
|
||||
SpaPollItem *item)
|
||||
{
|
||||
PinosDataLoop *this = SPA_CONTAINER_OF (poll, PinosDataLoop, poll);
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (this, PinosDataLoopImpl, this);
|
||||
bool in_thread = pthread_equal (impl->thread, pthread_self());
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < impl->n_poll; i++) {
|
||||
if (impl->poll[i].id == item->id)
|
||||
impl->poll[i] = *item;
|
||||
}
|
||||
if (item->n_fds)
|
||||
impl->rebuild_fds = true;
|
||||
|
||||
if (!in_thread)
|
||||
wakeup_thread (impl);
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
static SpaResult
|
||||
do_remove_item (SpaPoll *poll,
|
||||
SpaPollItem *item)
|
||||
{
|
||||
PinosDataLoop *this = SPA_CONTAINER_OF (poll, PinosDataLoop, poll);
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (this, PinosDataLoopImpl, this);
|
||||
bool in_thread = pthread_equal (impl->thread, pthread_self());
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < impl->n_poll; i++) {
|
||||
if (impl->poll[i].id == item->id) {
|
||||
impl->n_poll--;
|
||||
for (; i < impl->n_poll; i++)
|
||||
impl->poll[i] = impl->poll[i+1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (item->n_fds) {
|
||||
impl->rebuild_fds = true;
|
||||
if (!in_thread)
|
||||
wakeup_thread (impl);
|
||||
}
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
static SpaResult
|
||||
do_invoke (SpaPoll *poll,
|
||||
SpaPollInvokeFunc func,
|
||||
uint32_t seq,
|
||||
size_t size,
|
||||
void *data,
|
||||
void *user_data)
|
||||
{
|
||||
PinosDataLoop *this = SPA_CONTAINER_OF (poll, PinosDataLoop, poll);
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (this, PinosDataLoopImpl, this);
|
||||
bool in_thread = pthread_equal (impl->thread, pthread_self());
|
||||
SpaRingbufferArea areas[2];
|
||||
InvokeItem *item;
|
||||
SpaResult res;
|
||||
|
||||
if (in_thread) {
|
||||
res = func (poll, false, seq, size, data, user_data);
|
||||
} else {
|
||||
spa_ringbuffer_get_write_areas (&impl->buffer, areas);
|
||||
if (areas[0].len < sizeof (InvokeItem)) {
|
||||
pinos_log_warn ("queue full");
|
||||
return SPA_RESULT_ERROR;
|
||||
}
|
||||
item = SPA_MEMBER (impl->buffer_data, areas[0].offset, InvokeItem);
|
||||
item->seq = seq;
|
||||
item->func = func;
|
||||
item->user_data = user_data;
|
||||
item->size = size;
|
||||
|
||||
if (areas[0].len > sizeof (InvokeItem) + size) {
|
||||
item->data = SPA_MEMBER (item, sizeof (InvokeItem), void);
|
||||
item->item_size = sizeof (InvokeItem) + size;
|
||||
if (areas[0].len < sizeof (InvokeItem) + item->item_size)
|
||||
item->item_size = areas[0].len;
|
||||
} else {
|
||||
item->data = SPA_MEMBER (impl->buffer_data, areas[1].offset, void);
|
||||
item->item_size = areas[0].len + 1 + size;
|
||||
}
|
||||
memcpy (item->data, data, size);
|
||||
|
||||
spa_ringbuffer_write_advance (&impl->buffer, item->item_size);
|
||||
|
||||
wakeup_thread (impl);
|
||||
|
||||
if (seq != SPA_ID_INVALID)
|
||||
res = SPA_RESULT_RETURN_ASYNC (seq);
|
||||
else
|
||||
res = SPA_RESULT_OK;
|
||||
}
|
||||
return res;
|
||||
PinosDataLoopImpl *impl = data;
|
||||
impl->running = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -387,38 +128,64 @@ pinos_data_loop_new (void)
|
|||
PinosDataLoop *this;
|
||||
|
||||
impl = calloc (1, sizeof (PinosDataLoopImpl));
|
||||
this = &impl->this;
|
||||
|
||||
pinos_log_debug ("data-loop %p: new", impl);
|
||||
|
||||
this->poll.size = sizeof (SpaPoll);
|
||||
this->poll.info = NULL;
|
||||
this->poll.add_item = do_add_item;
|
||||
this->poll.update_item = do_update_item;
|
||||
this->poll.remove_item = do_remove_item;
|
||||
this->poll.invoke = do_invoke;
|
||||
|
||||
impl->fds[0].fd = eventfd (0, 0);
|
||||
impl->fds[0].events = POLLIN | POLLPRI | POLLERR;
|
||||
impl->fds[0].revents = 0;
|
||||
impl->n_fds = 1;
|
||||
|
||||
spa_ringbuffer_init (&impl->buffer, DATAS_SIZE);
|
||||
this = &impl->this;
|
||||
this->loop = pinos_loop_new ();
|
||||
pinos_signal_init (&this->destroy_signal);
|
||||
|
||||
impl->event = pinos_loop_add_event (this->loop,
|
||||
do_stop,
|
||||
impl);
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
pinos_data_loop_destroy (PinosDataLoop * loop)
|
||||
pinos_data_loop_destroy (PinosDataLoop *loop)
|
||||
{
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosDataLoopImpl, this);
|
||||
|
||||
pinos_log_debug ("data-loop %p: destroy", impl);
|
||||
stop_thread (impl, false);
|
||||
close (impl->fds[0].fd);
|
||||
pinos_signal_emit (&loop->destroy_signal, loop);
|
||||
|
||||
pinos_data_loop_stop (loop);
|
||||
|
||||
pinos_source_destroy (impl->event);
|
||||
pinos_loop_destroy (loop->loop);
|
||||
free (impl);
|
||||
}
|
||||
|
||||
SpaResult
|
||||
pinos_data_loop_start (PinosDataLoop *loop)
|
||||
{
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosDataLoopImpl, this);
|
||||
|
||||
if (!impl->running) {
|
||||
int err;
|
||||
|
||||
impl->running = true;
|
||||
if ((err = pthread_create (&impl->thread, NULL, do_loop, impl)) != 0) {
|
||||
pinos_log_warn ("data-loop %p: can't create thread: %s", impl, strerror (err));
|
||||
impl->running = false;
|
||||
return SPA_RESULT_ERROR;
|
||||
}
|
||||
pinos_loop_set_thread (impl->this.loop, &impl->thread);
|
||||
}
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
SpaResult
|
||||
pinos_data_loop_stop (PinosDataLoop *loop)
|
||||
{
|
||||
PinosDataLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosDataLoopImpl, this);
|
||||
|
||||
pinos_source_event_signal (impl->event);
|
||||
|
||||
pthread_join (impl->thread, NULL);
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
bool
|
||||
pinos_data_loop_in_thread (PinosDataLoop *loop)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue