| 1 | /* |
| 2 | * Copyright (C) 2024 Teluu Inc. (http://www.teluu.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #ifndef __PJ_ATOMIC_QUEUE_H__ |
| 19 | #define __PJ_ATOMIC_QUEUE_H__ |
| 20 | |
| 21 | /** |
| 22 | * @file atomic_queue.h |
| 23 | * @brief Atomic Queue operations |
| 24 | * @{ |
| 25 | * |
| 26 | * Atomic queue for a single consumer and producer. |
| 27 | * This cyclic queue employs a ring buffer for storage, maintaining read/write |
| 28 | * pointers without locks. It’s designed for one producer and one consumer, |
| 29 | * as having multiple producers/consumers could lead to conflicts with the |
| 30 | * read/write pointers. |
| 31 | * The producer uses #pj_atomic_queue_put() to add an item to the back |
| 32 | * of the queue, while the consumer uses #pj_atomic_queue_get() |
| 33 | * to retrieve an item from the front. |
| 34 | */ |
| 35 | |
| 36 | #include <pj/types.h> |
| 37 | |
| 38 | PJ_BEGIN_DECL |
| 39 | |
| 40 | /** |
| 41 | * Create a new Atomic Queue for single consumer and single producer case. |
| 42 | * |
| 43 | * @param pool The pool to allocate the atomic queue structure. |
| 44 | * @param max_item_cnt The maximum number of items that can be stored. |
| 45 | * @param item_size The size of each item. |
| 46 | * @param name The name of the queue. |
| 47 | * @param atomic_queue Pointer to hold the newly created Atomic Queue. |
| 48 | * |
| 49 | * @return PJ_SUCCESS on success. |
| 50 | */ |
| 51 | PJ_DECL(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool, |
| 52 | unsigned max_item_cnt, |
| 53 | unsigned item_size, |
| 54 | const char *name, |
| 55 | pj_atomic_queue_t **atomic_queue); |
| 56 | |
| 57 | /** |
| 58 | * Destroy the Atomic Queue. |
| 59 | * |
| 60 | * @param atomic_queue The Atomic Queue to be destroyed. |
| 61 | * |
| 62 | * @return PJ_SUCCESS if success. |
| 63 | */ |
| 64 | PJ_DECL(pj_status_t) pj_atomic_queue_destroy(pj_atomic_queue_t *atomic_queue); |
| 65 | |
| 66 | /** |
| 67 | * Put an item to the back of the queue. If the queue is almost full |
| 68 | * (the write pointer is right before the read pointer) the producer will |
| 69 | * forcefully discard the oldest item in the head of the queue by incrementing |
| 70 | * the read pointer. |
| 71 | * |
| 72 | * @param atomic_queue The Atomic Queue. |
| 73 | * @param item The pointer to the data to store. |
| 74 | * |
| 75 | * @return PJ_SUCCESS if success. |
| 76 | */ |
| 77 | PJ_DECL(pj_status_t) pj_atomic_queue_put(pj_atomic_queue_t *atomic_queue, |
| 78 | void *item); |
| 79 | |
| 80 | /** |
| 81 | * Get an item from the head of the queue. |
| 82 | * |
| 83 | * @param atomic_queue The Atomic Queue. |
| 84 | * @param item The pointer to data to get the data. |
| 85 | * |
| 86 | * @return PJ_SUCCESS if success. |
| 87 | */ |
| 88 | PJ_DECL(pj_status_t) pj_atomic_queue_get(pj_atomic_queue_t *atomic_queue, |
| 89 | void *item); |
| 90 | |
| 91 | /** |
| 92 | * @} |
| 93 | */ |
| 94 | |
| 95 | PJ_END_DECL |
| 96 | |
| 97 | #endif |