Skip to content

Commit

Permalink
Add checks and doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
trengginas committed Oct 16, 2024
1 parent 42b8d75 commit 0672e06
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
22 changes: 12 additions & 10 deletions pjlib/include/pj/atomic_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@
/**
* @file atomic_queue.h
* @brief Atomic Queue operations
* @{
*
* Atomic queue for a single consumer and producer.
* This cyclic queue employs a ring buffer for storage, maintaining read/write
* pointers without locks. It’s designed for one producer and one consumer,
* as having multiple producers/consumers could lead to conflicts with the
* read/write pointers.
* The producer uses #pj_atomic_queue_put() to add an item to the back
* of the queue, while the consumer uses #pj_atomic_queue_get()
* to retrieve an item from the front.
*/

#include <pj/types.h>

PJ_BEGIN_DECL

/* Atomic queue (ring buffer) for single consumer & single producer.
*
* Producer invokes 'pj_atomic_queue_put(item)' to put an item to the back of
* the queue.
* Consumer invokes 'pj_atomic_queue_get(item)' to get an item from the head of
* the queue.
*/

/**
* Create a new Atomic Queue.
* Create a new Atomic Queue for single consumer and single producer case.
*
* @param pool The pool to allocate the atomic queue structure.
* @param max_item_cnt The maximum number of items that can be stored.
Expand All @@ -49,7 +51,7 @@ PJ_BEGIN_DECL
PJ_DECL(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool,
unsigned max_item_cnt,
unsigned item_size,
const char* name,
const char *name,
pj_atomic_queue_t **atomic_queue);

/**
Expand Down
8 changes: 5 additions & 3 deletions pjlib/src/pj/atomic_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ struct pj_atomic_queue_t
PJ_DEF(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool,
unsigned max_item_cnt,
unsigned item_size,
const char* name,
const char *name,
pj_atomic_queue_t **atomic_queue)
{
pj_atomic_queue_t *aqueue;

PJ_ASSERT_RETURN(pool, PJ_EINVAL);
aqueue = PJ_POOL_ZALLOC_T(pool, pj_atomic_queue_t);
aqueue->aQ = new AtomicQueue(max_item_cnt, item_size, name);
*atomic_queue = aqueue;
Expand All @@ -145,6 +146,7 @@ PJ_DEF(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool,

PJ_DEF(pj_status_t) pj_atomic_queue_destroy(pj_atomic_queue_t *atomic_queue)
{
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ, PJ_EINVAL);
delete atomic_queue->aQ;
atomic_queue->aQ = NULL;
return PJ_SUCCESS;
Expand All @@ -160,7 +162,7 @@ PJ_DEF(pj_status_t) pj_atomic_queue_destroy(pj_atomic_queue_t *atomic_queue)
PJ_DEF(pj_status_t) pj_atomic_queue_put(pj_atomic_queue_t *atomic_queue,
void *item)
{
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ, PJ_EINVAL);
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ && item, PJ_EINVAL);
atomic_queue->aQ->put(item);
return PJ_SUCCESS;
}
Expand All @@ -173,7 +175,7 @@ PJ_DEF(pj_status_t) pj_atomic_queue_put(pj_atomic_queue_t *atomic_queue,
PJ_DEF(pj_status_t) pj_atomic_queue_get(pj_atomic_queue_t *atomic_queue,
void *item)
{
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ, PJ_EINVAL);
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ && item, PJ_EINVAL);
if (atomic_queue->aQ->get(item))
return PJ_SUCCESS;
else
Expand Down

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 0672e06

Please sign in to comment.