[vlc-commits] block: rewrite block_Fifo(Empty, Get, Put) with the new functions

Rémi Denis-Courmont git at videolan.org
Thu Mar 19 18:56:14 CET 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Mar 17 19:33:31 2015 +0200| [757ce25689dc8241817763dd357a6f4105418326] | committer: Rémi Denis-Courmont

block: rewrite block_Fifo(Empty,Get,Put) with the new functions

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=757ce25689dc8241817763dd357a6f4105418326
---

 include/vlc_block.h |    2 +-
 src/misc/fifo.c     |  103 ++++++++++++---------------------------------------
 2 files changed, 25 insertions(+), 80 deletions(-)

diff --git a/include/vlc_block.h b/include/vlc_block.h
index b62fd69..7e2c656 100644
--- a/include/vlc_block.h
+++ b/include/vlc_block.h
@@ -313,7 +313,7 @@ VLC_API block_fifo_t *block_FifoNew( void ) VLC_USED VLC_MALLOC;
 VLC_API void block_FifoRelease( block_fifo_t * );
 VLC_API void block_FifoPace( block_fifo_t *fifo, size_t max_depth, size_t max_size );
 VLC_API void block_FifoEmpty( block_fifo_t * );
-VLC_API size_t block_FifoPut( block_fifo_t *, block_t * );
+VLC_API void block_FifoPut( block_fifo_t *, block_t * );
 VLC_API void block_FifoWake( block_fifo_t * );
 VLC_API block_t * block_FifoGet( block_fifo_t * ) VLC_USED;
 VLC_API block_t * block_FifoShow( block_fifo_t * );
diff --git a/src/misc/fifo.c b/src/misc/fifo.c
index 39b2bca..10749e9 100644
--- a/src/misc/fifo.c
+++ b/src/misc/fifo.c
@@ -276,29 +276,17 @@ void block_FifoRelease( block_fifo_t *p_fifo )
     free( p_fifo );
 }
 
-void block_FifoEmpty( block_fifo_t *p_fifo )
+/**
+ * Clears all blocks in a FIFO.
+ */
+void block_FifoEmpty(block_fifo_t *fifo)
 {
     block_t *block;
 
-    vlc_mutex_lock( &p_fifo->lock );
-    block = p_fifo->p_first;
-    if (block != NULL)
-    {
-        p_fifo->i_depth = p_fifo->i_size = 0;
-        p_fifo->p_first = NULL;
-        p_fifo->pp_last = &p_fifo->p_first;
-    }
-    vlc_cond_broadcast( &p_fifo->wait_room );
-    vlc_mutex_unlock( &p_fifo->lock );
-
-    while (block != NULL)
-    {
-        block_t *buf;
-
-        buf = block->p_next;
-        block_Release (block);
-        block = buf;
-    }
+    vlc_fifo_Lock(fifo);
+    block = vlc_fifo_DequeueAllUnlocked(fifo);
+    vlc_fifo_Unlock(fifo);
+    block_ChainRelease(block);
 }
 
 /**
@@ -336,33 +324,12 @@ void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
  * Immediately queue one block at the end of a FIFO.
  * @param fifo queue
  * @param block head of a block list to queue (may be NULL)
- * @return total number of bytes appended to the queue
  */
-size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
+void block_FifoPut(block_fifo_t *fifo, block_t *block)
 {
-    size_t i_size = 0, i_depth = 0;
-    block_t *p_last;
-
-    if (p_block == NULL)
-        return 0;
-    for (p_last = p_block; ; p_last = p_last->p_next)
-    {
-        i_size += p_last->i_buffer;
-        i_depth++;
-        if (!p_last->p_next)
-            break;
-    }
-
-    vlc_mutex_lock (&p_fifo->lock);
-    *p_fifo->pp_last = p_block;
-    p_fifo->pp_last = &p_last->p_next;
-    p_fifo->i_depth += i_depth;
-    p_fifo->i_size += i_size;
-    /* We queued at least one block: wake up one read-waiting thread */
-    vlc_cond_signal( &p_fifo->wait );
-    vlc_mutex_unlock( &p_fifo->lock );
-
-    return i_size;
+    vlc_fifo_Lock(fifo);
+    vlc_fifo_QueueUnlocked(fifo, block);
+    vlc_fifo_Unlock(fifo);
 }
 
 void block_FifoWake( block_fifo_t *p_fifo )
@@ -380,46 +347,24 @@ void block_FifoWake( block_fifo_t *p_fifo )
  *
  * @return a valid block, or NULL if block_FifoWake() was called.
  */
-block_t *block_FifoGet( block_fifo_t *p_fifo )
+block_t *block_FifoGet(block_fifo_t *fifo)
 {
-    block_t *b;
-
-    vlc_testcancel( );
-
-    vlc_mutex_lock( &p_fifo->lock );
-    mutex_cleanup_push( &p_fifo->lock );
-
-    /* Remember vlc_cond_wait() may cause spurious wakeups
-     * (on both Win32 and POSIX) */
-    while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
-        vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
-
-    vlc_cleanup_pop();
-    b = p_fifo->p_first;
-
-    p_fifo->b_force_wake = false;
-    if( b == NULL )
-    {
-        /* Forced wakeup */
-        vlc_mutex_unlock( &p_fifo->lock );
-        return NULL;
-    }
+    block_t *block;
 
-    p_fifo->p_first = b->p_next;
-    p_fifo->i_depth--;
-    p_fifo->i_size -= b->i_buffer;
+    vlc_testcancel();
 
-    if( p_fifo->p_first == NULL )
+    vlc_fifo_Lock(fifo);
+    while (vlc_fifo_IsEmpty(fifo) && !fifo->b_force_wake)
     {
-        p_fifo->pp_last = &p_fifo->p_first;
+        vlc_fifo_CleanupPush(fifo);
+        vlc_fifo_Wait(fifo);
+        vlc_cleanup_pop();
     }
+    fifo->b_force_wake = false;
+    block = vlc_fifo_DequeueUnlocked(fifo);
+    vlc_fifo_Unlock(fifo);
 
-    /* We don't know how many threads can queue new packets now. */
-    vlc_cond_broadcast( &p_fifo->wait_room );
-    vlc_mutex_unlock( &p_fifo->lock );
-
-    b->p_next = NULL;
-    return b;
+    return block;
 }
 
 /**



More information about the vlc-commits mailing list