loop: add method to run a function with the lock

Convert some _invoke to _locked
This commit is contained in:
Wim Taymans 2025-03-10 17:24:13 +01:00
parent fb49e0795c
commit f7fdafc203
18 changed files with 99 additions and 57 deletions

View file

@ -684,7 +684,7 @@ static void stop_driver_timer(struct impl *this)
/* Perform the actual stop within
* the dataloop to avoid data races. */
spa_loop_invoke(this->data_loop, do_remove_driver_timer_source, 0, NULL, 0, true, this);
spa_loop_locked(this->data_loop, do_remove_driver_timer_source, 0, NULL, 0, this);
}
static void on_driver_timeout(struct spa_source *source)
@ -795,7 +795,7 @@ static void reevaluate_following_state(struct impl *this)
if (following != this->following) {
spa_log_debug(this->log, "%p: following state changed: %d->%d", this, this->following, following);
this->following = following;
spa_loop_invoke(this->data_loop, do_reevaluate_following_state, 0, NULL, 0, true, this);
spa_loop_locked(this->data_loop, do_reevaluate_following_state, 0, NULL, 0, this);
}
}

View file

@ -3739,7 +3739,7 @@ int spa_alsa_start(struct state *state)
}
state->started = true;
spa_loop_invoke(state->data_loop, do_state_sync, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_state_sync, 0, NULL, 0, state);
return 0;
}
@ -3783,7 +3783,7 @@ int spa_alsa_reassign_follower(struct state *state)
}
setup_matching(state);
if (state->started)
spa_loop_invoke(state->data_loop, do_state_sync, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_state_sync, 0, NULL, 0, state);
else if (state->want_started)
spa_alsa_start(state);
@ -3812,7 +3812,7 @@ int spa_alsa_pause(struct state *state)
spa_log_debug(state->log, "%p: pause", state);
state->started = false;
spa_loop_invoke(state->data_loop, do_state_sync, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_state_sync, 0, NULL, 0, state);
spa_list_for_each(follower, &state->followers, driver_link)
spa_alsa_pause(follower);

View file

@ -1176,7 +1176,7 @@ int spa_alsa_seq_reassign_follower(struct seq_state *state)
if (following != state->following) {
spa_log_debug(state->log, "alsa %p: reassign follower %d->%d", state, state->following, following);
state->following = following;
spa_loop_invoke(state->data_loop, do_reassign_follower, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_reassign_follower, 0, NULL, 0, state);
}
return 0;
}
@ -1205,7 +1205,7 @@ int spa_alsa_seq_pause(struct seq_state *state)
spa_log_debug(state->log, "alsa %p: pause", state);
spa_loop_invoke(state->data_loop, do_remove_source, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_remove_source, 0, NULL, 0, state);
if ((res = snd_seq_stop_queue(state->event.hndl, state->event.queue_id, NULL)) < 0) {
spa_log_warn(state->log, "failed to stop queue: %s", snd_strerror(res));

View file

@ -1386,7 +1386,7 @@ static int load_filter_graph(struct impl *impl, const char *graph, int order)
}
res = setup_filter_graphs(impl);
spa_loop_invoke(impl->data_loop, do_sync_filter_graph, 0, NULL, 0, true, impl);
spa_loop_locked(impl->data_loop, do_sync_filter_graph, 0, NULL, 0, impl);
if (impl->in_filter_props == 0)
clean_filter_handles(impl, false);
@ -3283,7 +3283,7 @@ impl_node_port_set_io(void *object,
case SPA_IO_Buffers:
if (this->data_loop) {
struct io_data d = { .port = port, .data = data, .size = size };
spa_loop_invoke(this->data_loop, do_set_port_io, 0, NULL, 0, true, &d);
spa_loop_locked(this->data_loop, do_set_port_io, 0, NULL, 0, &d);
}
else
port->io = data;

View file

@ -744,8 +744,8 @@ impl_node_port_set_io(void *object,
switch (id) {
case SPA_IO_Buffers:
case SPA_IO_AsyncBuffers:
spa_loop_invoke(this->data_loop,
do_port_set_io, SPA_ID_INVALID, NULL, 0, true, &info);
spa_loop_locked(this->data_loop,
do_port_set_io, SPA_ID_INVALID, NULL, 0, &info);
break;
default:
return -ENOENT;

View file

@ -680,8 +680,8 @@ impl_node_port_set_io(void *object,
switch (id) {
case SPA_IO_Buffers:
case SPA_IO_AsyncBuffers:
spa_loop_invoke(this->data_loop,
do_port_set_io, SPA_ID_INVALID, NULL, 0, true, &info);
spa_loop_locked(this->data_loop,
do_port_set_io, SPA_ID_INVALID, NULL, 0, &info);
break;
default:
return -ENOENT;

View file

@ -1013,7 +1013,7 @@ static int impl_clear(struct spa_handle *handle)
this = (struct impl *) handle;
if (this->data_loop)
spa_loop_invoke(this->data_loop, do_remove_timer, 0, NULL, 0, true, this);
spa_loop_locked(this->data_loop, do_remove_timer, 0, NULL, 0, this);
spa_system_close(this->data_system, this->timer_source.fd);
return 0;

View file

@ -1131,7 +1131,7 @@ int spa_avb_reassign_follower(struct state *state)
if (following != state->following) {
spa_log_debug(state->log, "%p: reassign follower %d->%d", state, state->following, following);
state->following = following;
spa_loop_invoke(state->data_loop, do_reassign_follower, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_reassign_follower, 0, NULL, 0, state);
}
freewheel = state->position &&
@ -1208,7 +1208,7 @@ int spa_avb_pause(struct state *state)
spa_log_debug(state->log, "%p: pause", state);
spa_loop_invoke(state->data_loop, do_remove_source, 0, NULL, 0, true, state);
spa_loop_locked(state->data_loop, do_remove_source, 0, NULL, 0, state);
state->started = false;

View file

@ -605,8 +605,8 @@ impl_node_port_set_io(void *object,
switch (id) {
case SPA_IO_Buffers:
case SPA_IO_AsyncBuffers:
spa_loop_invoke(this->data_loop,
do_port_set_io, SPA_ID_INVALID, NULL, 0, true, &info);
spa_loop_locked(this->data_loop,
do_port_set_io, SPA_ID_INVALID, NULL, 0, &info);
break;
default:
return -ENOENT;

View file

@ -195,7 +195,6 @@ static int remove_from_poll(struct impl *impl, struct spa_source *source)
static int loop_remove_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
spa_assert(!impl->polling);
int res = remove_from_poll(impl, source);
detach_source(source);
@ -553,6 +552,16 @@ static int loop_invoke(void *object, spa_invoke_func_t func, uint32_t seq,
}
return res;
}
static int loop_locked(void *object, spa_invoke_func_t func, uint32_t seq,
const void *data, size_t size, void *user_data)
{
struct impl *impl = object;
int res;
pthread_mutex_lock(&impl->lock);
res = func(&impl->loop, false, seq, data, size, user_data);
pthread_mutex_unlock(&impl->lock);
return res;
}
static int loop_get_fd(void *object)
{
@ -1214,6 +1223,7 @@ static const struct spa_loop_methods impl_loop = {
.update_source = loop_update_source,
.remove_source = loop_remove_source,
.invoke = loop_invoke,
.locked = loop_locked,
};
static const struct spa_loop_control_methods impl_loop_control_cancel = {