thread: deprecate pw_thread_utils_set()

Setting a global thread-utils is not a good idea, especially
when multiple contexts will register their own interface.

Instead, set the thread-utils as a context object and use this to
configure the data loop in the context.

In JACK we need a per context implementation of the interface so that
we can find the context specific thread-utils.

See #2252
This commit is contained in:
Wim Taymans 2022-03-30 20:31:42 +02:00
parent f3466f8cd6
commit f0424c0b99
6 changed files with 59 additions and 33 deletions

View file

@ -261,6 +261,8 @@ struct context {
struct pw_thread_loop *loop; /* thread_lock protects all below */ struct pw_thread_loop *loop; /* thread_lock protects all below */
struct pw_context *context; struct pw_context *context;
struct spa_thread_utils *old_thread_utils;
struct spa_thread_utils thread_utils;
pthread_mutex_t lock; /* protects map and lists below, in addition to thread_lock */ pthread_mutex_t lock; /* protects map and lists below, in addition to thread_lock */
struct spa_list objects; struct spa_list objects;
uint32_t free_count; uint32_t free_count;
@ -2499,6 +2501,7 @@ static struct spa_thread *impl_create(void *data,
const struct spa_dict *props, const struct spa_dict *props,
void *(*start)(void*), void *arg) void *(*start)(void*), void *arg)
{ {
struct client *c = (struct client *) data;
struct spa_thread *thr; struct spa_thread *thr;
int res = 0; int res = 0;
@ -2520,7 +2523,7 @@ static struct spa_thread *impl_create(void *data,
goto error; goto error;
thr = (struct spa_thread*)pt; thr = (struct spa_thread*)pt;
} else { } else {
thr = pw_thread_utils_create(NULL, start, arg); thr = spa_thread_utils_create(c->context.old_thread_utils, NULL, start, arg);
} }
return thr; return thr;
error: error:
@ -2530,31 +2533,25 @@ error:
} }
static int impl_join(void *d, static int impl_join(void *data,
struct spa_thread *thread, void **retval) struct spa_thread *thread, void **retval)
{ {
struct client *c = (struct client *) data;
pw_log_info("join thread"); pw_log_info("join thread");
return pw_thread_utils_join(thread, retval); return spa_thread_utils_join(c->context.old_thread_utils, thread, retval);
} }
static int impl_acquire_rt(void *data, struct spa_thread *thread, int priority) static int impl_acquire_rt(void *data, struct spa_thread *thread, int priority)
{ {
return pw_thread_utils_acquire_rt(thread, priority); struct client *c = (struct client *) data;
return spa_thread_utils_acquire_rt(c->context.old_thread_utils, thread, priority);
} }
static struct { static struct spa_thread_utils_methods thread_utils_impl = {
struct spa_thread_utils utils; SPA_VERSION_THREAD_UTILS_METHODS,
struct spa_thread_utils_methods methods;
} thread_utils_impl = {
{ { SPA_TYPE_INTERFACE_ThreadUtils,
SPA_VERSION_THREAD_UTILS,
SPA_CALLBACKS_INIT(&thread_utils_impl.methods,
&thread_utils_impl) } },
{ SPA_VERSION_THREAD_UTILS_METHODS,
.create = impl_create, .create = impl_create,
.join = impl_join, .join = impl_join,
.acquire_rt = impl_acquire_rt, .acquire_rt = impl_acquire_rt,
}
}; };
static jack_port_type_id_t string_to_type(const char *port_type) static jack_port_type_id_t string_to_type(const char *port_type)
@ -3276,10 +3273,23 @@ jack_client_t * jack_client_open (const char *client_name,
mix2 = mix2_sse; mix2 = mix2_sse;
#endif #endif
} }
client->loop = client->context.context->data_loop_impl; client->context.old_thread_utils =
pw_context_get_object(client->context.context,
SPA_TYPE_INTERFACE_ThreadUtils);
if (client->context.old_thread_utils == NULL)
client->context.old_thread_utils = pw_thread_utils_get();
client->context.thread_utils.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_ThreadUtils,
SPA_VERSION_THREAD_UTILS,
&thread_utils_impl, client);
client->loop = client->context.context->data_loop_impl;
pw_data_loop_stop(client->loop); pw_data_loop_stop(client->loop);
pw_data_loop_set_thread_utils(client->loop, &thread_utils_impl.utils);
pw_context_set_object(client->context.context,
SPA_TYPE_INTERFACE_ThreadUtils,
&client->context.thread_utils);
spa_list_init(&client->links); spa_list_init(&client->links);
spa_list_init(&client->rt.target_links); spa_list_init(&client->rt.target_links);
@ -6040,7 +6050,7 @@ int jack_client_max_real_time_priority (jack_client_t *client)
spa_return_val_if_fail(c != NULL, -1); spa_return_val_if_fail(c != NULL, -1);
pw_thread_utils_get_rt_range(NULL, &min, &max); spa_thread_utils_get_rt_range(&c->context.thread_utils, NULL, &min, &max);
return SPA_MIN(max, c->rt_max) - 1; return SPA_MIN(max, c->rt_max) - 1;
} }
@ -6082,6 +6092,7 @@ int jack_client_create_thread (jack_client_t* client,
void *(*start_routine)(void*), void *(*start_routine)(void*),
void *arg) void *arg)
{ {
struct client *c = (struct client *) client;
int res = 0; int res = 0;
struct spa_thread *thr; struct spa_thread *thr;
@ -6091,7 +6102,7 @@ int jack_client_create_thread (jack_client_t* client,
pw_log_info("client %p: create thread rt:%d prio:%d", client, realtime, priority); pw_log_info("client %p: create thread rt:%d prio:%d", client, realtime, priority);
thr = spa_thread_utils_create(&thread_utils_impl.utils, NULL, start_routine, arg); thr = spa_thread_utils_create(&c->context.thread_utils, NULL, start_routine, arg);
if (thr == NULL) if (thr == NULL)
res = -errno; res = -errno;
*thread = (pthread_t)thr; *thread = (pthread_t)thr;
@ -6110,13 +6121,16 @@ int jack_client_create_thread (jack_client_t* client,
SPA_EXPORT SPA_EXPORT
int jack_client_stop_thread(jack_client_t* client, jack_native_thread_t thread) int jack_client_stop_thread(jack_client_t* client, jack_native_thread_t thread)
{ {
struct client *c = (struct client *) client;
void* status; void* status;
if (thread == (jack_native_thread_t)NULL) if (thread == (jack_native_thread_t)NULL)
return -EINVAL; return -EINVAL;
spa_return_val_if_fail(client != NULL, -EINVAL);
pw_log_debug("join thread %lu", thread); pw_log_debug("join thread %lu", thread);
pw_thread_utils_join((struct spa_thread*)thread, &status); spa_thread_utils_join(&c->context.thread_utils, (struct spa_thread*)thread, &status);
pw_log_debug("stopped thread %lu", thread); pw_log_debug("stopped thread %lu", thread);
return 0; return 0;
} }
@ -6124,15 +6138,18 @@ int jack_client_stop_thread(jack_client_t* client, jack_native_thread_t thread)
SPA_EXPORT SPA_EXPORT
int jack_client_kill_thread(jack_client_t* client, jack_native_thread_t thread) int jack_client_kill_thread(jack_client_t* client, jack_native_thread_t thread)
{ {
struct client *c = (struct client *) client;
void* status; void* status;
if (thread == (jack_native_thread_t)NULL) if (thread == (jack_native_thread_t)NULL)
return -EINVAL; return -EINVAL;
spa_return_val_if_fail(client != NULL, -EINVAL);
pw_log_debug("cancel thread %lu", thread); pw_log_debug("cancel thread %lu", thread);
pthread_cancel(thread); pthread_cancel(thread);
pw_log_debug("join thread %lu", thread); pw_log_debug("join thread %lu", thread);
pw_thread_utils_join((struct spa_thread*)thread, &status); spa_thread_utils_join(&c->context.thread_utils, (struct spa_thread*)thread, &status);
pw_log_debug("stopped thread %lu", thread); pw_log_debug("stopped thread %lu", thread);
return 0; return 0;
} }

View file

@ -464,8 +464,7 @@ finish:
static void module_destroy(void *data) static void module_destroy(void *data)
{ {
struct impl *impl = data; struct impl *impl = data;
pw_context_set_object(impl->context, SPA_TYPE_INTERFACE_ThreadUtils, NULL);
pw_thread_utils_set(NULL);
spa_hook_remove(&impl->module_listener); spa_hook_remove(&impl->module_listener);
#ifdef HAVE_DBUS #ifdef HAVE_DBUS
@ -931,7 +930,9 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
SPA_TYPE_INTERFACE_ThreadUtils, SPA_TYPE_INTERFACE_ThreadUtils,
SPA_VERSION_THREAD_UTILS, SPA_VERSION_THREAD_UTILS,
&impl_thread_utils, impl); &impl_thread_utils, impl);
pw_thread_utils_set(&impl->thread_utils);
pw_context_set_object(context, SPA_TYPE_INTERFACE_ThreadUtils,
&impl->thread_utils);
pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl); pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl);

View file

@ -124,18 +124,20 @@ static int try_load_conf(struct pw_context *this, const char *conf_prefix,
static int context_set_freewheel(struct pw_context *context, bool freewheel) static int context_set_freewheel(struct pw_context *context, bool freewheel)
{ {
struct spa_thread *thr; struct spa_thread *thr;
int res; int res = 0;
if ((thr = pw_data_loop_get_thread(context->data_loop_impl)) == NULL) if ((thr = pw_data_loop_get_thread(context->data_loop_impl)) == NULL)
return -EIO; return -EIO;
if (freewheel) { if (freewheel) {
pw_log_info("%p: enter freewheel", context); pw_log_info("%p: enter freewheel", context);
res = pw_thread_utils_drop_rt(thr); if (context->thread_utils)
res = spa_thread_utils_drop_rt(context->thread_utils, thr);
} else { } else {
pw_log_info("%p: exit freewheel", context); pw_log_info("%p: exit freewheel", context);
// Use the priority as configured within the realtime module /* Use the priority as configured within the realtime module */
res = pw_thread_utils_acquire_rt(thr, -1); if (context->thread_utils)
res = spa_thread_utils_acquire_rt(context->thread_utils, thr, -1);
} }
if (res < 0) if (res < 0)
pw_log_info("%p: freewheel error:%s", context, spa_strerror(res)); pw_log_info("%p: freewheel error:%s", context, spa_strerror(res));
@ -1455,6 +1457,12 @@ int pw_context_set_object(struct pw_context *context, const char *type, void *va
} }
entry->value = value; entry->value = value;
} }
if (spa_streq(type, SPA_TYPE_INTERFACE_ThreadUtils)) {
context->thread_utils = value;
if (context->data_loop_impl)
pw_data_loop_set_thread_utils(context->data_loop_impl,
context->thread_utils);
}
return 0; return 0;
} }

