thread: make it possible to set a custom create function

Make a property to pass a custom function pointer to create threads
instead of pthread_create.

Use this in jack instead of bypassing the thread utils create function,
which gives the wrong thread rt priority with rtkit.

Fixes #4099
This commit is contained in:
Wim Taymans 2024-07-09 17:04:35 +02:00
parent 853a46120e
commit 4baa94fce2
3 changed files with 23 additions and 20 deletions

View file

@ -3182,31 +3182,27 @@ static struct spa_thread *impl_create(void *object,
void *(*start)(void*), void *arg)
{
struct client *c = (struct client *) object;
struct spa_thread *thr;
int res = 0;
struct spa_dict_item *items;
struct spa_dict copy;
char creator_ptr[64];
pw_log_info("create thread");
if (globals.creator != NULL) {
pthread_t pt;
pthread_attr_t *attr = NULL, attributes;
uint32_t i, n_items = props ? props->n_items : 0;
attr = pw_thread_fill_attr(props, &attributes);
items = alloca((n_items) + 1 * sizeof(*items));
res = -globals.creator(&pt, attr, start, arg);
if (attr)
pthread_attr_destroy(attr);
if (res != 0)
goto error;
thr = (struct spa_thread*)pt;
} else {
thr = spa_thread_utils_create(c->context.old_thread_utils, props, start, arg);
for (i = 0; i < n_items; i++)
items[i] = props->items[i];
snprintf(creator_ptr, sizeof(creator_ptr), "pointer:%p", globals.creator);
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_THREAD_CREATOR,
creator_ptr);
copy = SPA_DICT_INIT(items, n_items);
props = &copy;
}
return thr;
error:
pw_log_warn("create RT thread failed: %s", strerror(res));
errno = -res;
return NULL;
return spa_thread_utils_create(c->context.old_thread_utils, props, start, arg);
}
static int impl_join(void *object,

View file

@ -118,6 +118,7 @@ static inline int spa_thread_utils_drop_rt(struct spa_thread_utils *o,
#define SPA_KEY_THREAD_NAME "thread.name" /* the thread name */
#define SPA_KEY_THREAD_STACK_SIZE "thread.stack-size" /* the stack size of the thread */
#define SPA_KEY_THREAD_AFFINITY "thread.affinity" /* array of CPUs for this thread */
#define SPA_KEY_THREAD_CREATOR "thread.creator" /* platform specific thread creator function */
/**
* \}

View file

@ -90,10 +90,16 @@ static struct spa_thread *impl_create(void *object,
pthread_attr_t *attr = NULL, attributes;
const char *str;
int err;
int (*create_func)(pthread_t *, const pthread_attr_t *attr, void *(*start)(void*), void *) = NULL;
attr = pw_thread_fill_attr(props, &attributes);
err = pthread_create(&pt, attr, start, arg);
if (props == NULL ||
(str = spa_dict_lookup(props, SPA_KEY_THREAD_CREATOR)) == NULL ||
sscanf(str, "pointer:%p", &create_func) != 1)
create_func = pthread_create;
err = create_func(&pt, attr, start, arg);
if (attr)
pthread_attr_destroy(attr);