forked from 0xha0/datalink_repo
Initial version.
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* Description: A simple circular FIFO bounded queue
|
||||
*
|
||||
* Author: Fan Hao
|
||||
* Date: 2023-02-23 10:29
|
||||
* LastEditors: Fan Hao
|
||||
* LastEditTime: 2023-02-23 13:18
|
||||
* Revisions: Rev 0.0
|
||||
* History:
|
||||
* [2023-02-23]: Initial Version.
|
||||
*
|
||||
* Copyright(c) 2023 Beijing Institute of Technology.
|
||||
* Lab of Communication and Networking
|
||||
* NOTE: Forked from https://github.com/chrismerck/rpa_queue.git
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#include "circ_queue.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
// uncomment to print debug messages
|
||||
//#define QUEUE_DEBUG
|
||||
|
||||
struct CIRC_QUEUE_S {
|
||||
void **data;
|
||||
volatile uint32_t nelts; /**< # elements */
|
||||
uint32_t in; /**< next empty location */
|
||||
uint32_t out; /**< next filled location */
|
||||
uint32_t bounds;/**< max size of queue */
|
||||
uint32_t full_waiters;
|
||||
uint32_t empty_waiters;
|
||||
pthread_mutex_t *one_big_mutex;
|
||||
pthread_cond_t *not_empty;
|
||||
pthread_cond_t *not_full;
|
||||
};
|
||||
|
||||
#ifdef QUEUE_DEBUG
|
||||
static void Q_DBG(const char*msg, CIRC_QUEUE *q) {
|
||||
printf("#%d in %d out %d\t%s\n",
|
||||
q->nelts, q->in, q->out,
|
||||
msg
|
||||
);
|
||||
}
|
||||
#else
|
||||
#define Q_DBG(x,y)
|
||||
#endif
|
||||
|
||||
void CircQueueFree(CIRC_QUEUE * queue)
|
||||
{
|
||||
assert(queue != NULL);
|
||||
if (queue->data) free(queue->data);
|
||||
free(queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the CIRC_QUEUE.
|
||||
*/
|
||||
bool CircQueueCreate(CIRC_QUEUE **q, uint32_t queueCapacity)
|
||||
{
|
||||
assert(q != NULL);
|
||||
CIRC_QUEUE *queue;
|
||||
queue = malloc(sizeof(CIRC_QUEUE));
|
||||
if (!queue) {
|
||||
return false;
|
||||
}
|
||||
*q = queue;
|
||||
memset(queue, 0, sizeof(CIRC_QUEUE));
|
||||
|
||||
/* Set all the data in the queue to NULL */
|
||||
queue->data = malloc(queueCapacity * sizeof(void*));
|
||||
if(queue->data == NULL){
|
||||
goto error;
|
||||
}
|
||||
queue->bounds = queueCapacity;
|
||||
queue->nelts = 0;
|
||||
queue->in = 0;
|
||||
queue->out = 0;
|
||||
return true;
|
||||
error:
|
||||
free(queue);
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isCircQueueFull(CIRC_QUEUE *queue){
|
||||
assert(queue != NULL);
|
||||
return ((queue)->nelts == (queue)->bounds);
|
||||
}
|
||||
|
||||
inline bool isCircQueueEmpty(CIRC_QUEUE *queue){
|
||||
assert(queue != NULL);
|
||||
return ((queue)->nelts == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Push new data onto the queue.
|
||||
*/
|
||||
bool CircQueuePush(CIRC_QUEUE *queue, void *data)
|
||||
{
|
||||
assert(queue != NULL);
|
||||
|
||||
if (isCircQueueFull(queue)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
queue->data[queue->in] = data;
|
||||
queue->in++;
|
||||
if (queue->in >= queue->bounds) {
|
||||
queue->in -= queue->bounds;
|
||||
}
|
||||
queue->nelts++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current number of element in queue.
|
||||
*/
|
||||
inline uint32_t CircQueueSize(CIRC_QUEUE *queue) {
|
||||
return queue->nelts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the next item from the queue.
|
||||
*/
|
||||
bool CircQueuePop(CIRC_QUEUE *queue, void **data)
|
||||
{
|
||||
assert(queue != NULL);
|
||||
|
||||
if (isCircQueueEmpty(queue)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*data = queue->data[queue->out];
|
||||
queue->nelts--;
|
||||
|
||||
queue->out++;
|
||||
if (queue->out >= queue->bounds) {
|
||||
queue->out -= queue->bounds;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* Description: A simple circular FIFO bounded queue
|
||||
*
|
||||
* Author: Fan Hao
|
||||
* Date: 2023-02-23 10:29
|
||||
* LastEditors: Fan Hao
|
||||
* LastEditTime: 2023-02-23 12:53
|
||||
* Revisions: Rev 0.0
|
||||
* History:
|
||||
* [2023-02-23]: Initial Version.
|
||||
*
|
||||
* Copyright(c) 2023 Beijing Institute of Technology.
|
||||
* Lab of Communication and Networking
|
||||
* NOTE: Forked from https://github.com/chrismerck/rpa_queue.git
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef CIRC_QUEUE_H
|
||||
#define CIRC_QUEUE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
/**
|
||||
* opaque structure
|
||||
*/
|
||||
typedef struct CIRC_QUEUE_S CIRC_QUEUE;
|
||||
|
||||
/**
|
||||
* create a FIFO queue
|
||||
* @param queue The new queue
|
||||
* @param queue_capacity maximum size of the queue
|
||||
* @param a pool to allocate queue from
|
||||
*/
|
||||
bool CircQueueCreate(CIRC_QUEUE **queue, uint32_t queue_capacity);
|
||||
|
||||
/** free queue memory -- call after destroy
|
||||
* @note we do not free in destroy() to follow pthreads convention
|
||||
*/
|
||||
void CircQueueFree(CIRC_QUEUE * queue);
|
||||
|
||||
/**
|
||||
* Detects when the CIRC_QUEUE is full. This utility function is expected
|
||||
* to be called from within critical sections, and is not threadsafe.
|
||||
*/
|
||||
bool isCircQueueFull(CIRC_QUEUE *queue);
|
||||
|
||||
/**
|
||||
* Detects when the CIRC_QUEUE is empty. This utility function is expected
|
||||
* to be called from within critical sections, and is not threadsafe.
|
||||
*/
|
||||
bool isCircQueueEmpty(CIRC_QUEUE *queue);
|
||||
|
||||
/**
|
||||
* push/add an object to the queue, blocking if the queue is already full
|
||||
*
|
||||
* @param queue the queue
|
||||
* @param data the data
|
||||
* @returns RPA_EINTR the blocking was interrupted (try again)
|
||||
* @returns RPA_EOF the queue has been terminated
|
||||
* @returns RPA_SUCCESS on a successful push
|
||||
*/
|
||||
bool CircQueuePush(CIRC_QUEUE *queue, void *data);
|
||||
|
||||
|
||||
/**
|
||||
* pop/get an object from the queue, blocking if the queue is already empty
|
||||
*
|
||||
* @param queue the queue
|
||||
* @param data the data
|
||||
* @returns RPA_EINTR the blocking was interrupted (try again)
|
||||
* @returns RPA_EOF if the queue has been terminated
|
||||
* @returns RPA_SUCCESS on a successful pop
|
||||
*/
|
||||
bool CircQueuePop(CIRC_QUEUE *queue, void **data);
|
||||
|
||||
/**
|
||||
* returns the size of the queue.
|
||||
*
|
||||
* @warning this is not threadsafe, and is intended for reporting/monitoring
|
||||
* of the queue.
|
||||
* @param queue the queue
|
||||
* @returns the size of the queue
|
||||
*/
|
||||
uint32_t CircQueueSize(CIRC_QUEUE *queue);
|
||||
|
||||
#endif /* CIRC_QUEUE_H */
|
||||
Reference in New Issue
Block a user