data-loop: add method to set custom thread-utils

So that the thread creation can be switched for this data loop.
This commit is contained in:
Wim Taymans 2022-03-29 10:49:37 +02:00
parent 90d755ab4c
commit 2180b6aa17
3 changed files with 30 additions and 2 deletions

View file

@ -197,10 +197,14 @@ SPA_EXPORT
int pw_data_loop_start(struct pw_data_loop *loop)
{
if (!loop->running) {
struct spa_thread_utils *utils;
struct spa_thread *thr;
loop->running = true;
thr = pw_thread_utils_create(NULL, do_loop, loop);
if ((utils = loop->thread_utils) == NULL)
utils = pw_thread_utils_get();
thr = spa_thread_utils_create(utils, NULL, do_loop, loop);
loop->thread = (pthread_t)thr;
if (thr == NULL) {
pw_log_error("%p: can't create thread: %m", loop);
@ -223,6 +227,7 @@ int pw_data_loop_stop(struct pw_data_loop *loop)
{
pw_log_debug("%p stopping", loop);
if (loop->running) {
struct spa_thread_utils *utils;
if (loop->cancel) {
pw_log_debug("%p cancel", loop);
pthread_cancel(loop->thread);
@ -231,7 +236,9 @@ int pw_data_loop_stop(struct pw_data_loop *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);
if ((utils = loop->thread_utils) == NULL)
utils = pw_thread_utils_get();
spa_thread_utils_join(utils, (struct spa_thread*)loop->thread, NULL);
pw_log_debug("%p joined", loop);
}
pw_log_debug("%p stopped", loop);
@ -273,3 +280,17 @@ int pw_data_loop_invoke(struct pw_data_loop *loop,
res = func(loop->loop->loop, false, seq, data, size, user_data);
return res;
}
/** Set a thread utils implementation.
* \param loop the data loop to set the thread utils on
* \param impl the thread utils implementation
*
* This configures a custom spa_thread_utils implementation for this data
* loop. Use NULL to restore the system default implementation.
*/
SPA_EXPORT
void pw_data_loop_set_thread_utils(struct pw_data_loop *loop,
struct spa_thread_utils *impl)
{
loop->thread_utils = impl;
}

View file

@ -30,6 +30,7 @@ extern "C" {
#endif
#include <spa/utils/hook.h>
#include <spa/support/thread.h>
/** \defgroup pw_data_loop Data Loop
*
@ -97,6 +98,10 @@ int pw_data_loop_invoke(struct pw_data_loop *loop,
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size,
bool block, void *user_data);
/** Set a custom spa_thread_utils for this loop. Setting NULL restores the
* system default implementation. Since 0.3.50 */
void pw_data_loop_set_thread_utils(struct pw_data_loop *loop,
struct spa_thread_utils *impl);
/**
* \}
*/

View file

@ -483,6 +483,8 @@ struct pw_data_loop {
struct spa_hook_list listener_list;
struct spa_thread_utils *thread_utils;
pthread_t thread;
unsigned int cancel:1;
unsigned int created:1;