sscall

UDP based voice chat
git clone git://git.2f30.org/sscall
Log | Files | Refs | README | LICENSE

commit 1812ba7a3cd52c3d88f6cf9ee81412ab016f2aee
parent d7876a5a9c1cb0c98879589815b8d56aef783864
Author: sin <sin@2f30.org>
Date:   Tue,  5 Jun 2012 16:52:31 +0100

sscall: Sleep for at most 3 seconds in the output thread

We need to ensure that the thread will wake up in order
to notice the state change (specifically state->quit) in case
we send a SIGINT signal to the process.


Diffstat:
Msscall.c | 25++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/sscall.c b/sscall.c @@ -13,6 +13,8 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> +#include <sys/time.h> +#include <time.h> #include <ao/ao.h> #include <pthread.h> @@ -95,12 +97,29 @@ output_pcm(void *data) struct pcm_buf *pctx; struct list_head *iter, *q; struct output_pcm_state *state = data; + struct timespec ts; + struct timeval tp; + int rc; do { pthread_mutex_lock(&pcm_buf_lock); - if (list_empty(&pcm_buf.list)) - pthread_cond_wait(&tx_pcm_cond, - &pcm_buf_lock); + gettimeofday(&tp, NULL); + /* Convert from timeval to timespec */ + ts.tv_sec = tp.tv_sec; + ts.tv_nsec = tp.tv_usec * 1000; + /* Default to a 3 second wait internal */ + ts.tv_sec += 3; + + if (list_empty(&pcm_buf.list)) { + /* Wait at most 3 seconds to give some + * grace to perform cleanup if necessary */ + rc = pthread_cond_timedwait(&tx_pcm_cond, + &pcm_buf_lock, + &ts); + if (rc == ETIMEDOUT) + if (fverbose) + printf("Output thread is starving...\n"); + } pthread_mutex_lock(&output_pcm_state_lock); if (state->quit) {