thread-loop: add timed wait

This commit is contained in:
Karin Hedlund 2018-02-20 10:26:51 +01:00 committed by Wim Taymans
parent 58667d6ced
commit 43a384c51e
2 changed files with 29 additions and 0 deletions

View file

@ -18,6 +18,7 @@
*/ */
#include <pthread.h> #include <pthread.h>
#include <sys/time.h>
#include "pipewire.h" #include "pipewire.h"
#include "thread-loop.h" #include "thread-loop.h"
@ -266,6 +267,30 @@ void pw_thread_loop_wait(struct pw_thread_loop *loop)
loop->n_waiting--; 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() /** Signal the loop thread waiting for accept with \ref pw_thread_loop_signal()
* *
* \param loop a \ref pw_thread_loop to signal * \param loop a \ref pw_thread_loop to signal

View file

@ -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 */ /** Release the lock and wait until some thread calls \ref pw_thread_loop_signal */
void pw_thread_loop_wait(struct pw_thread_loop *loop); 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 */ /** Signal all threads waiting with \ref pw_thread_loop_wait */
void pw_thread_loop_signal(struct pw_thread_loop *loop, bool wait_for_accept); void pw_thread_loop_signal(struct pw_thread_loop *loop, bool wait_for_accept);