| 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 | #include <pj/assert.h> |
| 19 | #include <pj/atomic_queue.h> |
| 20 | #include <pj/errno.h> |
| 21 | #include <pj/pool.h> |
| 22 | #include <pj/string.h> |
| 23 | #include <atomic> |
| 24 | |
| 25 | #if 0 |
| 26 | # define TRACE_(arg) PJ_LOG(4,arg) |
| 27 | #else |
| 28 | # define TRACE_(expr) |
| 29 | #endif |
| 30 | class AtomicQueue { |
| 31 | public: |
| 32 | /** |
| 33 | * Constructor |
| 34 | */ |
| 35 | AtomicQueue(unsigned maxItemCnt, unsigned itemSize, const char* name = ""): |
| 36 | maxItemCnt_(maxItemCnt), |
| 37 | itemSize_(itemSize), |
| 38 | ptrWrite(0), |
| 39 | ptrRead(0), |
| 40 | buffer(NULL), |
| 41 | name_(name) |
| 42 | { |
| 43 | buffer = new char[maxItemCnt_ * itemSize_]; |
| 44 | |
| 45 | /* Surpress warning when debugging log is disabled */ |
| 46 | PJ_UNUSED_ARG(name_); |
| 47 | |
| 48 | TRACE_((name_, "Created AtomicQueue: maxItemCnt=%d itemSize=%d", |
| 49 | maxItemCnt_, itemSize_)); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Destructor |
| 54 | */ |
| 55 | ~AtomicQueue() |
| 56 | { |
| 57 | delete [] buffer; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get a item from the head of the queue |
| 62 | */ |
| 63 | bool get(void* item) |
| 64 | { |
| 65 | if (ptrRead == ptrWrite) |
| 66 | return false; |
| 67 | |
| 68 | unsigned cur_ptr = ptrRead; |
| 69 | void *p = &buffer[cur_ptr * itemSize_]; |
| 70 | pj_memcpy(item, p, itemSize_); |
| 71 | inc_ptrRead_if_not_yet(cur_ptr); |
| 72 | |
| 73 | TRACE_((name_, "GET: ptrRead=%d ptrWrite=%d\n", |
| 74 | ptrRead.load(), ptrWrite.load())); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Put a item to the back of the queue |
| 81 | */ |
| 82 | void put(void* item) |
| 83 | { |
| 84 | unsigned cur_ptr = ptrWrite; |
| 85 | void *p = &buffer[cur_ptr * itemSize_]; |
| 86 | pj_memcpy(p, item, itemSize_); |
| 87 | unsigned next_ptr = inc_ptrWrite(cur_ptr); |
| 88 | |
| 89 | /* Increment read pointer if next write is overlapping |
| 90 | * (next_ptr == read ptr) |
| 91 | */ |
| 92 | unsigned next_read_ptr = (next_ptr == maxItemCnt_-1)? 0 : (next_ptr+1); |
| 93 | ptrRead.compare_exchange_strong(next_ptr, next_read_ptr); |
| 94 | |
| 95 | TRACE_((name_, "PUT: ptrRead=%d ptrWrite=%d\n", |
| 96 | ptrRead.load(), ptrWrite.load())); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | unsigned maxItemCnt_; |
| 101 | unsigned itemSize_; |
| 102 | std::atomic<unsigned> ptrWrite; |
| 103 | std::atomic<unsigned> ptrRead; |
| 104 | char *buffer; |
| 105 | const char *name_; |
| 106 | |
| 107 | /* Increment read pointer, only if producer not incemented it already. |
| 108 | * Producer may increment the read pointer if the write pointer is |
| 109 | * right before the read pointer (buffer almost full). |
| 110 | */ |
| 111 | bool inc_ptrRead_if_not_yet(unsigned old_ptr) |
| 112 | { |
| 113 | unsigned new_ptr = (old_ptr == maxItemCnt_-1)? 0 : (old_ptr+1); |
| 114 | return ptrRead.compare_exchange_strong(old_ptr, new_ptr); |
| 115 | } |
| 116 | |
| 117 | /* Increment write pointer */ |
| 118 | unsigned inc_ptrWrite(unsigned old_ptr) |
| 119 | { |
| 120 | unsigned new_ptr = (old_ptr == maxItemCnt_-1)? 0 : (old_ptr+1); |
| 121 | if (ptrWrite.compare_exchange_strong(old_ptr, new_ptr)) |
| 122 | return new_ptr; |
| 123 | |
| 124 | /* Should never happen */ |
| 125 | pj_assert(!"There is more than one producer!"); |
| 126 | return old_ptr; |
| 127 | } |
| 128 | |
| 129 | AtomicQueue():maxItemCnt_(0), itemSize_(0), ptrWrite(0), |
| 130 | ptrRead(0), buffer(NULL), name_("") |
| 131 | {} |
| 132 | }; |
| 133 | |
| 134 | struct pj_atomic_queue_t |
| 135 | { |
| 136 | AtomicQueue *aQ; |
| 137 | }; |
| 138 | |
| 139 | PJ_DEF(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool, |
| 140 | unsigned max_item_cnt, |
| 141 | unsigned item_size, |
| 142 | const char *name, |
| 143 | pj_atomic_queue_t **atomic_queue) |
| 144 | { |
| 145 | pj_atomic_queue_t *aqueue; |
| 146 | |
| 147 | PJ_ASSERT_RETURN(pool, PJ_EINVAL); |
| 148 | aqueue = PJ_POOL_ZALLOC_T(pool, pj_atomic_queue_t); |
| 149 | aqueue->aQ = new AtomicQueue(max_item_cnt, item_size, name); |
| 150 | *atomic_queue = aqueue; |
| 151 | return PJ_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | PJ_DEF(pj_status_t) pj_atomic_queue_destroy(pj_atomic_queue_t *atomic_queue) |
| 155 | { |
| 156 | PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ, PJ_EINVAL); |
| 157 | delete atomic_queue->aQ; |
| 158 | atomic_queue->aQ = NULL; |
| 159 | return PJ_SUCCESS; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * For producer, there is write pointer 'ptrWrite' that will be incremented |
| 164 | * every time a item is queued to the back of the queue. |
| 165 | */ |
| 166 | PJ_DEF(pj_status_t) pj_atomic_queue_put(pj_atomic_queue_t *atomic_queue, |
| 167 | void *item) |
| 168 | { |
| 169 | PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ && item, PJ_EINVAL); |
| 170 | atomic_queue->aQ->put(item); |
| 171 | return PJ_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * For consumer, there is read pointer 'ptrRead' that will be incremented |
| 176 | * every time a item is fetched from the head of the queue, only if the |
| 177 | * pointer is not modified by producer (in case of queue full). |
| 178 | */ |
| 179 | PJ_DEF(pj_status_t) pj_atomic_queue_get(pj_atomic_queue_t *atomic_queue, |
| 180 | void *item) |
| 181 | { |
| 182 | PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ && item, PJ_EINVAL); |
| 183 | if (atomic_queue->aQ->get(item)) |
| 184 | return PJ_SUCCESS; |
| 185 | else |
| 186 | return PJ_ENOTFOUND; |
| 187 | } |