From: Jean-Baptiste Kempf Date: Tue, 28 Sep 2021 19:47:40 +0000 (+0200) Subject: lookahead: Keep b_exit_thread under ifbuf.mutex X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=66a5bc1bd1563d8227d5d18440b525a09bcf17ca;p=libx264 lookahead: Keep b_exit_thread under ifbuf.mutex The lookahead_thread main loop checks b_exit_thread and exits if it is set. That flag is set by x264_lookahead_delete, which uses ifbuf.mutex to guard accessing it. However, the read in the while-loop condition of lookahead_thread is not guarded, and so TSAN sometimes reports a data race. --- diff --git a/encoder/lookahead.c b/encoder/lookahead.c index 18958bd0..93f59b19 100644 --- a/encoder/lookahead.c +++ b/encoder/lookahead.c @@ -89,9 +89,14 @@ static void lookahead_slicetype_decide( x264_t *h ) REALIGN_STACK static void *lookahead_thread( x264_t *h ) { - while( !h->lookahead->b_exit_thread ) + while( 1 ) { x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex ); + if( h->lookahead->b_exit_thread ) + { + x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex ); + break; + } x264_pthread_mutex_lock( &h->lookahead->next.mutex ); int shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size ); lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );