From 45eb0d5c014707614710d39287c264e7428ad0dd Mon Sep 17 00:00:00 2001 From: Frediano Ziglio Date: Thu, 29 Sep 2016 18:54:52 +0100 Subject: Wait proper writer thread termination Signed-off-by: Frediano Ziglio --- tun.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tun.c b/tun.c index 0b91f18..6cfbce9 100644 --- a/tun.c +++ b/tun.c @@ -257,8 +257,14 @@ get_packet(void) unsigned n; pthread_mutex_lock(&pkt_buf_mtx); - while (buf_allocated() == 0) + while (!term && buf_allocated() == 0) { pthread_cond_wait(&pkt_cond_write, &pkt_buf_mtx); + } + if (term) { + pthread_mutex_unlock(&pkt_buf_mtx); + return NULL; + } + /* get the flow with the packet with minimum time to send */ min_flow = NULL; @@ -464,4 +470,13 @@ handle_tun(void) add_packet(flow, pkt); pkt = alloc_packet(); } + term = 1; + + /* wake up write if blocked to pick up the termination */ + pthread_mutex_lock(&pkt_buf_mtx); + pthread_cond_signal(&pkt_cond_write); + pthread_mutex_unlock(&pkt_buf_mtx); + + /* wait writer termination */ + pthread_join(writer, NULL); } -- cgit v1.2.3