Files
openairinterface5g/openair2/PHY_INTERFACE/queue.h
T
Michael Cook 6dd80ffd2f queue: add requeue, fix unqueue_matching
Synchronized with emane.git's implementation of this code which
includes a unit test.

This commit also fixes two bugs in unqueue_matching and one inb
unqueue:

- unqueue_matching was returning the wrong pointer when
  there was no match

- both unqueue_matching and unqueue were not clearing the vacated
  queue entry after removing the item
2020-10-20 09:04:23 -04:00

62 lines
1.5 KiB
C

/*
queue_t is a basic thread-safe non-blocking fixed-size queue.
put_queue returns false if the queue is full.
get_queue returns NULL if the queue is empty.
Example usage:
// Initialization:
queue_t fooq;
init_queue(&fooq);
// Producer:
foo_t *item = new_foo();
if (!put_queue(&fooq, item))
delete_foo(item);
// Consumer:
foo_t *item = get_queue(&fooq);
if (item)
{
do_something_with_foo(item);
delete_foo(item);
}
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <pthread.h>
#define MAX_QUEUE_SIZE 512
typedef struct queue_t
{
void *items[MAX_QUEUE_SIZE];
size_t read_index, write_index;
size_t num_items;
pthread_mutex_t mutex;
} queue_t;
void init_queue(queue_t *q);
bool put_queue(queue_t *q, void *item);
void *get_queue(queue_t *q);
/* Put the given item back onto this queue at the head.
(The next call to put_queue would return this item.)
Return true if successful, false if the queue was full */
bool requeue(queue_t *q, void *item);
/* Remove the last item queued.
Return the item or NULL if the queue was empty */
void *unqueue(queue_t *q);
typedef bool queue_matcher_t(void *wanted, void *candidate);
/* Unqueue the most recently queued item for watch `matcher(wanted, candidate)`
returns true where `candidate` is an item currently on the queue.
Returns the candidate item, or NULL if none matches */
void *unqueue_matching(queue_t *q, queue_matcher_t *matcher, void *wanted);