#include <sys/stat.h>
#include "vlc_block.h"
-/*****************************************************************************
- * Block functions.
- *****************************************************************************/
-/* private */
+/**
+ * @section Block handling functions.
+ */
+
+/**
+ * Internal state for heap block.
+ */
struct block_sys_t
{
block_t self;
void block_Init( block_t *restrict b, void *buf, size_t size )
{
/* Fill all fields to their default */
- b->p_next = b->p_prev = NULL;
+ b->p_next = NULL;
b->i_flags = 0;
b->i_pts = b->i_dts = b->i_length = 0;
b->i_rate = 0;
return p_rea;
}
- /* We have a very large reserved footer now? Release some of it. */
- if ((p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
- (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE)
+ /* We have a very large reserved footer now? Release some of it.
+ * XXX it may not keep the algniment of p_buffer */
+ if( (p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
+ (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE )
{
- const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16;
- block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news);
+ const ptrdiff_t i_prebody = p_block->p_buffer - p_sys->p_allocated_buffer;
+ const size_t i_new = i_prebody + p_block->i_buffer + 1 * BLOCK_PADDING_SIZE;
+ block_sys_t *p_new = realloc( p_sys, sizeof (*p_sys) + i_new );
- if (newb != NULL)
+ if( p_new != NULL )
{
- p_sys = newb;
- p_sys->i_allocated_buffer = news;
+ p_sys = p_new;
+ p_sys->i_allocated_buffer = i_new;
p_block = &p_sys->self;
- p_block->p_buffer = p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
- + BLOCK_ALIGN
- - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN);
+ p_block->p_buffer = &p_sys->p_allocated_buffer[i_prebody];
}
}
return p_block;
return block;
}
-/*****************************************************************************
- * block_fifo_t management
- *****************************************************************************/
+/**
+ * @section Thread-safe block queue functions
+ */
+
+/**
+ * Internal state for block queues
+ */
struct block_fifo_t
{
vlc_mutex_t lock; /* fifo data lock */
- vlc_cond_t wait; /* fifo data conditional variable */
+ vlc_cond_t wait; /**< Wait for data */
+ vlc_cond_t wait_room; /**< Wait for queue depth to shrink */
block_t *p_first;
block_t **pp_last;
vlc_mutex_init( &p_fifo->lock );
vlc_cond_init( &p_fifo->wait );
+ vlc_cond_init( &p_fifo->wait_room );
p_fifo->p_first = NULL;
p_fifo->pp_last = &p_fifo->p_first;
p_fifo->i_depth = p_fifo->i_size = 0;
void block_FifoRelease( block_fifo_t *p_fifo )
{
block_FifoEmpty( p_fifo );
+ vlc_cond_destroy( &p_fifo->wait_room );
vlc_cond_destroy( &p_fifo->wait );
vlc_mutex_destroy( &p_fifo->lock );
free( p_fifo );
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 );
}
+/**
+ * Wait until the FIFO gets below a certain size (if needed).
+ *
+ * Note that if more than one thread writes to the FIFO, you cannot assume that
+ * the FIFO is actually below the requested size upon return (since another
+ * thread could have refilled it already). This is typically not an issue, as
+ * this function is meant for (relaxed) congestion control.
+ *
+ * This function may be a cancellation point and it is cancel-safe.
+ *
+ * @param fifo queue to wait on
+ * @param max_depth wait until the queue has no more than this many blocks
+ * (use SIZE_MAX to ignore this constraint)
+ * @param max_size wait until the queue has no more than this many bytes
+ * (use SIZE_MAX to ignore this constraint)
+ * @return nothing.
+ */
+void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
+{
+ vlc_testcancel ();
+
+ vlc_mutex_lock (&fifo->lock);
+ while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
+ {
+ mutex_cleanup_push (&fifo->lock);
+ vlc_cond_wait (&fifo->wait_room, &fifo->lock);
+ vlc_cleanup_pop ();
+ }
+ vlc_mutex_unlock (&fifo->lock);
+}
+
+/**
+ * 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)
+ */
size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
{
size_t i_size = 0;
vlc_mutex_lock( &p_fifo->lock );
- do
+ while (p_block != NULL)
{
i_size += p_block->i_buffer;
p_fifo->i_size += p_block->i_buffer;
p_block = p_block->p_next;
+ }
- } while( p_block );
-
- /* warn there is data in this fifo */
+ /* We queued one block: wake up one read-waiting thread */
vlc_cond_signal( &p_fifo->wait );
vlc_mutex_unlock( &p_fifo->lock );
{
block_t *b;
+ vlc_testcancel( );
+
vlc_mutex_lock( &p_fifo->lock );
mutex_cleanup_push( &p_fifo->lock );
p_fifo->pp_last = &p_fifo->p_first;
}
+ /* 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;
{
block_t *b;
+ vlc_testcancel( );
+
vlc_mutex_lock( &p_fifo->lock );
mutex_cleanup_push( &p_fifo->lock );
- if( p_fifo->p_first == NULL )
+ while( p_fifo->p_first == NULL )
vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
b = p_fifo->p_first;