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.
This commit is contained in:
Wim Taymans 2022-01-27 15:07:38 +01:00
parent 3256c6e5e7
commit 5ab031b472
3 changed files with 18 additions and 35 deletions

View file

@ -90,11 +90,13 @@ static void *do_loop(void *user_data)
return NULL; 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); pw_log_debug("%p: stopping", this);
this->running = false; this->running = false;
return 0;
} }
static struct pw_data_loop *loop_new(struct pw_loop *loop, const struct spa_dict *props) 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; this->loop = loop;
if (props == NULL || if (props != NULL &&
(str = spa_dict_lookup(props, "loop.cancel")) == NULL || (str = spa_dict_lookup(props, "loop.cancel")) != NULL)
pw_properties_parse_bool(str) == false) { this->cancel = pw_properties_parse_bool(str);
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;
}
}
spa_hook_list_init(&this->listener_list); spa_hook_list_init(&this->listener_list);
return this; return this;
error_loop_destroy:
if (this->created && this->loop)
pw_loop_destroy(this->loop);
error_free: error_free:
free(this); free(this);
error_cleanup: error_cleanup:
@ -169,8 +162,6 @@ void pw_data_loop_destroy(struct pw_data_loop *loop)
pw_data_loop_stop(loop); pw_data_loop_stop(loop);
if (loop->event)
pw_loop_destroy_source(loop->loop, loop->event);
if (loop->created) if (loop->created)
pw_loop_destroy(loop->loop); 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); pw_log_debug("%p stopping", loop);
if (loop->running) { if (loop->running) {
if (loop->event) { if (loop->cancel) {
pw_log_debug("%p signal", loop);
pw_loop_signal_event(loop->loop, loop->event);
} else {
pw_log_debug("%p cancel", loop); pw_log_debug("%p cancel", loop);
pthread_cancel(loop->thread); 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_log_debug("%p join", loop);
pw_thread_utils_join((struct spa_thread*)loop->thread, NULL); pw_thread_utils_join((struct spa_thread*)loop->thread, NULL);

View file

@ -29,11 +29,13 @@
PW_LOG_TOPIC_EXTERN(log_main_loop); PW_LOG_TOPIC_EXTERN(log_main_loop);
#define PW_LOG_TOPIC_DEFAULT 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); pw_log_debug("%p: do stop", this);
this->running = false; this->running = false;
return 0;
} }
static struct pw_main_loop *loop_new(struct pw_loop *loop, const struct spa_dict *props) 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->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); spa_hook_list_init(&this->listener_list);
return this; return this;
error_free_loop:
if (this->created && this->loop)
pw_loop_destroy(this->loop);
error_free: error_free:
free(this); free(this);
error_cleanup: error_cleanup:
@ -132,7 +125,7 @@ SPA_EXPORT
int pw_main_loop_quit(struct pw_main_loop *loop) int pw_main_loop_quit(struct pw_main_loop *loop)
{ {
pw_log_debug("%p: quit", 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 /** Start a main loop

View file

@ -472,9 +472,9 @@ struct pw_data_loop {
struct pw_loop *loop; struct pw_loop *loop;
struct spa_hook_list listener_list; struct spa_hook_list listener_list;
struct spa_source *event;
pthread_t thread; pthread_t thread;
unsigned int cancel:1;
unsigned int created:1; unsigned int created:1;
unsigned int running:1; unsigned int running:1;
}; };
@ -486,7 +486,6 @@ struct pw_main_loop {
struct pw_loop *loop; struct pw_loop *loop;
struct spa_hook_list listener_list; struct spa_hook_list listener_list;
struct spa_source *event;
unsigned int created:1; unsigned int created:1;
unsigned int running:1; unsigned int running:1;