mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-06 13:30:01 -05:00
Refactor the work queue
Make a separate work queue to track async operations. Keep separate work queues for links and nodes. This avoids lockups when some async operation take a long time and the work queue has SYNC_WAIT operations.
This commit is contained in:
parent
6d4db64767
commit
474981ddda
9 changed files with 344 additions and 238 deletions
|
|
@ -27,156 +27,13 @@
|
|||
#include "pinos/client/log.h"
|
||||
#include "pinos/server/main-loop.h"
|
||||
|
||||
typedef struct _WorkItem WorkItem;
|
||||
|
||||
struct _WorkItem {
|
||||
uint32_t id;
|
||||
void *obj;
|
||||
uint32_t seq;
|
||||
SpaResult res;
|
||||
PinosDeferFunc func;
|
||||
void *data;
|
||||
SpaList link;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PinosMainLoop this;
|
||||
|
||||
bool running;
|
||||
SpaSource *wakeup;
|
||||
|
||||
uint32_t counter;
|
||||
|
||||
SpaList work_list;
|
||||
SpaList free_list;
|
||||
} PinosMainLoopImpl;
|
||||
|
||||
static void
|
||||
process_work_queue (SpaSource *source,
|
||||
void *data)
|
||||
{
|
||||
PinosMainLoopImpl *impl = data;
|
||||
PinosMainLoop *this = &impl->this;
|
||||
WorkItem *item, *tmp;
|
||||
|
||||
spa_list_for_each_safe (item, tmp, &impl->work_list, link) {
|
||||
if (item->seq != SPA_ID_INVALID) {
|
||||
pinos_log_debug ("main-loop %p: waiting for item %p %d", this, item->obj, item->seq);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item->res == SPA_RESULT_WAIT_SYNC && item != spa_list_first (&impl->work_list, WorkItem, link)) {
|
||||
pinos_log_debug ("main-loop %p: sync item %p not head", this, item->obj);
|
||||
continue;
|
||||
}
|
||||
|
||||
spa_list_remove (&item->link);
|
||||
|
||||
if (item->func) {
|
||||
pinos_log_debug ("main-loop %p: process work item %p %d", this, item->obj, item->seq);
|
||||
item->func (item->obj, item->data, item->res, item->id);
|
||||
}
|
||||
spa_list_insert (impl->free_list.prev, &item->link);
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
main_loop_defer (PinosMainLoop *loop,
|
||||
void *obj,
|
||||
SpaResult res,
|
||||
PinosDeferFunc func,
|
||||
void *data)
|
||||
{
|
||||
PinosMainLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosMainLoopImpl, this);
|
||||
WorkItem *item;
|
||||
bool have_work = false;
|
||||
|
||||
if (!spa_list_is_empty (&impl->free_list)) {
|
||||
item = spa_list_first (&impl->free_list, WorkItem, link);
|
||||
spa_list_remove (&item->link);
|
||||
} else {
|
||||
item = malloc (sizeof (WorkItem));
|
||||
if (item == NULL)
|
||||
return SPA_ID_INVALID;
|
||||
}
|
||||
item->id = ++impl->counter;
|
||||
item->obj = obj;
|
||||
item->func = func;
|
||||
item->data = data;
|
||||
|
||||
if (SPA_RESULT_IS_ASYNC (res)) {
|
||||
item->seq = SPA_RESULT_ASYNC_SEQ (res);
|
||||
item->res = res;
|
||||
pinos_log_debug ("main-loop %p: defer async %d for object %p", loop, item->seq, obj);
|
||||
} else if (res == SPA_RESULT_WAIT_SYNC) {
|
||||
pinos_log_debug ("main-loop %p: wait sync object %p", loop, obj);
|
||||
item->seq = SPA_ID_INVALID;
|
||||
item->res = res;
|
||||
have_work = true;
|
||||
} else {
|
||||
item->seq = SPA_ID_INVALID;
|
||||
item->res = res;
|
||||
have_work = true;
|
||||
pinos_log_debug ("main-loop %p: defer object %p", loop, obj);
|
||||
}
|
||||
spa_list_insert (impl->work_list.prev, &item->link);
|
||||
|
||||
if (have_work)
|
||||
pinos_loop_signal_event (impl->this.loop, impl->wakeup);
|
||||
|
||||
return item->id;
|
||||
}
|
||||
|
||||
static void
|
||||
main_loop_defer_cancel (PinosMainLoop *loop,
|
||||
void *obj,
|
||||
uint32_t id)
|
||||
{
|
||||
PinosMainLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosMainLoopImpl, this);
|
||||
bool have_work = false;
|
||||
WorkItem *item;
|
||||
|
||||
spa_list_for_each (item, &impl->work_list, link) {
|
||||
if ((id == 0 || item->id == id) && (obj == NULL || item->obj == obj)) {
|
||||
pinos_log_debug ("main-loop %p: cancel defer %d for object %p", loop, item->seq, item->obj);
|
||||
item->seq = SPA_ID_INVALID;
|
||||
item->func = NULL;
|
||||
have_work = true;
|
||||
}
|
||||
}
|
||||
if (have_work)
|
||||
pinos_loop_signal_event (impl->this.loop, impl->wakeup);
|
||||
}
|
||||
|
||||
static bool
|
||||
main_loop_defer_complete (PinosMainLoop *loop,
|
||||
void *obj,
|
||||
uint32_t seq,
|
||||
SpaResult res)
|
||||
{
|
||||
WorkItem *item;
|
||||
PinosMainLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosMainLoopImpl, this);
|
||||
bool have_work = false;
|
||||
|
||||
spa_list_for_each (item, &impl->work_list, link) {
|
||||
if (item->obj == obj && item->seq == seq) {
|
||||
pinos_log_debug ("main-loop %p: found defered %d for object %p", loop, seq, obj);
|
||||
item->seq = SPA_ID_INVALID;
|
||||
item->res = res;
|
||||
have_work = true;
|
||||
}
|
||||
}
|
||||
if (!have_work) {
|
||||
pinos_log_debug ("main-loop %p: no defered %d found for object %p", loop, seq, obj);
|
||||
} else {
|
||||
pinos_loop_signal_event (impl->this.loop, impl->wakeup);
|
||||
}
|
||||
|
||||
return have_work;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pinos_main_loop_new:
|
||||
*
|
||||
|
|
@ -203,17 +60,6 @@ pinos_main_loop_new (void)
|
|||
|
||||
pinos_signal_init (&this->destroy_signal);
|
||||
|
||||
impl->wakeup = pinos_loop_add_event (this->loop,
|
||||
process_work_queue,
|
||||
impl);
|
||||
|
||||
this->defer = main_loop_defer;
|
||||
this->defer_cancel = main_loop_defer_cancel;
|
||||
this->defer_complete = main_loop_defer_complete;
|
||||
|
||||
spa_list_init (&impl->work_list);
|
||||
spa_list_init (&impl->free_list);
|
||||
|
||||
return this;
|
||||
|
||||
no_loop:
|
||||
|
|
@ -225,16 +71,12 @@ void
|
|||
pinos_main_loop_destroy (PinosMainLoop *loop)
|
||||
{
|
||||
PinosMainLoopImpl *impl = SPA_CONTAINER_OF (loop, PinosMainLoopImpl, this);
|
||||
WorkItem *item, *tmp;
|
||||
|
||||
pinos_log_debug ("main-loop %p: destroy", impl);
|
||||
pinos_signal_emit (&loop->destroy_signal, loop);
|
||||
|
||||
pinos_loop_destroy_source (loop->loop, impl->wakeup);
|
||||
pinos_loop_destroy (loop->loop);
|
||||
|
||||
spa_list_for_each_safe (item, tmp, &impl->free_list, link)
|
||||
free (item);
|
||||
free (impl);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue