You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
63 lines
1.6 KiB
#ifndef _QUEUE_h
|
|
#define _QUEUE_h
|
|
|
|
#include "watchos_types.h"
|
|
|
|
#define QUEUE_RING_SIZE 8
|
|
|
|
/// <summary>
|
|
/// Simple FIFO ring buffer implementation for kernel handles
|
|
/// </summary>
|
|
class Queue
|
|
{
|
|
private:
|
|
/// <summary>
|
|
/// Buffered data
|
|
/// </summary>
|
|
kernel_handle_t m_item[QUEUE_RING_SIZE];
|
|
/// <summary>
|
|
/// Buffer offset to read the next item from
|
|
/// </summary>
|
|
int m_read = 0;
|
|
/// <summary>
|
|
/// Buffer offset to write the next item to
|
|
/// </summary>
|
|
int m_write = 0;
|
|
/// <summary>
|
|
/// Set if the buffer is full
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// When m_read == m_write, the buffer is either full or empty. This allows us to differentiate.
|
|
/// </remarks>
|
|
bool m_full = false;
|
|
/// <summary>
|
|
/// Get the next buffer index, implementing wrapping
|
|
/// </summary>
|
|
/// <param name="n">current buffer index</param>
|
|
/// <returns>the buffer index after the passed buffer index</returns>
|
|
int next(int n);
|
|
public:
|
|
Queue();
|
|
/// <summary>
|
|
/// Push a handle into the buffer
|
|
/// </summary>
|
|
/// <param name="item">handle to add to the buffer</param>
|
|
void push(kernel_handle_t item);
|
|
/// <summary>
|
|
/// Remove and return the next item in the buffer
|
|
/// </summary>
|
|
/// <returns>next handle in the buffer</returns>
|
|
kernel_handle_t pop();
|
|
/// <summary>
|
|
/// Return the next item in the buffer without removing it
|
|
/// </summary>
|
|
/// <returns>next handle in the buffer</returns>
|
|
kernel_handle_t peek();
|
|
/// <summary>
|
|
/// Check whether there are any items in the buffer
|
|
/// </summary>
|
|
/// <returns>true if no items in buffer, false if any items in buffer</returns>
|
|
bool isEmpty();
|
|
};
|
|
|
|
#endif
|