View file

@ -455,6 +455,7 @@ struct pw_context {
struct spa_hook_list driver_listener_list; struct spa_hook_list driver_listener_list;
struct spa_hook_list listener_list; struct spa_hook_list listener_list;
struct spa_thread_utils *thread_utils;
struct pw_loop *main_loop; /**< main loop for control */ struct pw_loop *main_loop; /**< main loop for control */
struct pw_loop *data_loop; /**< data loop for data passing */ struct pw_loop *data_loop; /**< data loop for data passing */
struct pw_data_loop *data_loop_impl; struct pw_data_loop *data_loop_impl;

View file

@ -83,9 +83,7 @@ static struct spa_thread_utils *global_impl = &default_impl.utils;
SPA_EXPORT SPA_EXPORT
void pw_thread_utils_set(struct spa_thread_utils *impl) void pw_thread_utils_set(struct spa_thread_utils *impl)
{ {
if (impl == NULL) pw_log_warn("pw_thread_utils_set is deprecated and does nothing anymore");
impl = &default_impl.utils;
global_impl = impl;
} }
SPA_EXPORT SPA_EXPORT

View file

@ -44,6 +44,7 @@ extern "C" {
* \{ * \{
*/ */
SPA_DEPRECATED
void pw_thread_utils_set(struct spa_thread_utils *impl); void pw_thread_utils_set(struct spa_thread_utils *impl);
struct spa_thread_utils *pw_thread_utils_get(void); struct spa_thread_utils *pw_thread_utils_get(void);