[vlc-devel] [RFC PATCH 1/3] fifo: add block_FifoNewExt: create a fifo with custom lock and cond
Thomas Guillem
thomas at gllm.fr
Wed Apr 1 16:13:16 CEST 2015
---
include/vlc_block.h | 1 +
src/libvlccore.sym | 1 +
src/misc/fifo.c | 71 ++++++++++++++++++++++++++++++++++++-----------------
3 files changed, 50 insertions(+), 23 deletions(-)
diff --git a/include/vlc_block.h b/include/vlc_block.h
index e8589f6..af9bae2 100644
--- a/include/vlc_block.h
+++ b/include/vlc_block.h
@@ -309,6 +309,7 @@ static inline block_t *block_ChainGather( block_t *p_list )
****************************************************************************/
VLC_API block_fifo_t *block_FifoNew( void ) VLC_USED VLC_MALLOC;
+VLC_API block_fifo_t *block_FifoNewExt( vlc_mutex_t *, vlc_cond_t * ) VLC_USED VLC_MALLOC;
VLC_API void block_FifoRelease( block_fifo_t * );
VLC_API void block_FifoEmpty( block_fifo_t * );
VLC_API void block_FifoPut( block_fifo_t *, block_t * );
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index e89cf4e..079f04b 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -27,6 +27,7 @@ block_FifoCount
block_FifoEmpty
block_FifoGet
block_FifoNew
+block_FifoNewExt
block_FifoPut
block_FifoRelease
block_FifoShow
diff --git a/src/misc/fifo.c b/src/misc/fifo.c
index b6cc0ca..6ff9925 100644
--- a/src/misc/fifo.c
+++ b/src/misc/fifo.c
@@ -43,6 +43,8 @@ struct block_fifo_t
{
vlc_mutex_t lock; /* fifo data lock */
vlc_cond_t wait; /**< Wait for data */
+ vlc_mutex_t *p_lock;
+ vlc_cond_t *p_wait;
block_t *p_first;
block_t **pp_last;
@@ -63,7 +65,7 @@ struct block_fifo_t
*/
void vlc_fifo_Lock(vlc_fifo_t *fifo)
{
- vlc_mutex_lock(&fifo->lock);
+ vlc_mutex_lock(fifo->p_lock);
}
/**
@@ -75,7 +77,7 @@ void vlc_fifo_Lock(vlc_fifo_t *fifo)
*/
void vlc_fifo_Unlock(vlc_fifo_t *fifo)
{
- vlc_mutex_unlock(&fifo->lock);
+ vlc_mutex_unlock(fifo->p_lock);
}
/**
@@ -88,7 +90,7 @@ void vlc_fifo_Unlock(vlc_fifo_t *fifo)
*/
void vlc_fifo_Signal(vlc_fifo_t *fifo)
{
- vlc_cond_signal(&fifo->wait);
+ vlc_cond_signal(fifo->p_wait);
}
/**
@@ -102,12 +104,12 @@ void vlc_fifo_Signal(vlc_fifo_t *fifo)
*/
void vlc_fifo_Wait(vlc_fifo_t *fifo)
{
- vlc_fifo_WaitCond(fifo, &fifo->wait);
+ vlc_fifo_WaitCond(fifo, fifo->p_wait);
}
void vlc_fifo_WaitCond(vlc_fifo_t *fifo, vlc_cond_t *condvar)
{
- vlc_cond_wait(condvar, &fifo->lock);
+ vlc_cond_wait(condvar, fifo->p_lock);
}
/**
@@ -157,7 +159,7 @@ size_t vlc_fifo_GetBytes(const vlc_fifo_t *fifo)
*/
void vlc_fifo_QueueUnlocked(block_fifo_t *fifo, block_t *block)
{
- vlc_assert_locked(&fifo->lock);
+ vlc_assert_locked(fifo->p_lock);
assert(*(fifo->pp_last) == NULL);
*(fifo->pp_last) = block;
@@ -171,7 +173,7 @@ void vlc_fifo_QueueUnlocked(block_fifo_t *fifo, block_t *block)
block = block->p_next;
}
- vlc_cond_signal(&fifo->wait);
+ vlc_cond_signal(fifo->p_wait);
}
/**
@@ -186,7 +188,7 @@ void vlc_fifo_QueueUnlocked(block_fifo_t *fifo, block_t *block)
*/
block_t *vlc_fifo_DequeueUnlocked(block_fifo_t *fifo)
{
- vlc_assert_locked(&fifo->lock);
+ vlc_assert_locked(fifo->p_lock);
block_t *block = fifo->p_first;
@@ -220,7 +222,7 @@ block_t *vlc_fifo_DequeueUnlocked(block_fifo_t *fifo)
*/
block_t *vlc_fifo_DequeueAllUnlocked(block_fifo_t *fifo)
{
- vlc_assert_locked(&fifo->lock);
+ vlc_assert_locked(fifo->p_lock);
block_t *block = fifo->p_first;
@@ -232,20 +234,30 @@ block_t *vlc_fifo_DequeueAllUnlocked(block_fifo_t *fifo)
return block;
}
-
/**
- * Creates a thread-safe FIFO queue of blocks.
+ * Creates a thread-safe FIFO queue of blocks that will use the lock and the
+ * cond provided.
* See also block_FifoPut() and block_FifoGet().
* @return the FIFO or NULL on memory error
*/
-block_fifo_t *block_FifoNew( void )
+block_fifo_t *block_FifoNewExt( vlc_mutex_t *p_lock, vlc_cond_t *p_wait )
{
block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
if( !p_fifo )
return NULL;
- vlc_mutex_init( &p_fifo->lock );
- vlc_cond_init( &p_fifo->wait );
+ if( !p_lock && !p_wait )
+ {
+ vlc_mutex_init( &p_fifo->lock );
+ vlc_cond_init( &p_fifo->wait );
+ p_fifo->p_lock = &p_fifo->lock;
+ p_fifo->p_wait = &p_fifo->wait;
+ } else
+ {
+ p_fifo->p_lock = p_lock;
+ p_fifo->p_wait = p_wait;
+ }
+
p_fifo->p_first = NULL;
p_fifo->pp_last = &p_fifo->p_first;
p_fifo->i_depth = p_fifo->i_size = 0;
@@ -254,14 +266,27 @@ block_fifo_t *block_FifoNew( void )
}
/**
+ * Creates a thread-safe FIFO queue of blocks.
+ * See also block_FifoPut() and block_FifoGet().
+ * @return the FIFO or NULL on memory error
+ */
+block_fifo_t *block_FifoNew( void )
+{
+ return block_FifoNewExt( NULL, NULL );
+}
+
+/**
* Destroys a FIFO created by block_FifoNew().
* Any queued blocks are also destroyed.
*/
void block_FifoRelease( block_fifo_t *p_fifo )
{
block_ChainRelease( p_fifo->p_first );
- vlc_cond_destroy( &p_fifo->wait );
- vlc_mutex_destroy( &p_fifo->lock );
+ if( p_fifo->p_wait == &p_fifo->wait && p_fifo->p_lock == &p_fifo->lock )
+ {
+ vlc_cond_destroy( &p_fifo->wait );
+ vlc_mutex_destroy( &p_fifo->lock );
+ }
free( p_fifo );
}
@@ -332,11 +357,11 @@ block_t *block_FifoShow( block_fifo_t *p_fifo )
vlc_testcancel( );
- vlc_mutex_lock( &p_fifo->lock );
- mutex_cleanup_push( &p_fifo->lock );
+ vlc_mutex_lock( p_fifo->p_lock );
+ mutex_cleanup_push( p_fifo->p_lock );
while( p_fifo->p_first == NULL )
- vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
+ vlc_cond_wait( p_fifo->p_wait, p_fifo->p_lock );
b = p_fifo->p_first;
@@ -349,9 +374,9 @@ size_t block_FifoSize (block_fifo_t *fifo)
{
size_t size;
- vlc_mutex_lock (&fifo->lock);
+ vlc_mutex_lock (fifo->p_lock);
size = fifo->i_size;
- vlc_mutex_unlock (&fifo->lock);
+ vlc_mutex_unlock (fifo->p_lock);
return size;
}
@@ -360,8 +385,8 @@ size_t block_FifoCount (block_fifo_t *fifo)
{
size_t depth;
- vlc_mutex_lock (&fifo->lock);
+ vlc_mutex_lock (fifo->p_lock);
depth = fifo->i_depth;
- vlc_mutex_unlock (&fifo->lock);
+ vlc_mutex_unlock (fifo->p_lock);
return depth;
}
--
2.1.3
More information about the vlc-devel
mailing list