diff --git a/src/pipewire/thread-loop.c b/src/pipewire/thread-loop.c index aa6368598..938f603d6 100644 --- a/src/pipewire/thread-loop.c +++ b/src/pipewire/thread-loop.c @@ -18,6 +18,7 @@ */ #include +#include #include "pipewire.h" #include "thread-loop.h" @@ -266,6 +267,30 @@ void pw_thread_loop_wait(struct pw_thread_loop *loop) loop->n_waiting--; } +/** Wait for the loop thread to call \ref pw_thread_loop_signal() + * or time out. + * + * \param loop a \ref pw_thread_loop to signal + * \param wait_max_sec the maximum number of seconds to wait for a \ref pw_thread_loop_signal() + * \return 0 on success or ETIMEDOUT on timeout + * + * \memberof pw_thread_loop + */ +int pw_thread_loop_timed_wait(struct pw_thread_loop *loop, int wait_max_sec) +{ + struct timeval now; + gettimeofday(&now, NULL); + struct timespec timeout; + timeout.tv_sec = now.tv_sec + wait_max_sec; + timeout.tv_nsec = now.tv_usec * 1000; + int ret = 0; + + loop->n_waiting++; + ret = pthread_cond_timedwait(&loop->cond, &loop->lock, &timeout); + loop->n_waiting--; + return ret; +} + /** Signal the loop thread waiting for accept with \ref pw_thread_loop_signal() * * \param loop a \ref pw_thread_loop to signal diff --git a/src/pipewire/thread-loop.h b/src/pipewire/thread-loop.h index 1412e7b27..be07286e0 100644 --- a/src/pipewire/thread-loop.h +++ b/src/pipewire/thread-loop.h @@ -129,6 +129,10 @@ void pw_thread_loop_unlock(struct pw_thread_loop *loop); /** Release the lock and wait until some thread calls \ref pw_thread_loop_signal */ void pw_thread_loop_wait(struct pw_thread_loop *loop); +/** Release the lock and wait a maximum of 'wait_max_sec' seconds + * until some thread calls \ref pw_thread_loop_signal or time out */ +int pw_thread_loop_timed_wait(struct pw_thread_loop *loop, int wait_max_sec); + /** Signal all threads waiting with \ref pw_thread_loop_wait */ void pw_thread_loop_signal(struct pw_thread_loop *loop, bool wait_for_accept);