Shaka Packager SDK
Public Member Functions | List of all members
shaka::media::ProducerConsumerQueue< T > Class Template Reference

#include <producer_consumer_queue.h>

Public Member Functions

 ProducerConsumerQueue (size_t capacity)
 
 ProducerConsumerQueue (size_t capacity, size_t starting_pos)
 
Status Push (const T &element, int64_t timeout_ms)
 
Status Pop (T *element, int64_t timeout_ms)
 
Status Peek (size_t pos, T *element, int64_t timeout_ms)
 
void Stop ()
 
bool Empty () const
 
size_t Size () const
 
size_t HeadPos () const
 
size_t TailPos () const
 
bool Stopped () const
 

Detailed Description

template<class T>
class shaka::media::ProducerConsumerQueue< T >

A thread safe producer consumer queue implementation. It allows the standard push and pop operations. It also maintains a monotonically-increasing element position and allows peeking at the element at certain position.

Definition at line 32 of file producer_consumer_queue.h.

Constructor & Destructor Documentation

◆ ProducerConsumerQueue() [1/2]

template<class T >
shaka::media::ProducerConsumerQueue< T >::ProducerConsumerQueue ( size_t  capacity)
explicit

Create a ProducerConsumerQueue starting from position 0.

Parameters
capacityis the maximum number of elements that the queue can hold at once. A value of zero means unlimited capacity.

Definition at line 145 of file producer_consumer_queue.h.

◆ ProducerConsumerQueue() [2/2]

template<class T >
shaka::media::ProducerConsumerQueue< T >::ProducerConsumerQueue ( size_t  capacity,
size_t  starting_pos 
)

Create a ProducerConsumerQueue starting from indicated position.

Parameters
capacityis the maximum number of elements that the queue can hold at once. A value of zero means unlimited capacity.
starting_posis the starting head position.

Definition at line 151 of file producer_consumer_queue.h.

Member Function Documentation

◆ Empty()

template<class T >
bool shaka::media::ProducerConsumerQueue< T >::Empty ( ) const
inline
Returns
true if there are no elements in the queue.

Definition at line 92 of file producer_consumer_queue.h.

◆ HeadPos()

template<class T >
size_t shaka::media::ProducerConsumerQueue< T >::HeadPos ( ) const
inline
Returns
The position of the head element in the queue. Note that the returned value may be meaningless if the queue is empty.

Definition at line 105 of file producer_consumer_queue.h.

◆ Peek()

template<class T >
Status shaka::media::ProducerConsumerQueue< T >::Peek ( size_t  pos,
T *  element,
int64_t  timeout_ms 
)

Peek at the element at the specified position from the queue. If the element is not available yet, block until it to be available or time out or stopped. NOTE: Elements before (pos - capacity/2) will be removed from the queue after Peek operation.

Parameters
posrefers to the element position.
[out]elementreceives the peeked element.
timeout_msindicates timeout in milliseconds. A value of zero means return immediately. A negative value means waiting indefinitely.
Returns
STOPPED if Stop has been called and pos is out of range, INVALID_ARGUMENT if the pos < Head(), TIME_OUT if times out, OK otherwise.

Definition at line 255 of file producer_consumer_queue.h.

◆ Pop()

template<class T >
Status shaka::media::ProducerConsumerQueue< T >::Pop ( T *  element,
int64_t  timeout_ms 
)

Pop an element from the front of the queue. If the queue is empty, block for an element to be available to be consumed or time out or stopped.

Parameters
[out]elementreceives the popped element.
timeout_msindicates timeout in milliseconds. A value of zero means return immediately. A negative value means waiting indefinitely.
Returns
STOPPED if Stop has been called and the queue is completely empty, TIME_OUT if times out, OK otherwise.

Definition at line 212 of file producer_consumer_queue.h.

◆ Push()

template<class T >
Status shaka::media::ProducerConsumerQueue< T >::Push ( const T &  element,
int64_t  timeout_ms 
)

Push an element to the back of the queue. If the queue has reached its capacity limit, block until spare capacity is available or time out or stopped.

Parameters
elementrefers the element to be pushed.
timeout_msindicates timeout in milliseconds. A value of zero means return immediately. A negative value means waiting indefinitely.
Returns
OK if the element was pushed successfully, STOPPED if Stop has has been called, TIME_OUT if times out.

Definition at line 162 of file producer_consumer_queue.h.

◆ Size()

template<class T >
size_t shaka::media::ProducerConsumerQueue< T >::Size ( ) const
inline
Returns
The number of elements in the queue.

Definition at line 98 of file producer_consumer_queue.h.

◆ Stop()

template<class T >
void shaka::media::ProducerConsumerQueue< T >::Stop ( )
inline

Terminate Pop and Peek requests once the queue drains entirely. Also terminate all waiting and future Push requests immediately. Stop cannot stall.

Definition at line 83 of file producer_consumer_queue.h.

◆ Stopped()

template<class T >
bool shaka::media::ProducerConsumerQueue< T >::Stopped ( ) const
inline
Returns
true if the queue has been stopped using Stop(). This allows producers to check if they can add new elements to the queue.

Definition at line 119 of file producer_consumer_queue.h.

◆ TailPos()

template<class T >
size_t shaka::media::ProducerConsumerQueue< T >::TailPos ( ) const
inline
Returns
The position of the tail element in the queue. Note that the returned value may be meaningless if the queue is empty.

Definition at line 112 of file producer_consumer_queue.h.


The documentation for this class was generated from the following file: