From 5ab031b472f00f6eefee517b9829ef06a6d8bd3b Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 27 Jan 2022 15:07:38 +0100 Subject: [PATCH] loop: remove the eventfd to stop the loop We can just as well use _invoke to schedule a task in the context of the loop. --- src/pipewire/data-loop.c | 33 ++++++++++++--------------------- src/pipewire/main-loop.c | 17 +++++------------ src/pipewire/private.h | 3 +-- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/src/pipewire/data-loop.c b/src/pipewire/data-loop.c index 2651cbdf5..30dbb85c9 100644 --- a/src/pipewire/data-loop.c +++ b/src/pipewire/data-loop.c @@ -90,11 +90,13 @@ static void *do_loop(void *user_data) return NULL; } -static void do_stop(void *data, uint64_t count) +static int do_stop(struct spa_loop *loop, bool async, uint32_t seq, + const void *data, size_t size, void *user_data) { - struct pw_data_loop *this = data; + struct pw_data_loop *this = user_data; pw_log_debug("%p: stopping", this); this->running = false; + return 0; } static struct pw_data_loop *loop_new(struct pw_loop *loop, const struct spa_dict *props) @@ -122,23 +124,14 @@ static struct pw_data_loop *loop_new(struct pw_loop *loop, const struct spa_dict } this->loop = loop; - if (props == NULL || - (str = spa_dict_lookup(props, "loop.cancel")) == NULL || - pw_properties_parse_bool(str) == false) { - this->event = pw_loop_add_event(this->loop, do_stop, this); - if (this->event == NULL) { - res = -errno; - pw_log_error("%p: can't add event: %m", this); - goto error_loop_destroy; - } - } + if (props != NULL && + (str = spa_dict_lookup(props, "loop.cancel")) != NULL) + this->cancel = pw_properties_parse_bool(str); + spa_hook_list_init(&this->listener_list); return this; -error_loop_destroy: - if (this->created && this->loop) - pw_loop_destroy(this->loop); error_free: free(this); error_cleanup: @@ -169,8 +162,6 @@ void pw_data_loop_destroy(struct pw_data_loop *loop) pw_data_loop_stop(loop); - if (loop->event) - pw_loop_destroy_source(loop->loop, loop->event); if (loop->created) pw_loop_destroy(loop->loop); @@ -232,12 +223,12 @@ int pw_data_loop_stop(struct pw_data_loop *loop) { pw_log_debug("%p stopping", loop); if (loop->running) { - if (loop->event) { - pw_log_debug("%p signal", loop); - pw_loop_signal_event(loop->loop, loop->event); - } else { + if (loop->cancel) { pw_log_debug("%p cancel", loop); pthread_cancel(loop->thread); + } else { + pw_log_debug("%p signal", loop); + pw_loop_invoke(loop->loop, do_stop, 1, NULL, 0, false, loop); } pw_log_debug("%p join", loop); pw_thread_utils_join((struct spa_thread*)loop->thread, NULL); diff --git a/src/pipewire/main-loop.c b/src/pipewire/main-loop.c index 57be7649d..4e8c8aa38 100644 --- a/src/pipewire/main-loop.c +++ b/src/pipewire/main-loop.c @@ -29,11 +29,13 @@ PW_LOG_TOPIC_EXTERN(log_main_loop); #define PW_LOG_TOPIC_DEFAULT log_main_loop -static void do_stop(void *data, uint64_t count) +static int do_stop(struct spa_loop *loop, bool async, uint32_t seq, + const void *data, size_t size, void *user_data) { - struct pw_main_loop *this = data; + struct pw_main_loop *this = user_data; pw_log_debug("%p: do stop", this); this->running = false; + return 0; } static struct pw_main_loop *loop_new(struct pw_loop *loop, const struct spa_dict *props) @@ -59,19 +61,10 @@ static struct pw_main_loop *loop_new(struct pw_loop *loop, const struct spa_dict } this->loop = loop; - this->event = pw_loop_add_event(this->loop, do_stop, this); - if (this->event == NULL) { - res = -errno; - goto error_free_loop; - } - spa_hook_list_init(&this->listener_list); return this; -error_free_loop: - if (this->created && this->loop) - pw_loop_destroy(this->loop); error_free: free(this); error_cleanup: @@ -132,7 +125,7 @@ SPA_EXPORT int pw_main_loop_quit(struct pw_main_loop *loop) { pw_log_debug("%p: quit", loop); - return pw_loop_signal_event(loop->loop, loop->event); + return pw_loop_invoke(loop->loop, do_stop, 1, NULL, 0, false, loop); } /** Start a main loop diff --git a/src/pipewire/private.h b/src/pipewire/private.h index 9af21a5e5..f36f4e746 100644 --- a/src/pipewire/private.h +++ b/src/pipewire/private.h @@ -472,9 +472,9 @@ struct pw_data_loop { struct pw_loop *loop; struct spa_hook_list listener_list; - struct spa_source *event; pthread_t thread; + unsigned int cancel:1; unsigned int created:1; unsigned int running:1; }; @@ -486,7 +486,6 @@ struct pw_main_loop { struct pw_loop *loop; struct spa_hook_list listener_list; - struct spa_source *event; unsigned int created:1; unsigned int running:1;