discoLight
Loading...
Searching...
No Matches
SWE4211RPi::CommandQueue Class Reference

Public Member Functions

 CommandQueue ()
 
virtual ~CommandQueue ()
 
bool hasItem ()
 
CommandQueueEntry dequeue ()
 
void enqueue (CommandQueueEntry value)
 

Private Attributes

std::queue< CommandQueueEntry > commandQueueContents
 
sem_t queueCountSemaphore
 
std::mutex queueMutex
 

Constructor & Destructor Documentation

◆ CommandQueue()

CommandQueue::CommandQueue ( )

This is the default constructor, which creates an instance of the queue.

This is the default constructor, which creates an empty instance of the queue.

  1. Initialize the counting semaphore. Its count must always equal the number of items on the queue that are available to be dequeued. Because a newly created queue is empty, choose an initial value that indicates no items have been enqueued. The semaphore is shared only among the threads of this process, not between separate processes.

◆ ~CommandQueue()

CommandQueue::~CommandQueue ( )
virtual

The virtual destructor, which deallocates any allocated resources.

The destructor, which deallocates any allocated resources.

  1. Release the counting semaphore that was initialized in the constructor. Every semaphore that is initialized should be destroyed once it is no longer needed.

Member Function Documentation

◆ dequeue()

CommandQueueEntry CommandQueue::dequeue ( )

This method will dequeue the next command from the queue. This method will block if there are no items on the queue.

Returns
The return will be the next command that is to be processed.

This method removes and returns the next command from the front of the queue. If the queue is empty, the calling thread blocks until another thread enqueues a command.

Returns
The next command that is to be processed.
  1. Create a local CommandQueueEntry to hold the value that will be returned, and initialize all of its fields to zero.

  2. Block on the counting semaphore. If no items are on the queue, this blocks the calling thread until an item is enqueued.

  3. Lock the mutex so that no other thread can modify the queue while this thread is accessing it.

  4. Copy the item at the front of the queue into the return value, and then remove that item from the queue.

  5. Return the copied item.

Here is the caller graph for this function:

◆ enqueue()

void CommandQueue::enqueue ( CommandQueueEntry  value)

This method will enqueue a command on the queue. Any number can be enqueued as a command. Enqueueing a command will cause a thread blocked waiting for a command to be unblocked.

Parameters
commandThis is the command that is to be enqueued.

This method will enqueue a command on the queue. Enqueueing a command will cause a thread blocked waiting for a command to be unblocked.

Parameters
valueThis is the value that is to be enqueued.
  1. Lock the queue.

  2. Place the given item on the end of the queue.

  3. Indicate that something has been enqueued through the semaphore.

  4. Handle any other clean up you need to do.

Here is the caller graph for this function:

◆ hasItem()

bool CommandQueue::hasItem ( )

This method will indicate whether or not the queue has an item that is ready to be dequeued.

Returns
true if there is an item on the queue. False otherwise.

This method indicates whether the queue currently contains at least one item that is ready to be dequeued. It does not block and does not remove anything from the queue. The result is only a snapshot: another thread may change the queue immediately afterwards.

Returns
true if there is at least one item on the queue. False otherwise.
  1. Lock the mutex before examining the queue. Even reading the queue is unsafe while another thread may be modifying it. As in the other methods, use a scoped lock.
  2. Determine whether the queue contains one or more items and return the result. Use the queue's own state to make this determination, not the value of the semaphore.
Here is the caller graph for this function:

Member Data Documentation

◆ commandQueueContents

std::queue<CommandQueueEntry> SWE4211RPi::CommandQueue::commandQueueContents
private

This attribute holds the contents of the queue. Items are processed in FIFO order. The internal parameters of this attribute should be used to determine if the queue is empty or not.

◆ queueCountSemaphore

sem_t SWE4211RPi::CommandQueue::queueCountSemaphore
private

This is a counting semaphore which keeps track of how many items are on the queue. It should only be used as a pure semaphore.

◆ queueMutex

std::mutex SWE4211RPi::CommandQueue::queueMutex
private

This is a mutex for the class which is used to lock critical sections in different methods as is applicable.


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