Fix clobbering of mutex/cvs
authorAnton Mitrofanov <Bugmaster@narod.ru>
Mon, 12 Mar 2012 06:08:18 +0000 (23:08 -0700)
committerFiona Glaser <fiona@x264.com>
Mon, 12 Mar 2012 06:10:23 +0000 (23:10 -0700)
Regression in r2183.
Bizarrely seemed to work on many platforms, but crashed on win64 and may have been slower.
Only affected sliced threads during encoding, but could cause crashes on x264 encoder close even without sliced threads.

encoder/encoder.c

index 03d05ae..88897ef 100644 (file)
@@ -1271,18 +1271,8 @@ x264_t *x264_encoder_open( x264_param_t *param )
         goto fail;
 
     h->thread[0] = h;
-    for( int i = 0; i < h->param.i_threads + !!h->param.i_sync_lookahead; i++ )
-    {
-        if( i )
-            CHECKED_MALLOC( h->thread[i], sizeof(x264_t) );
-        if( i < h->param.i_threads )
-        {
-            if( x264_pthread_mutex_init( &h->thread[i]->mutex, NULL ) )
-                goto fail;
-            if( x264_pthread_cond_init( &h->thread[i]->cv, NULL ) )
-                goto fail;
-        }
-    }
+    for( int i = 1; i < h->param.i_threads + !!h->param.i_sync_lookahead; i++ )
+        CHECKED_MALLOC( h->thread[i], sizeof(x264_t) );
 
     for( int i = 0; i < h->param.i_threads; i++ )
     {
@@ -1291,6 +1281,11 @@ x264_t *x264_encoder_open( x264_param_t *param )
         if( i > 0 )
             *h->thread[i] = *h;
 
+        if( x264_pthread_mutex_init( &h->thread[i]->mutex, NULL ) )
+            goto fail;
+        if( x264_pthread_cond_init( &h->thread[i]->cv, NULL ) )
+            goto fail;
+
         if( allocate_threadlocal_data )
         {
             h->thread[i]->fdec = x264_frame_pop_unused( h, 1 );