OSDN Git Service

fix potential runaway buffer usage
authorjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Tue, 15 Dec 2009 01:28:55 +0000 (01:28 +0000)
committerjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Tue, 15 Dec 2009 01:28:55 +0000 (01:28 +0000)
pthread_cond_timedwait can wake early.  under certain system load conditions, this
happens often.  I was going ahead and adding buffers whenever it woke, regardless
of whether the condition had actually been met. so the fifo depth would
increase until memory ran out.

git-svn-id: svn://localhost/HandBrake/trunk@3030 b64f7644-9d1e-0410-96f1-a4d463321fa5

libhb/fifo.c
libhb/internal.h
libhb/reader.c
libhb/work.c

index 781c940..c72b4e1 100644 (file)
@@ -374,6 +374,21 @@ hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
     return b;
 }
 
+int hb_fifo_full_wait( hb_fifo_t * f )
+{
+    int result;
+
+    hb_lock( f->lock );
+    if( f->size >= f->capacity )
+    {
+        f->wait_full = 1;
+        hb_cond_timedwait( f->cond_full, f->lock, FIFO_TIMEOUT );
+    }
+    result = ( f->size < f->capacity );
+    hb_unlock( f->lock );
+    return result;
+}
+
 void hb_fifo_push_wait( hb_fifo_t * f, hb_buffer_t * b )
 {
     if( !b )
index 8085374..efe18fa 100644 (file)
@@ -96,6 +96,7 @@ hb_buffer_t * hb_fifo_see_wait( hb_fifo_t * );
 hb_buffer_t * hb_fifo_see2( hb_fifo_t * );
 void          hb_fifo_push( hb_fifo_t *, hb_buffer_t * );
 void          hb_fifo_push_wait( hb_fifo_t *, hb_buffer_t * );
+int           hb_fifo_full_wait( hb_fifo_t * f );
 void          hb_fifo_push_head( hb_fifo_t *, hb_buffer_t * );
 void          hb_fifo_close( hb_fifo_t ** );
 void          hb_fifo_flush( hb_fifo_t * f );
index 6b7d4cd..928d869 100644 (file)
@@ -69,7 +69,14 @@ hb_thread_t * hb_reader_init( hb_job_t * job )
 
 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
 {
-    hb_fifo_push_wait( fifo, buf );
+    while ( !*r->die )
+    {
+        if ( hb_fifo_full_wait( fifo ) )
+        {
+            hb_fifo_push( fifo, buf );
+            break;
+        }
+    }
 }
 
 static int is_audio( hb_reader_t *r, int id )
index 05bf6a1..7a923a1 100644 (file)
@@ -944,7 +944,14 @@ static void do_job( hb_job_t * job, int cpu_count )
         }
         if( buf_out )
         {
-            hb_fifo_push_wait( w->fifo_out, buf_out );
+            while ( !*job->die )
+            {
+                if ( hb_fifo_full_wait( w->fifo_out ) )
+                {
+                    hb_fifo_push( w->fifo_out, buf_out );
+                    break;
+                }
+            }
         }
     }
     hb_list_rem( job->list_work, w );
@@ -1160,7 +1167,14 @@ static void work_loop( void * _w )
         }
         if( buf_out )
         {
-            hb_fifo_push_wait( w->fifo_out, buf_out );
+            while ( !*w->done )
+            {
+                if ( hb_fifo_full_wait( w->fifo_out ) )
+                {
+                    hb_fifo_push( w->fifo_out, buf_out );
+                    break;
+                }
+            }
         }
     }
 }