thread-mainloop: Add API for running a callback unlocked

This adds API to allow clients to schedule a callback in the mainloop
thread without the mainloop lock being held. This is meant for a case
where the client might be dealing with locking its own objects in
addition to the mainloop thread itself. In this case, it might need ton
control the locking order of the two, to match the order in other
threads, as it might not always be able to allow for its objects to be
locked after the mainloop thread lock.
This commit is contained in:
Arun Raghavan 2019-03-12 12:08:08 +05:30 committed by Georg Chini
parent 824e685ac0
commit 363b1ae69c
4 changed files with 75 additions and 5 deletions

View file

@ -44,6 +44,7 @@
struct pa_threaded_mainloop {
pa_mainloop *real_mainloop;
volatile int n_waiting, n_waiting_for_accept;
pa_atomic_t in_once_unlocked;
pa_thread* thread;
pa_mutex* mutex;
@ -174,8 +175,8 @@ void pa_threaded_mainloop_stop(pa_threaded_mainloop *m) {
void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
pa_assert(m);
/* Make sure that this function is not called from the helper thread */
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
/* Make sure that this function is not called from the helper thread, unless it is unlocked */
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m) || pa_atomic_load(&m->in_once_unlocked));
pa_mutex_lock(m->mutex);
}
@ -183,8 +184,8 @@ void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
pa_assert(m);
/* Make sure that this function is not called from the helper thread */
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
/* Make sure that this function is not called from the helper thread, unless it is unlocked */
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m) || pa_atomic_load(&m->in_once_unlocked));
pa_mutex_unlock(m->mutex);
}
@ -258,3 +259,43 @@ void pa_threaded_mainloop_set_name(pa_threaded_mainloop *m, const char *name) {
if (m->thread)
pa_thread_set_name(m->thread, m->name);
}
typedef struct {
pa_threaded_mainloop *mainloop;
void (*callback)(pa_threaded_mainloop *m, void *userdata);
void *userdata;
} once_unlocked_data;
static void once_unlocked_cb(pa_mainloop_api *api, void *userdata) {
once_unlocked_data *data = userdata;
pa_assert(userdata);
pa_atomic_store(&data->mainloop->in_once_unlocked, 1);
pa_mutex_unlock(data->mainloop->mutex);
data->callback(data->mainloop, data->userdata);
pa_mutex_lock(data->mainloop->mutex);
pa_atomic_store(&data->mainloop->in_once_unlocked, 0);
}
void pa_threaded_mainloop_once_unlocked(pa_threaded_mainloop *m, void (*callback)(pa_threaded_mainloop *m, void *userdata),
void *userdata) {
pa_mainloop_api *api;
once_unlocked_data *data;
pa_assert(m);
pa_assert(callback);
/* Make sure that this function is not called from the helper thread */
pa_assert((m->thread && !pa_thread_is_running(m->thread)) || !in_worker(m));
api = pa_mainloop_get_api(m->real_mainloop);
data = pa_xnew0(once_unlocked_data, 1);
data->mainloop = m;
data->callback = callback;
data->userdata = userdata;
pa_mainloop_api_once(api, once_unlocked_cb, data);
}