mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	thread-loop: add timed wait
This commit is contained in:
		
							parent
							
								
									58667d6ced
								
							
						
					
					
						commit
						43a384c51e
					
				
					 2 changed files with 29 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -18,6 +18,7 @@
 | 
			
		|||
 */
 | 
			
		||||
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <sys/time.h>
 | 
			
		||||
 | 
			
		||||
#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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue