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

@ -30,8 +30,9 @@
#include <pulse/util.h>
#include <pulse/thread-mainloop.h>
#include <pulsecore/macro.h>
#include <pulsecore/core-rtclock.h>
#include <pulsecore/macro.h>
#include <pulsecore/mutex.h>
static void tcb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
pa_assert_se(pa_threaded_mainloop_in_thread(userdata));
@ -40,6 +41,12 @@ static void tcb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv,
fprintf(stderr, "TIME EVENT END\n");
}
static void ocb(pa_threaded_mainloop *m, void *userdata) {
pa_threaded_mainloop_lock(m);
pa_threaded_mainloop_signal(m, 0);
pa_threaded_mainloop_unlock(m);
}
START_TEST (thread_mainloop_test) {
pa_mainloop_api *a;
pa_threaded_mainloop *m;
@ -69,6 +76,17 @@ START_TEST (thread_mainloop_test) {
fprintf(stderr, "waiting 5s (sleep)\n");
pa_msleep(5000);
/* Test pa_threaded_mainloop_once_unlocked() */
pa_threaded_mainloop_lock(m);
fprintf(stderr, "scheduling unlocked callback\n");
pa_threaded_mainloop_once_unlocked(m, ocb, NULL);
pa_threaded_mainloop_wait(m);
fprintf(stderr, "got unlocked callback\n");
pa_threaded_mainloop_unlock(m);
pa_threaded_mainloop_stop(m);
pa_threaded_mainloop_free(m);